Skip to content

Commit d5cb5c8

Browse files
committed
Update getHeadingText helper to exclude HTML comments from returned heading text (fixes #1411).
1 parent d36a57d commit d5cb5c8

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

helpers/micromark-helpers.cjs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const { flatTokensSymbol, htmlFlowSymbol } = require("./shared.cjs");
77
// eslint-disable-next-line jsdoc/valid-types
88
/** @typedef {import("micromark-util-types", { with: { "resolution-mode": "import" } }).TokenType} TokenType */
99
/** @typedef {import("../lib/exports.mjs").MicromarkToken} Token */
10+
// eslint-disable-next-line jsdoc/valid-types
11+
/** @typedef {import("../lib/micromark-types.d.mts", { with: { "resolution-mode": "import" } })} */
1012

1113
/**
1214
* Determines if a Micromark token is within an htmlFlow type.
@@ -214,8 +216,12 @@ function getHeadingStyle(heading) {
214216
* @returns {string} Heading text.
215217
*/
216218
function getHeadingText(heading) {
217-
const headingTexts = getDescendantsByType(heading, [ [ "atxHeadingText", "setextHeadingText" ] ]);
218-
return headingTexts[0]?.text.replace(/[\r\n]+/g, " ") || "";
219+
const headingText = getDescendantsByType(heading, [ [ "atxHeadingText", "setextHeadingText" ] ])
220+
.flatMap((descendant) => descendant.children.filter((child) => child.type !== "htmlText"))
221+
.map((data) => data.text)
222+
.join("")
223+
.replace(/[\r\n]+/g, " ");
224+
return headingText || "";
219225
}
220226

221227
/**

test/heading-duplicate-content-siblings-only.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ BB
9595

9696
{MD024:94}
9797

98+
## Headings <!-- that differ -->
99+
100+
## Headings <!-- in comments -->
101+
102+
{MD024:-2}
103+
98104
<!-- markdownlint-configure-file {
99105
"heading-style": false,
100106
"no-duplicate-heading": {

test/snapshots/markdownlint-test-scenarios.mjs.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15554,6 +15554,19 @@ Generated by [AVA](https://avajs.dev).
1555415554
'no-duplicate-heading',
1555515555
],
1555615556
},
15557+
{
15558+
errorContext: 'Headings',
15559+
errorDetail: null,
15560+
errorRange: null,
15561+
fixInfo: null,
15562+
lineNumber: 100,
15563+
ruleDescription: 'Multiple headings with the same content',
15564+
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md024.md',
15565+
ruleNames: [
15566+
'MD024',
15567+
'no-duplicate-heading',
15568+
],
15569+
},
1555715570
{
1555815571
errorContext: 'A',
1555915572
errorDetail: null,
@@ -15722,6 +15735,12 @@ Generated by [AVA](https://avajs.dev).
1572215735
1572315736
{MD024:94}␊
1572415737
15738+
## Headings <!-- that differ -->␊
15739+
15740+
## Headings <!-- in comments -->␊
15741+
15742+
{MD024:-2}␊
15743+
1572515744
<!-- markdownlint-configure-file {␊
1572615745
"heading-style": false,␊
1572715746
"no-duplicate-heading": {␊
Binary file not shown.

0 commit comments

Comments
 (0)