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
/** @typedef {import("micromark-util-types", { with: { "resolution-mode": "import" } }).TokenType} TokenType */
/** @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.
@ -214,8 +216,12 @@ function getHeadingStyle(heading) {
* @returns {string} Heading text.
*/
function getHeadingText(heading) {
const headingTexts = getDescendantsByType(heading, [ [ "atxHeadingText", "setextHeadingText" ] ]);
return headingTexts[0]?.text.replace(/[\r\n]+/g, " ") || "";
const headingText = getDescendantsByType(heading, [ [ "atxHeadingText", "setextHeadingText" ] ])
.flatMap((descendant) => descendant.children.filter((child) => child.type !== "htmlText"))
.map((data) => data.text)
.join("")
.replace(/[\r\n]+/g, " ");
return headingText || "";
}
/**