Allow options.markdownItFactory to be implemented asynchronously so the markdown-it parser import can be deferred.

This commit is contained in:
David Anson 2024-12-27 21:22:14 -08:00
parent d4b981bcb3
commit 44c302fe0b
6 changed files with 448 additions and 249 deletions

View file

@ -580,14 +580,22 @@ declaring the dependency and returning an instance from this factory. If any
[`markdown-it` plugins][markdown-it-plugin] are needed, they should be `use`d by [`markdown-it` plugins][markdown-it-plugin] are needed, they should be `use`d by
the caller before returning the `markdown-it` instance. the caller before returning the `markdown-it` instance.
For compatibility with previous versions of `markdownlint`, this function can be For compatibility with previous versions of `markdownlint`, this function should
implemented like: be similar to:
```javascript ```javascript
import markdownIt from "markdown-it"; import markdownIt from "markdown-it";
const markdownItFactory = () => markdownIt({ "html": true }); const markdownItFactory = () => markdownIt({ "html": true });
``` ```
When an asynchronous implementation of `lint` is being invoked (e.g., via
`markdownlint/async` or `markdownlint/promise`), this function can return a
`Promise` in order to defer the import of `markdown-it`:
```javascript
const markdownItFactory = () => import("markdown-it").then((module) => module.default({ "html": true }));
```
> Note that this function is only invoked when a `markdown-it` parser is > Note that this function is only invoked when a `markdown-it` parser is
> needed. None of the built-in rules use the `markdown-it` parser, so it is only > needed. None of the built-in rules use the `markdown-it` parser, so it is only
> invoked when one or more [custom rules][custom-rules] are present that use the > invoked when one or more [custom rules][custom-rules] are present that use the

View file

@ -98,7 +98,7 @@ options = {
"frontMatter": /---/, "frontMatter": /---/,
"handleRuleFailures": false, "handleRuleFailures": false,
"noInlineConfig": false, "noInlineConfig": false,
"markdownItFactory": () => new markdownIt() "markdownItFactory": () => markdownIt()
}; };
assertLintResults(lintSync(options)); assertLintResults(lintSync(options));

View file

@ -5,7 +5,7 @@
const { newLineRe } = require("../helpers"); const { newLineRe } = require("../helpers");
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/52529 // @ts-expect-error https://github.com/microsoft/TypeScript/issues/52529
/** @typedef {import("markdownlint").MarkdownItFactory} MarkdownItFactory */ /** @typedef {import("markdownlint").MarkdownIt} MarkdownIt */
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/52529 // @ts-expect-error https://github.com/microsoft/TypeScript/issues/52529
/** @typedef {import("markdownlint").MarkdownItToken} MarkdownItToken */ /** @typedef {import("markdownlint").MarkdownItToken} MarkdownItToken */
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/52529 // @ts-expect-error https://github.com/microsoft/TypeScript/issues/52529
@ -154,13 +154,12 @@ function annotateAndFreezeTokens(tokens, lines) {
/** /**
* Gets an array of markdown-it tokens for the input. * Gets an array of markdown-it tokens for the input.
* *
* @param {MarkdownItFactory} markdownItFactory Function to create a markdown-it parser. * @param {MarkdownIt} markdownIt Instance of the markdown-it parser.
* @param {string} content Markdown content. * @param {string} content Markdown content.
* @param {string[]} lines Lines of Markdown content. * @param {string[]} lines Lines of Markdown content.
* @returns {MarkdownItToken[]} Array of markdown-it tokens. * @returns {MarkdownItToken[]} Array of markdown-it tokens.
*/ */
function getMarkdownItTokens(markdownItFactory, content, lines) { function getMarkdownItTokens(markdownIt, content, lines) {
const markdownIt = markdownItFactory();
const tokens = markdownIt.parse(content, {}); const tokens = markdownIt.parse(content, {});
annotateAndFreezeTokens(tokens, lines); annotateAndFreezeTokens(tokens, lines);
return tokens; return tokens;

View file

@ -373,7 +373,7 @@ export type MarkdownIt = {
/** /**
* Gets an instance of the markdown-it parser. Any plugins should already have been loaded. * Gets an instance of the markdown-it parser. Any plugins should already have been loaded.
*/ */
export type MarkdownItFactory = () => MarkdownIt; export type MarkdownItFactory = () => MarkdownIt | Promise<MarkdownIt>;
/** /**
* Configuration options. * Configuration options.
*/ */

View file

@ -443,8 +443,7 @@ function getEnabledRulesPerLineNumber(
* Lints a string containing Markdown content. * Lints a string containing Markdown content.
* *
* @param {Rule[]} ruleList List of rules. * @param {Rule[]} ruleList List of rules.
* @param {Object.<string, string[]>} aliasToRuleNames Map of alias to rule * @param {Object.<string, string[]>} aliasToRuleNames Map of alias to rule names.
* names.
* @param {string} name Identifier for the content. * @param {string} name Identifier for the content.
* @param {string} content Markdown content. * @param {string} content Markdown content.
* @param {MarkdownItFactory} markdownItFactory Function to create a markdown-it parser. * @param {MarkdownItFactory} markdownItFactory Function to create a markdown-it parser.
@ -454,6 +453,7 @@ function getEnabledRulesPerLineNumber(
* @param {boolean} handleRuleFailures Whether to handle exceptions in rules. * @param {boolean} handleRuleFailures Whether to handle exceptions in rules.
* @param {boolean} noInlineConfig Whether to allow inline configuration. * @param {boolean} noInlineConfig Whether to allow inline configuration.
* @param {number} resultVersion Version of the LintResults object to return. * @param {number} resultVersion Version of the LintResults object to return.
* @param {boolean} synchronous Whether to execute synchronously.
* @param {LintContentCallback} callback Callback (err, result) function. * @param {LintContentCallback} callback Callback (err, result) function.
* @returns {void} * @returns {void}
*/ */
@ -469,7 +469,10 @@ function lintContent(
handleRuleFailures, handleRuleFailures,
noInlineConfig, noInlineConfig,
resultVersion, resultVersion,
synchronous,
callback) { callback) {
// Provide a consistent error-reporting callback
const callbackError = (error) => callback(error instanceof Error ? error : new Error(error));
// Remove UTF-8 byte order marker (if present) // Remove UTF-8 byte order marker (if present)
content = content.replace(/^\uFEFF/, ""); content = content.replace(/^\uFEFF/, "");
// Remove front matter // Remove front matter
@ -501,9 +504,8 @@ function lintContent(
content = helpers.clearHtmlCommentText(content); content = helpers.clearHtmlCommentText(content);
// Parse content into lines and get markdown-it tokens // Parse content into lines and get markdown-it tokens
const lines = content.split(helpers.newLineRe); const lines = content.split(helpers.newLineRe);
const markdownitTokens = needMarkdownItTokens ? // Function to run after fetching markdown-it tokens (when needed)
requireMarkdownItCjs().getMarkdownItTokens(markdownItFactory, preClearedContent, lines) : const lintContentInternal = (markdownitTokens) => {
[];
// Create (frozen) parameters for rules // Create (frozen) parameters for rules
/** @type {MarkdownParsers} */ /** @type {MarkdownParsers} */
// @ts-ignore // @ts-ignore
@ -678,8 +680,7 @@ function lintContent(
} }
return null; return null;
}; };
// eslint-disable-next-line jsdoc/require-jsdoc const formatResults = () => {
function formatResults() {
// Sort results by rule name by line number // Sort results by rule name by line number
results.sort((a, b) => ( results.sort((a, b) => (
a.ruleName.localeCompare(b.ruleName) || a.ruleName.localeCompare(b.ruleName) ||
@ -723,7 +724,7 @@ function lintContent(
} }
} }
return results; return results;
} };
// Run all rules // Run all rules
const ruleListAsync = enabledRuleList.filter((rule) => rule.asynchronous); const ruleListAsync = enabledRuleList.filter((rule) => rule.asynchronous);
const ruleListSync = enabledRuleList.filter((rule) => !rule.asynchronous); const ruleListSync = enabledRuleList.filter((rule) => !rule.asynchronous);
@ -732,8 +733,6 @@ function lintContent(
...ruleListSync ...ruleListSync
]; ];
const callbackSuccess = () => callback(null, formatResults()); const callbackSuccess = () => callback(null, formatResults());
const callbackError =
(error) => callback(error instanceof Error ? error : new Error(error));
try { try {
const ruleResults = ruleListAsyncFirst.map(forRule); const ruleResults = ruleListAsyncFirst.map(forRule);
if (ruleListAsync.length > 0) { if (ruleListAsync.length > 0) {
@ -748,6 +747,25 @@ function lintContent(
} finally { } finally {
cacheInitialize(); cacheInitialize();
} }
};
if (!needMarkdownItTokens || synchronous) {
// Need/able to call into markdown-it and lintContentInternal synchronously
const markdownItTokens = needMarkdownItTokens ?
requireMarkdownItCjs().getMarkdownItTokens(markdownItFactory(), preClearedContent, lines) :
[];
lintContentInternal(markdownItTokens);
} else {
// Need to call into markdown-it and lintContentInternal asynchronously
Promise.all([
// eslint-disable-next-line no-inline-comments
import(/* webpackMode: "eager" */ "./markdownit.cjs"),
// eslint-disable-next-line no-promise-executor-return
new Promise((resolve) => resolve(markdownItFactory()))
]).then(([ markdownitCjs, markdownIt ]) => {
const markdownItTokens = markdownitCjs.getMarkdownItTokens(markdownIt, preClearedContent, lines);
lintContentInternal(markdownItTokens);
}).catch(callbackError);
}
} }
/** /**
@ -799,6 +817,7 @@ function lintFile(
handleRuleFailures, handleRuleFailures,
noInlineConfig, noInlineConfig,
resultVersion, resultVersion,
synchronous,
callback callback
); );
} }
@ -919,6 +938,7 @@ function lintInput(options, synchronous, callback) {
handleRuleFailures, handleRuleFailures,
noInlineConfig, noInlineConfig,
resultVersion, resultVersion,
synchronous,
lintWorkerCallback lintWorkerCallback
); );
} else if (concurrency === 0) { } else if (concurrency === 0) {
@ -1494,7 +1514,7 @@ export function getVersion() {
* Gets an instance of the markdown-it parser. Any plugins should already have been loaded. * Gets an instance of the markdown-it parser. Any plugins should already have been loaded.
* *
* @callback MarkdownItFactory * @callback MarkdownItFactory
* @returns {MarkdownIt} Instance of the markdown-it parser. * @returns {MarkdownIt|Promise<MarkdownIt>} Instance of the markdown-it parser.
*/ */
/** /**

View file

@ -676,6 +676,178 @@ test("customRulesMarkdownItFactoryUndefined", (t) => {
); );
}); });
test("customRulesMarkdownItFactoryNotNeededSync", (t) => {
t.plan(1);
/** @type {import("markdownlint").Options} */
const options = {
"customRules": [
{
"names": [ "name" ],
"description": "description",
"tags": [ "tag" ],
"parser": "none",
"function": () => {}
}
],
"markdownItFactory": () => t.fail(),
"strings": {
"string": "# Heading\n"
}
};
t.pass();
return lintSync(options);
});
test("customRulesMarkdownItFactoryNeededSync", (t) => {
t.plan(1);
/** @type {import("markdownlint").Options} */
const options = {
"customRules": [
{
"names": [ "name" ],
"description": "description",
"tags": [ "tag" ],
"parser": "markdownit",
"function": () => {}
}
],
"markdownItFactory": () => t.pass() && markdownIt(),
"strings": {
"string": "# Heading\n"
}
};
return lintSync(options);
});
test("customRulesMarkdownItFactoryNotNeededAsync", (t) => {
t.plan(1);
/** @type {import("markdownlint").Options} */
const options = {
"customRules": [
{
"names": [ "name" ],
"description": "description",
"tags": [ "tag" ],
"parser": "none",
"function": () => {}
}
],
"markdownItFactory": () => t.fail(),
"strings": {
"string": "# Heading\n"
}
};
t.pass();
return lintPromise(options);
});
test("customRulesMarkdownItFactoryNeededAsyncRunsSync", (t) => {
t.plan(1);
/** @type {import("markdownlint").Options} */
const options = {
"customRules": [
{
"names": [ "name" ],
"description": "description",
"tags": [ "tag" ],
"parser": "markdownit",
"function": () => {}
}
],
"markdownItFactory": () => t.pass() && markdownIt(),
"strings": {
"string": "# Heading\n"
}
};
return lintPromise(options);
});
test("customRulesMarkdownItFactoryNeededAsyncRunsAsync", (t) => {
t.plan(1);
/** @type {import("markdownlint").Options} */
const options = {
"customRules": [
{
"names": [ "name" ],
"description": "description",
"tags": [ "tag" ],
"parser": "markdownit",
"function": () => {}
}
],
"markdownItFactory": () => t.pass() && Promise.resolve(markdownIt()),
"strings": {
"string": "# Heading\n"
}
};
return lintPromise(options);
});
test("customRulesMarkdownItFactoryNeededAsyncRunsAsyncWithImport", (t) => {
t.plan(1);
/** @type {import("markdownlint").Options} */
const options = {
"customRules": [
{
"names": [ "name" ],
"description": "description",
"tags": [ "tag" ],
"parser": "markdownit",
"function": () => {}
}
],
"markdownItFactory": () => import("markdown-it").then((module) => t.pass() && module.default()),
"strings": {
"string": "# Heading\n"
}
};
return lintPromise(options);
});
test("customRulesMarkdownItInstanceCanBeReusedSync", (t) => {
t.plan(1);
const markdownItInstance = markdownItFactory();
/** @type {import("markdownlint").Options} */
const options = {
"customRules": [
{
"names": [ "name" ],
"description": "description",
"tags": [ "tag" ],
"parser": "markdownit",
"function": () => {}
}
],
"markdownItFactory": () => markdownItInstance,
"strings": {
"string": "# Heading"
}
};
t.deepEqual(lintSync(options), lintSync(options));
});
test("customRulesMarkdownItInstanceCanBeReusedAsync", async(t) => {
t.plan(1);
const markdownItInstance = markdownItFactory();
/** @type {import("markdownlint").Options} */
const options = {
"customRules": [
{
"names": [ "name" ],
"description": "description",
"tags": [ "tag" ],
"parser": "markdownit",
"function": () => {}
}
],
"markdownItFactory": () => Promise.resolve(markdownItInstance),
"strings": {
"string": "# Heading"
}
};
t.deepEqual(await lintPromise(options), await lintPromise(options));
});
test("customRulesMarkdownItParamsTokensSameObject", (t) => { test("customRulesMarkdownItParamsTokensSameObject", (t) => {
t.plan(1); t.plan(1);
/** @type {import("markdownlint").Options} */ /** @type {import("markdownlint").Options} */