Refactor use of micromark so token stream is authentic by shimming undefined link reference handling, remove no-longer-necessary parse operation in MD034.

This commit is contained in:
David Anson 2024-10-21 20:56:22 -07:00
parent 1d9bd4725d
commit 38b4ec0c2f
6 changed files with 369 additions and 122 deletions

View file

@ -499,7 +499,7 @@ function lintContent(
// Parse content into parser tokens
const micromarkTokens = micromark.parse(
content,
{ "freezeTokens": customRulesPresent, "shimReferences": true }
{ "freezeTokens": customRulesPresent }
);
// Hide the content of HTML comments from rules
const preClearedContent = content;

View file

@ -4,8 +4,6 @@
const { addErrorContext } = require("../helpers");
const { filterByPredicate, getHtmlTagInfo, inHtmlFlow } = require("../helpers/micromark-helpers.cjs");
const { parse } = require("../helpers/micromark-parse.cjs");
const { filterByTypesCached } = require("./cache");
// eslint-disable-next-line jsdoc/valid-types
/** @type import("./markdownlint").Rule */
@ -39,6 +37,7 @@ module.exports = {
return false;
},
(token) => {
// Ignore content of inline HTML tags
const { children } = token;
const result = [];
for (let i = 0; i < children.length; i++) {
@ -69,31 +68,25 @@ module.exports = {
}
)
);
const autoLinks = filterByTypesCached([ "literalAutolink" ]);
if (autoLinks.length > 0) {
// Re-parse with correct link/image reference definition handling
const document = params.lines.join("\n");
const tokens = parse(document);
for (const token of literalAutolinks(tokens)) {
const range = [
token.startColumn,
token.endColumn - token.startColumn
];
const fixInfo = {
"editColumn": range[0],
"deleteCount": range[1],
"insertText": `<${token.text}>`
};
addErrorContext(
onError,
token.startLine,
token.text,
undefined,
undefined,
range,
fixInfo
);
}
for (const token of literalAutolinks(params.parsers.micromark.tokens)) {
const range = [
token.startColumn,
token.endColumn - token.startColumn
];
const fixInfo = {
"editColumn": range[0],
"deleteCount": range[1],
"insertText": `<${token.text}>`
};
addErrorContext(
onError,
token.startLine,
token.text,
undefined,
undefined,
range,
fixInfo
);
}
}
};