Reimplement MD024/no-duplicate-heading using micromark tokens.

This commit is contained in:
David Anson 2024-06-19 21:05:31 -07:00
parent b1b16dabec
commit 96354678dc
6 changed files with 77 additions and 49 deletions

View file

@ -1564,6 +1564,20 @@ function getHeadingStyle(heading) {
return "atx_closed";
}
/**
* Gets the heading text of a Micromark heading token.
*
* @param {Token} heading Micromark heading token.
* @returns {string} Heading text.
*/
function getHeadingText(heading) {
const headingTexts = filterByTypes(
heading.children,
[ "atxHeadingText", "setextHeadingText" ]
);
return headingTexts[0]?.text.replace(/[\r\n]+/g, " ") || "";
}
/**
* HTML tag information.
*
@ -1679,6 +1693,7 @@ module.exports = {
filterByTypes,
getHeadingLevel,
getHeadingStyle,
getHeadingText,
getHtmlTagInfo,
getMicromarkEvents,
getTokenParentOfType,
@ -4575,7 +4590,8 @@ module.exports = {
const { addErrorContext, forEachHeading } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { addErrorContext } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { filterByTypes, getHeadingLevel, getHeadingText } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
// eslint-disable-next-line jsdoc/valid-types
/** @type import("./markdownlint").Rule */
@ -4583,15 +4599,20 @@ module.exports = {
"names": [ "MD024", "no-duplicate-heading" ],
"description": "Multiple headings with the same content",
"tags": [ "headings" ],
"parser": "markdownit",
"parser": "micromark",
"function": function MD024(params, onError) {
const siblingsOnly = !!params.config.siblings_only || false;
const knownContents = [ null, [] ];
let lastLevel = 1;
let knownContent = knownContents[lastLevel];
forEachHeading(params, (heading, content) => {
const headings = filterByTypes(
params.parsers.micromark.tokens,
[ "atxHeading", "setextHeading" ]
);
for (const heading of headings) {
const headingText = getHeadingText(heading);
if (siblingsOnly) {
const newLevel = heading.tag.slice(1);
const newLevel = getHeadingLevel(heading);
while (lastLevel < newLevel) {
lastLevel++;
knownContents[lastLevel] = [];
@ -4603,17 +4624,17 @@ module.exports = {
knownContent = knownContents[newLevel];
}
// @ts-ignore
if (knownContent.includes(content)) {
if (knownContent.includes(headingText)) {
addErrorContext(
onError,
heading.lineNumber,
heading.line.trim()
heading.startLine,
headingText.trim()
);
} else {
// @ts-ignore
knownContent.push(content);
knownContent.push(headingText);
}
});
}
}
};
@ -4632,7 +4653,7 @@ module.exports = {
const { addErrorContext, frontMatterHasTitle } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { filterByTypes, getHeadingLevel } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
const { filterByTypes, getHeadingLevel, getHeadingText } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
// eslint-disable-next-line jsdoc/valid-types
/** @type import("./markdownlint").Rule */
@ -4657,14 +4678,7 @@ module.exports = {
const headingLevel = getHeadingLevel(heading);
if (headingLevel === level) {
if (hasTopLevelHeading || foundFrontMatterTitle) {
const headingTexts = filterByTypes(
heading.children,
[ "atxHeadingText", "setextHeadingText" ]
);
const headingText = headingTexts.
map((token) => token.text).
join(" ").
replace(/[\r\n]+/g, " ");
const headingText = getHeadingText(heading);
addErrorContext(
onError,
heading.startLine,