Skip to content

Commit 01542bc

Browse files
authored
fix: duplicating ToC ids (#620)
1 parent 11a188e commit 01542bc

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

apps/www/src/lib/components/docs/table-of-contents.svelte

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,20 @@
1818
const hierarchy: TableOfContents = { items: [] };
1919
let currentLevel: TableOfContentsItem | undefined = undefined;
2020
21+
const newIdSet: Set<string> = new Set();
22+
let count = 1;
2123
headings.forEach((heading: HTMLHeadingElement) => {
2224
const level = parseInt(heading.tagName.charAt(1));
2325
if (!heading.id) {
2426
let newId = heading.innerText
2527
.replaceAll(/[^a-zA-Z0-9 ]/g, "")
2628
.replaceAll(" ", "-")
2729
.toLowerCase();
30+
if (newIdSet.has(newId)) {
31+
newId = `${newId}-${count}`;
32+
count++;
33+
}
34+
newIdSet.add(newId);
2835
heading.id = `${newId}`;
2936
}
3037

0 commit comments

Comments
 (0)