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

View file

@ -345,6 +345,20 @@ function getHeadingStyle(heading) {
return "atx_closed"; 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. * HTML tag information.
* *
@ -460,6 +474,7 @@ module.exports = {
filterByTypes, filterByTypes,
getHeadingLevel, getHeadingLevel,
getHeadingStyle, getHeadingStyle,
getHeadingText,
getHtmlTagInfo, getHtmlTagInfo,
getMicromarkEvents, getMicromarkEvents,
getTokenParentOfType, getTokenParentOfType,

View file

@ -2,7 +2,8 @@
"use strict"; "use strict";
const { addErrorContext, forEachHeading } = require("../helpers"); const { addErrorContext } = require("../helpers");
const { filterByTypes, getHeadingLevel, getHeadingText } = require("../helpers/micromark.cjs");
// eslint-disable-next-line jsdoc/valid-types // eslint-disable-next-line jsdoc/valid-types
/** @type import("./markdownlint").Rule */ /** @type import("./markdownlint").Rule */
@ -10,15 +11,20 @@ module.exports = {
"names": [ "MD024", "no-duplicate-heading" ], "names": [ "MD024", "no-duplicate-heading" ],
"description": "Multiple headings with the same content", "description": "Multiple headings with the same content",
"tags": [ "headings" ], "tags": [ "headings" ],
"parser": "markdownit", "parser": "micromark",
"function": function MD024(params, onError) { "function": function MD024(params, onError) {
const siblingsOnly = !!params.config.siblings_only || false; const siblingsOnly = !!params.config.siblings_only || false;
const knownContents = [ null, [] ]; const knownContents = [ null, [] ];
let lastLevel = 1; let lastLevel = 1;
let knownContent = knownContents[lastLevel]; 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) { if (siblingsOnly) {
const newLevel = heading.tag.slice(1); const newLevel = getHeadingLevel(heading);
while (lastLevel < newLevel) { while (lastLevel < newLevel) {
lastLevel++; lastLevel++;
knownContents[lastLevel] = []; knownContents[lastLevel] = [];
@ -30,16 +36,16 @@ module.exports = {
knownContent = knownContents[newLevel]; knownContent = knownContents[newLevel];
} }
// @ts-ignore // @ts-ignore
if (knownContent.includes(content)) { if (knownContent.includes(headingText)) {
addErrorContext( addErrorContext(
onError, onError,
heading.lineNumber, heading.startLine,
heading.line.trim() headingText.trim()
); );
} else { } else {
// @ts-ignore // @ts-ignore
knownContent.push(content); knownContent.push(headingText);
} }
}); }
} }
}; };

View file

@ -3,7 +3,7 @@
"use strict"; "use strict";
const { addErrorContext, frontMatterHasTitle } = require("../helpers"); const { addErrorContext, frontMatterHasTitle } = require("../helpers");
const { filterByTypes, getHeadingLevel } = require("../helpers/micromark.cjs"); const { filterByTypes, getHeadingLevel, getHeadingText } = require("../helpers/micromark.cjs");
// eslint-disable-next-line jsdoc/valid-types // eslint-disable-next-line jsdoc/valid-types
/** @type import("./markdownlint").Rule */ /** @type import("./markdownlint").Rule */
@ -28,14 +28,7 @@ module.exports = {
const headingLevel = getHeadingLevel(heading); const headingLevel = getHeadingLevel(heading);
if (headingLevel === level) { if (headingLevel === level) {
if (hasTopLevelHeading || foundFrontMatterTitle) { if (hasTopLevelHeading || foundFrontMatterTitle) {
const headingTexts = filterByTypes( const headingText = getHeadingText(heading);
heading.children,
[ "atxHeadingText", "setextHeadingText" ]
);
const headingText = headingTexts.
map((token) => token.text).
join(" ").
replace(/[\r\n]+/g, " ");
addErrorContext( addErrorContext(
onError, onError,
heading.startLine, heading.startLine,

View file

@ -638,7 +638,7 @@ Generated by [AVA](https://avajs.dev).
], ],
}, },
{ {
errorContext: '# Heading', errorContext: 'Heading',
errorDetail: null, errorDetail: null,
errorRange: null, errorRange: null,
fixInfo: null, fixInfo: null,
@ -7156,7 +7156,7 @@ Generated by [AVA](https://avajs.dev).
], ],
}, },
{ {
errorContext: '# Heading 8', errorContext: 'Heading 8',
errorDetail: null, errorDetail: null,
errorRange: null, errorRange: null,
fixInfo: null, fixInfo: null,
@ -14804,7 +14804,7 @@ Generated by [AVA](https://avajs.dev).
], ],
}, },
{ {
errorContext: '## B', errorContext: 'B',
errorDetail: null, errorDetail: null,
errorRange: null, errorRange: null,
fixInfo: null, fixInfo: null,
@ -14817,7 +14817,7 @@ Generated by [AVA](https://avajs.dev).
], ],
}, },
{ {
errorContext: '### C', errorContext: 'C',
errorDetail: null, errorDetail: null,
errorRange: null, errorRange: null,
fixInfo: null, fixInfo: null,
@ -14830,7 +14830,7 @@ Generated by [AVA](https://avajs.dev).
], ],
}, },
{ {
errorContext: '#### G', errorContext: 'G',
errorDetail: null, errorDetail: null,
errorRange: null, errorRange: null,
fixInfo: null, fixInfo: null,
@ -14843,7 +14843,7 @@ Generated by [AVA](https://avajs.dev).
], ],
}, },
{ {
errorContext: '### E', errorContext: 'E',
errorDetail: null, errorDetail: null,
errorRange: null, errorRange: null,
fixInfo: null, fixInfo: null,
@ -14856,7 +14856,7 @@ Generated by [AVA](https://avajs.dev).
], ],
}, },
{ {
errorContext: '# A', errorContext: 'A',
errorDetail: null, errorDetail: null,
errorRange: null, errorRange: null,
fixInfo: null, fixInfo: null,
@ -14869,7 +14869,7 @@ Generated by [AVA](https://avajs.dev).
], ],
}, },
{ {
errorContext: '## B', errorContext: 'B',
errorDetail: null, errorDetail: null,
errorRange: null, errorRange: null,
fixInfo: null, fixInfo: null,
@ -14882,7 +14882,7 @@ Generated by [AVA](https://avajs.dev).
], ],
}, },
{ {
errorContext: '# Heading duplicate content si...', errorContext: 'Heading duplicate content sibl...',
errorDetail: null, errorDetail: null,
errorRange: null, errorRange: null,
fixInfo: null, fixInfo: null,
@ -14908,7 +14908,7 @@ Generated by [AVA](https://avajs.dev).
], ],
}, },
{ {
errorContext: '## BBB ##', errorContext: 'BBB',
errorDetail: null, errorDetail: null,
errorRange: null, errorRange: null,
fixInfo: null, fixInfo: null,
@ -15104,7 +15104,7 @@ Generated by [AVA](https://avajs.dev).
{ {
errors: [ errors: [
{ {
errorContext: '## Heading 1', errorContext: 'Heading 1',
errorDetail: null, errorDetail: null,
errorRange: null, errorRange: null,
fixInfo: null, fixInfo: null,
@ -15117,7 +15117,7 @@ Generated by [AVA](https://avajs.dev).
], ],
}, },
{ {
errorContext: '### Heading 2', errorContext: 'Heading 2',
errorDetail: null, errorDetail: null,
errorRange: null, errorRange: null,
fixInfo: null, fixInfo: null,
@ -15177,7 +15177,7 @@ Generated by [AVA](https://avajs.dev).
{ {
errors: [ errors: [
{ {
errorContext: '### Bug fixes', errorContext: 'Bug fixes',
errorDetail: null, errorDetail: null,
errorRange: null, errorRange: null,
fixInfo: null, fixInfo: null,