mirror of
https://github.com/RGBCube/Site
synced 2025-07-31 13:07:46 +00:00
headers: make anchors truly unique
This commit is contained in:
parent
a01188e892
commit
e799cc1c45
2 changed files with 16 additions and 23 deletions
16
site.ts
16
site.ts
|
@ -70,6 +70,8 @@ site.process([".html"], (pages) => {
|
|||
wrapper.appendChild(element);
|
||||
});
|
||||
|
||||
const encountered: Record<string, boolean> = {};
|
||||
|
||||
document
|
||||
.querySelectorAll(".text-content :where(h1, h2, h3, h4, h5, h6)")
|
||||
.forEach((header) => {
|
||||
|
@ -85,10 +87,20 @@ site.process([".html"], (pages) => {
|
|||
.replace(/-+/g, "-")
|
||||
.trim();
|
||||
|
||||
header.id = textNormalized;
|
||||
let textUnique = textNormalized;
|
||||
let counter = 1;
|
||||
|
||||
while (encountered[textUnique]) {
|
||||
counter++;
|
||||
textUnique = `${textNormalized}-${counter}`;
|
||||
}
|
||||
|
||||
encountered[textUnique] = true;
|
||||
|
||||
header.id = textUnique;
|
||||
|
||||
const link = document.createElement("a");
|
||||
link.setAttribute("href", "#" + textNormalized);
|
||||
link.setAttribute("href", "#" + textUnique);
|
||||
|
||||
header.parentNode!.insertBefore(link, header);
|
||||
link.appendChild(header);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue