Introduce options.markdownItFactory (and remove options.markdownItPlugins) so the markdown-it parser can be removed as a direct dependency because it is no longer used by default.

This commit is contained in:
David Anson 2024-12-25 20:42:32 -08:00
parent 3cbe1cb6c5
commit d4b981bcb3
11 changed files with 172 additions and 67 deletions

View file

@ -4,6 +4,7 @@ import fs from "node:fs/promises";
import { createRequire } from "node:module";
const require = createRequire(import.meta.url);
import test from "ava";
import markdownIt from "markdown-it";
import { lint as lintAsync } from "markdownlint/async";
import { lint as lintPromise } from "markdownlint/promise";
import { lint as lintSync } from "markdownlint/sync";
@ -13,6 +14,8 @@ import { __filename, importWithTypeJson } from "./esm-helpers.mjs";
const packageJson = await importWithTypeJson(import.meta, "../package.json");
const { homepage, version } = packageJson;
const markdownItFactory = () => markdownIt({ "html": true });
test("customRulesV0", (t) => new Promise((resolve) => {
t.plan(4);
const customRulesMd = "./test/custom-rules.md";
@ -20,6 +23,7 @@ test("customRulesV0", (t) => new Promise((resolve) => {
const options = {
"customRules": customRules.all,
"files": [ customRulesMd ],
markdownItFactory,
"resultVersion": 0
};
lintAsync(options, function callback(err, actualResult) {
@ -92,6 +96,7 @@ test("customRulesV1", (t) => new Promise((resolve) => {
const options = {
"customRules": customRules.all,
"files": [ customRulesMd ],
markdownItFactory,
"resultVersion": 1
};
lintAsync(options, function callback(err, actualResult) {
@ -223,6 +228,7 @@ test("customRulesV2", (t) => new Promise((resolve) => {
const options = {
"customRules": customRules.all,
"files": [ customRulesMd ],
markdownItFactory,
"resultVersion": 2
};
lintAsync(options, function callback(err, actualResult) {
@ -351,6 +357,7 @@ test("customRulesConfig", (t) => new Promise((resolve) => {
},
"letters-e-x": false
},
markdownItFactory,
"resultVersion": 0
};
lintAsync(options, function callback(err, actualResult) {
@ -376,6 +383,7 @@ test("customRulesNpmPackage", (t) => new Promise((resolve) => {
require("./rules/npm"),
require("markdownlint-rule-extended-ascii")
],
markdownItFactory,
"strings": {
"string": "# Text\n\n---\n\nText ✅\n"
},
@ -555,11 +563,12 @@ test("customRulesParserUndefined", (t) => {
}
}
],
markdownItFactory,
"strings": {
"string": "# Heading\n"
}
};
return lintPromise(options).then(() => null);
return lintPromise(options);
});
test("customRulesParserNone", (t) => {
@ -583,7 +592,7 @@ test("customRulesParserNone", (t) => {
"string": "# Heading\n"
}
};
return lintPromise(options).then(() => null);
return lintPromise(options);
});
test("customRulesParserMarkdownIt", (t) => {
@ -606,11 +615,12 @@ test("customRulesParserMarkdownIt", (t) => {
}
}
],
markdownItFactory,
"strings": {
"string": "# Heading\n"
}
};
return lintPromise(options).then(() => null);
return lintPromise(options);
});
test("customRulesParserMicromark", (t) => {
@ -637,7 +647,33 @@ test("customRulesParserMicromark", (t) => {
"string": "# Heading\n"
}
};
return lintPromise(options).then(() => null);
return lintPromise(options);
});
test("customRulesMarkdownItFactoryUndefined", (t) => {
t.plan(1);
/** @type {import("markdownlint").Options} */
const options = {
"customRules": [
{
"names": [ "name" ],
"description": "description",
"tags": [ "tag" ],
"parser": "markdownit",
"function": () => {}
}
],
"strings": {
"string": "# Heading\n"
}
};
t.throws(
() => lintSync(options),
{
"message": "The option 'markdownItFactory' was required (due to the option 'customRules' including a rule requiring the 'markdown-it' parser), but 'markdownItFactory' was not set."
},
"No exception when markdownItFactory is undefined."
);
});
test("customRulesMarkdownItParamsTokensSameObject", (t) => {
@ -657,11 +693,12 @@ test("customRulesMarkdownItParamsTokensSameObject", (t) => {
}
}
],
markdownItFactory,
"strings": {
"string": "# Heading\n"
}
};
return lintPromise(options).then(() => null);
return lintPromise(options);
});
test("customRulesMarkdownItTokensSnapshot", (t) => {
@ -680,13 +717,14 @@ test("customRulesMarkdownItTokensSnapshot", (t) => {
}
}
],
markdownItFactory,
"noInlineConfig": true
};
return fs
.readFile("./test/every-markdown-syntax.md", "utf8")
.then((content) => {
options.strings = { "content": content.split(newLineRe).join("\n") };
return lintPromise(options).then(() => null);
return lintPromise(options);
});
});
@ -712,7 +750,7 @@ test("customRulesMicromarkTokensSnapshot", (t) => {
.readFile("./test/every-markdown-syntax.md", "utf8")
.then((content) => {
options.strings = { "content": content.split(newLineRe).join("\n") };
return lintPromise(options).then(() => null);
return lintPromise(options);
});
});
@ -1665,7 +1703,8 @@ test("customRulesLintJavaScript", (t) => new Promise((resolve) => {
/** @type {import("markdownlint").Options} */
const options = {
"customRules": customRules.lintJavaScript,
"files": "test/lint-javascript.md"
"files": "test/lint-javascript.md",
markdownItFactory
};
lintAsync(options, (err, actual) => {
t.falsy(err);
@ -1693,7 +1732,8 @@ test("customRulesValidateJson", (t) => new Promise((resolve) => {
/** @type {import("markdownlint").Options} */
const options = {
"customRules": customRules.validateJson,
"files": "test/validate-json.md"
"files": "test/validate-json.md",
markdownItFactory
};
lintAsync(options, (err, actual) => {
t.falsy(err);
@ -1792,9 +1832,10 @@ test("customRulesParamsAreFrozen", (t) => {
"function": assertParamsFrozen
}
],
"files": [ "README.md" ]
"files": [ "README.md" ],
markdownItFactory
};
return lintPromise(options).then(() => null);
return lintPromise(options);
});
test("customRulesParamsAreStable", (t) => {
@ -1862,7 +1903,7 @@ test("customRulesParamsAreStable", (t) => {
"string": "# Heading"
}
};
return lintPromise(options).then(() => null);
return lintPromise(options);
});
test("customRulesAsyncReadFiles", (t) => {

View file

@ -30,6 +30,23 @@ const ajvOptions = {
"allowUnionTypes": true
};
/**
* Gets an instance of a markdown-it factory, suitable for use with options.markdownItFactory.
*
* @param {import("../lib/markdownlint.mjs").Plugin[]} markdownItPlugins Additional markdown-it plugins.
* @returns {import("../lib/markdownlint.mjs").MarkdownItFactory} Function to create a markdown-it parser.
*/
function getMarkdownItFactory(markdownItPlugins) {
return () => {
const md = markdownIt({ "html": true });
for (const markdownItPlugin of markdownItPlugins) {
// @ts-ignore
md.use(...markdownItPlugin);
}
return md;
};
}
test("simpleAsync", (t) => new Promise((resolve) => {
t.plan(2);
const options = {
@ -622,7 +639,7 @@ test("readmeHeadings", (t) => new Promise((resolve) => {
"##### options.frontMatter",
"##### options.fs",
"##### options.handleRuleFailures",
"##### options.markdownItPlugins",
"##### options.markdownItFactory",
"##### options.noInlineConfig",
"##### options.resultVersion",
"##### options.strings",
@ -1054,9 +1071,9 @@ test("markdownItPluginsSingle", (t) => new Promise((resolve) => {
},
// Use a markdown-it custom rule so the markdown-it plugin will be run
"customRules": customRules.anyBlockquote,
"markdownItPlugins": [
"markdownItFactory": getMarkdownItFactory([
[ pluginInline, "check_text_plugin", "text", () => t.true(true) ]
]
])
}, function callback(err, actual) {
t.falsy(err);
const expected = { "string": [] };
@ -1073,12 +1090,12 @@ test("markdownItPluginsMultiple", (t) => new Promise((resolve) => {
},
// Use a markdown-it custom rule so the markdown-it plugin will be run
"customRules": customRules.anyBlockquote,
"markdownItPlugins": [
"markdownItFactory": getMarkdownItFactory([
[ pluginSub ],
[ pluginSup ],
[ pluginInline, "check_sub_plugin", "sub_open", () => t.true(true) ],
[ pluginInline, "check_sup_plugin", "sup_open", () => t.true(true) ]
]
])
}, function callback(err, actual) {
t.falsy(err);
const expected = { "string": [] };
@ -1093,9 +1110,9 @@ test("markdownItPluginsNoMarkdownIt", (t) => new Promise((resolve) => {
"strings": {
"string": "# Heading\n\nText\n"
},
"markdownItPlugins": [
"markdownItFactory": getMarkdownItFactory([
[ pluginInline, "check_text_plugin", "text", () => t.fail() ]
]
])
}, function callback(err, actual) {
t.falsy(err);
const expected = { "string": [] };
@ -1115,9 +1132,9 @@ test("markdownItPluginsUnusedUncalled", (t) => new Promise((resolve) => {
},
// Use a markdown-it custom rule so the markdown-it plugin will be run
"customRules": customRules.anyBlockquote,
"markdownItPlugins": [
"markdownItFactory": getMarkdownItFactory([
[ pluginInline, "check_text_plugin", "text", () => t.fail() ]
]
])
}, function callback(err, actual) {
t.falsy(err);
const expected = { "string": [] };
@ -1184,7 +1201,8 @@ test("token-map-spans", (t) => {
}
}
],
"files": [ "./test/token-map-spans.md" ]
"files": [ "./test/token-map-spans.md" ],
"markdownItFactory": getMarkdownItFactory([])
};
lintSync(options);
});