Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/rules/no-duplicate-headings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}]`;
}
Expand Down
42 changes: 42 additions & 0 deletions tests/rules/no-duplicate-headings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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: "" },
},
],
},
],
});

Expand Down
Loading