Reimplement MD014/commands-show-output using micromark tokens.

This commit is contained in:
David Anson 2024-06-22 15:12:37 -07:00
parent ec957029a5
commit 347302169c
6 changed files with 132 additions and 80 deletions

View file

@ -4115,7 +4115,8 @@ module.exports = {
const { addErrorContext, filterTokens } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { addErrorContext } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { filterByTypes } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
const dollarCommandRe = /^(\s*)(\$\s+)/;
@ -4125,47 +4126,45 @@ module.exports = {
"names": [ "MD014", "commands-show-output" ],
"description": "Dollar signs used before commands without showing output",
"tags": [ "code" ],
"parser": "markdownit",
"parser": "micromark",
"function": function MD014(params, onError) {
for (const type of [ "code_block", "fence" ]) {
filterTokens(params, type, (token) => {
const margin = (token.type === "fence") ? 1 : 0;
const dollarInstances = [];
let allDollars = true;
for (let i = token.map[0] + margin; i < token.map[1] - margin; i++) {
const line = params.lines[i];
const lineTrim = line.trim();
if (lineTrim) {
const match = dollarCommandRe.exec(line);
if (match) {
const column = match[1].length + 1;
const length = match[2].length;
dollarInstances.push([ i, lineTrim, column, length ]);
} else {
allDollars = false;
const codeBlocks = filterByTypes(
params.parsers.micromark.tokens,
[ "codeFenced", "codeIndented" ]
);
for (const codeBlock of codeBlocks) {
const codeFlowValues = filterByTypes(
codeBlock.children,
[ "codeFlowValue" ]
);
const dollarMatches = codeFlowValues.
map((codeFlowValue) => ({
"result": codeFlowValue.text.match(dollarCommandRe),
"startColumn": codeFlowValue.startColumn,
"startLine": codeFlowValue.startLine,
"text": codeFlowValue.text
})).
filter((dollarMatch) => dollarMatch.result);
if (dollarMatches.length === codeFlowValues.length) {
for (const dollarMatch of dollarMatches) {
// @ts-ignore
const column = dollarMatch.startColumn + dollarMatch.result[1].length;
// @ts-ignore
const length = dollarMatch.result[2].length;
addErrorContext(
onError,
dollarMatch.startLine,
dollarMatch.text,
undefined,
undefined,
[ column, length ],
{
"editColumn": column,
"deleteCount": length
}
}
);
}
if (allDollars) {
for (const instance of dollarInstances) {
const [ i, lineTrim, column, length ] = instance;
addErrorContext(
onError,
// @ts-ignore
i + 1,
// @ts-ignore
lineTrim,
null,
null,
[ column, length ],
{
"editColumn": column,
"deleteCount": length
}
);
}
}
});
}
}
}
};