From e8c6935638409be7aeb2b02463bf2c207556ff36 Mon Sep 17 00:00:00 2001 From: lumir Date: Wed, 25 Nov 2026 14:15:24 +0900 Subject: [PATCH] fix: compare inline image alt text and URLs in `no-duplicate-headings` --- src/rules/no-duplicate-headings.js | 21 ++++++++---- tests/rules/no-duplicate-headings.test.js | 42 +++++++++++++++++++++++ 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/src/rules/no-duplicate-headings.js b/src/rules/no-duplicate-headings.js index bf3f0cc84..f0909e972 100644 --- a/src/rules/no-duplicate-headings.js +++ b/src/rules/no-duplicate-headings.js @@ -93,13 +93,22 @@ export default /** @satisfies {NoDuplicateHeadingsRuleDefinition} */ ({ } }, - "heading *"({ type, value }) { - if (value) { - headingChildrenSequence += `[${type},${value}]`; // We use a custom sequence representation to keep track of heading children. + "heading *"({ type, alt, url, value }) { + /** @type {string} */ + let comparisonValue = value; + /** @type {string} */ + let text = value; + + if (type === "image") { + comparisonValue = `${alt.length}:${alt}${url.length}:${url}`; + text = alt; + } else if (type === "html") { + text = ""; + } - if (type !== "html") { - headingText += value; - } + if (comparisonValue) { + headingChildrenSequence += `[${type},${comparisonValue.length}:${comparisonValue}]`; + headingText += text; } else { headingChildrenSequence += `[${type}]`; } diff --git a/tests/rules/no-duplicate-headings.test.js b/tests/rules/no-duplicate-headings.test.js index 39b1b5cd4..e8737b9be 100644 --- a/tests/rules/no-duplicate-headings.test.js +++ b/tests/rules/no-duplicate-headings.test.js @@ -96,6 +96,12 @@ ruleTester.run("no-duplicate-headings", rule, { "# foo \\###\n# foo ###", "# foo #\\##\n# foo ###", "# foo \\#", + "# ![Sun](a.png)\n# ![Moon](a.png)", + "# ![Sun](a.png)\n# ![Sun](b.png)", + "# ![Sun,Moon](icon.png)\n# ![Sun](Moon,icon.png)", + "# x![a](b)\n# x][image,1:a1:b", + "# ![]()\n# ![](a.png)\n# ![](b.png)", + "# ![]()\n# ![Sun]()\n# ![Moon]()", "Heading \nHi\n===\n\nHeading\nHi\n===", // The first setext heading uses a Break node (double spaces), so the second setext heading isn't considered a duplicate. { code: dedent` @@ -607,6 +613,42 @@ Heading 1 }, ], }, + { + code: dedent` + # ![Sun](a.png) + + # ![Sun](a.png) + `, + errors: [ + { + messageId: "duplicateHeading", + line: 3, + column: 1, + endLine: 3, + endColumn: 16, + data: { + text: "Sun", + }, + }, + ], + }, + { + code: dedent` + # ![]() + + # ![]() + `, + errors: [ + { + messageId: "duplicateHeading", + line: 3, + column: 1, + endLine: 3, + endColumn: 8, + data: { text: "" }, + }, + ], + }, ], });