Add options.markdownItPlugins to support using markdown-it plugins when parsing.

This commit is contained in:
David Anson 2019-01-19 12:52:13 -08:00
parent ff86e1d7f1
commit 8a175955d7
5 changed files with 121 additions and 6 deletions

View file

@ -69,8 +69,8 @@
"max-lines": "off",
"max-lines-per-function": "off",
"max-nested-callbacks": "error",
"max-params": ["error", 8],
"max-statements": ["error", 20],
"max-params": ["error", 9],
"max-statements": ["error", 22],
"max-statements-per-line": "error",
"multiline-comment-style": ["error", "separate-lines"],
"multiline-ternary": "off",

View file

@ -386,6 +386,20 @@ Passing a `resultVersion` of `2` corresponds to a detailed format where each err
includes information about the line number, rule names, description, as well as any
additional detail or context that is available. This is the default.
##### options.markdownItPlugins
Type: `Array` of `Array` of `Function` and plugin parameters
Specifies additional [markdown-it plugins](https://www.npmjs.com/search?q=keywords:markdown-it-plugin)
to use when parsing input. Plugins can be used to support additional syntax and
features for advanced scenarios.
Each item in the top-level `Array` should be of the form:
```js
[ require("markdown-it-plugin"), plugin_param_0, plugin_param_1, ... ]
```
#### callback
Type: `Function` taking (`Error`, `Object`)

View file

@ -5,7 +5,7 @@
const fs = require("fs");
const path = require("path");
const { URL } = require("url");
const md = require("markdown-it")({ "html": true });
const markdownIt = require("markdown-it");
const rules = require("./rules");
const shared = require("./shared");
@ -289,7 +289,14 @@ function uniqueFilterForSortedErrors(value, index, array) {
// Lints a single string
function lintContent(
ruleList, name, content, config, frontMatter, noInlineConfig, resultVersion,
ruleList,
name,
content,
md,
config,
frontMatter,
noInlineConfig,
resultVersion,
callback) {
// Remove UTF-8 byte order marker (if present)
content = content.replace(/^\ufeff/, "");
@ -410,6 +417,7 @@ function lintContent(
function lintFile(
ruleList,
file,
md,
config,
frontMatter,
noInlineConfig,
@ -420,8 +428,8 @@ function lintFile(
if (err) {
return callback(err);
}
lintContent(ruleList, file, content, config, frontMatter, noInlineConfig,
resultVersion, callback);
lintContent(ruleList, file, content, md, config, frontMatter,
noInlineConfig, resultVersion, callback);
}
// Make a/synchronous call to read file
if (synchronous) {
@ -455,6 +463,12 @@ function lintInput(options, synchronous, callback) {
const noInlineConfig = !!options.noInlineConfig;
const resultVersion = (options.resultVersion === undefined) ?
2 : options.resultVersion;
const md = markdownIt({ "html": true });
const markdownItPlugins = options.markdownItPlugins || [];
markdownItPlugins.forEach(function forPlugin(plugin) {
// @ts-ignore
md.use(...plugin);
});
const results = newResults(ruleList);
// Helper to lint the next string or file
function lintNextItem() {
@ -476,6 +490,7 @@ function lintInput(options, synchronous, callback) {
ruleList,
item,
strings[item] || "",
md,
config,
frontMatter,
noInlineConfig,
@ -486,6 +501,7 @@ function lintInput(options, synchronous, callback) {
lintFile(
ruleList,
item,
md,
config,
frontMatter,
noInlineConfig,

View file

@ -36,6 +36,10 @@
"glob": "~7.1.2",
"istanbul": "~0.4.5",
"js-yaml": "~3.12.0",
"markdown-it-for-inline": "~0.1.1",
"markdown-it-mathjax": "~2.0.0",
"markdown-it-sub": "~1.0.0",
"markdown-it-sup": "~1.0.0",
"nodeunit": "~0.11.3",
"rimraf": "~2.6.2",
"toml": "~2.3.3",

View file

@ -4,6 +4,10 @@ const fs = require("fs");
const path = require("path");
const { URL } = require("url");
const md = require("markdown-it")();
const pluginInline = require("markdown-it-for-inline");
const pluginMathjax = require("markdown-it-mathjax");
const pluginSub = require("markdown-it-sub");
const pluginSup = require("markdown-it-sup");
const tv4 = require("tv4");
const packageJson = require("../package.json");
const markdownlint = require("../lib/markdownlint");
@ -952,6 +956,7 @@ module.exports.readmeHeadings = function readmeHeadings(test) {
"##### options.frontMatter",
"##### options.noInlineConfig",
"##### options.resultVersion",
"##### options.markdownItPlugins",
"#### callback",
"#### result",
"### Config",
@ -2451,3 +2456,79 @@ module.exports.customRulesDoc = function customRulesDoc(test) {
test.done();
});
};
module.exports.markdownItPluginsSingle =
function markdownItPluginsSingle(test) {
test.expect(2);
markdownlint({
"strings": {
"string": "# Heading\n\nText [ link ](https://example.com)"
},
"markdownItPlugins": [
[
pluginInline,
"trim_text_plugin",
"text",
function iterator(tokens, index) {
tokens[index].content = tokens[index].content.trim();
}
]
]
}, function callback(err, actual) {
test.ifError(err);
const expected = { "string": [] };
test.deepEqual(actual, expected, "Unexpected issues.");
test.done();
});
};
module.exports.markdownItPluginsMultiple =
function markdownItPluginsMultiple(test) {
test.expect(4);
markdownlint({
"strings": {
"string": "# Heading\n\nText H~2~0 text 29^th^ text"
},
"markdownItPlugins": [
[ pluginSub ],
[ pluginSup ],
[ pluginInline, "check_sub_plugin", "sub_open", test.ok ],
[ pluginInline, "check_sup_plugin", "sup_open", test.ok ]
]
}, function callback(err, actual) {
test.ifError(err);
const expected = { "string": [] };
test.deepEqual(actual, expected, "Unexpected issues.");
test.done();
});
};
module.exports.markdownItPluginsMathjax =
function markdownItPluginsMathjax(test) {
test.expect(2);
markdownlint({
"strings": {
"string":
"# Heading\n" +
"\n" +
"$1 *2* 3$\n" +
"\n" +
"$$1 *2* 3$$\n" +
"\n" +
"$$1\n" +
"+ 2\n" +
"+ 3$$\n"
},
"markdownItPlugins": [ [ pluginMathjax ] ],
"resultVersion": 0
}, function callback(err, actual) {
test.ifError(err);
const expected = {
"string": {
"MD032": [ 8 ]
}
};
test.deepEqual(actual, expected, "Unexpected issues.");
test.done();
});
};