Update getHeadingText helper to exclude HTML comments from returned heading text (fixes #1411).

This commit is contained in:
David Anson 2025-03-04 21:04:24 -08:00
parent d36a57d680
commit d5cb5c8546
4 changed files with 33 additions and 2 deletions

View file

@ -7,6 +7,8 @@ const { flatTokensSymbol, htmlFlowSymbol } = require("./shared.cjs");
// eslint-disable-next-line jsdoc/valid-types // eslint-disable-next-line jsdoc/valid-types
/** @typedef {import("micromark-util-types", { with: { "resolution-mode": "import" } }).TokenType} TokenType */ /** @typedef {import("micromark-util-types", { with: { "resolution-mode": "import" } }).TokenType} TokenType */
/** @typedef {import("../lib/exports.mjs").MicromarkToken} Token */ /** @typedef {import("../lib/exports.mjs").MicromarkToken} Token */
// eslint-disable-next-line jsdoc/valid-types
/** @typedef {import("../lib/micromark-types.d.mts", { with: { "resolution-mode": "import" } })} */
/** /**
* Determines if a Micromark token is within an htmlFlow type. * Determines if a Micromark token is within an htmlFlow type.
@ -214,8 +216,12 @@ function getHeadingStyle(heading) {
* @returns {string} Heading text. * @returns {string} Heading text.
*/ */
function getHeadingText(heading) { function getHeadingText(heading) {
const headingTexts = getDescendantsByType(heading, [ [ "atxHeadingText", "setextHeadingText" ] ]); const headingText = getDescendantsByType(heading, [ [ "atxHeadingText", "setextHeadingText" ] ])
return headingTexts[0]?.text.replace(/[\r\n]+/g, " ") || ""; .flatMap((descendant) => descendant.children.filter((child) => child.type !== "htmlText"))
.map((data) => data.text)
.join("")
.replace(/[\r\n]+/g, " ");
return headingText || "";
} }
/** /**

View file

@ -95,6 +95,12 @@ BB
{MD024:94} {MD024:94}
## Headings <!-- that differ -->
## Headings <!-- in comments -->
{MD024:-2}
<!-- markdownlint-configure-file { <!-- markdownlint-configure-file {
"heading-style": false, "heading-style": false,
"no-duplicate-heading": { "no-duplicate-heading": {

View file

@ -15554,6 +15554,19 @@ Generated by [AVA](https://avajs.dev).
'no-duplicate-heading', 'no-duplicate-heading',
], ],
}, },
{
errorContext: 'Headings',
errorDetail: null,
errorRange: null,
fixInfo: null,
lineNumber: 100,
ruleDescription: 'Multiple headings with the same content',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md024.md',
ruleNames: [
'MD024',
'no-duplicate-heading',
],
},
{ {
errorContext: 'A', errorContext: 'A',
errorDetail: null, errorDetail: null,
@ -15722,6 +15735,12 @@ Generated by [AVA](https://avajs.dev).
{MD024:94}␊ {MD024:94}␊
## Headings <!-- that differ -->
## Headings <!-- in comments -->
{MD024:-2}␊
<!-- markdownlint-configure-file {␊ <!-- markdownlint-configure-file {␊
"heading-style": false,␊ "heading-style": false,␊
"no-duplicate-heading": {␊ "no-duplicate-heading": {␊