Skip to content

Commit

Permalink
fix: duplicating ToC ids (#620)
Browse files Browse the repository at this point in the history
  • Loading branch information
huntabyte authored Jan 8, 2024
1 parent 11a188e commit 01542bc
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions apps/www/src/lib/components/docs/table-of-contents.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,20 @@
const hierarchy: TableOfContents = { items: [] };
let currentLevel: TableOfContentsItem | undefined = undefined;
const newIdSet: Set<string> = new Set();
let count = 1;
headings.forEach((heading: HTMLHeadingElement) => {
const level = parseInt(heading.tagName.charAt(1));
if (!heading.id) {
let newId = heading.innerText
.replaceAll(/[^a-zA-Z0-9 ]/g, "")
.replaceAll(" ", "-")
.toLowerCase();
if (newIdSet.has(newId)) {
newId = `${newId}-${count}`;
count++;
}
newIdSet.add(newId);
heading.id = `${newId}`;
}
Expand Down

1 comment on commit 01542bc

@vercel
Copy link

@vercel vercel bot commented on 01542bc Jan 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.