1
Fork 0
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:
RGBCube 2025-06-02 23:18:15 +03:00
parent a01188e892
commit e799cc1c45
Signed by: RGBCube
SSH key fingerprint: SHA256:CzqbPcfwt+GxFYNnFVCqoN5Itn4YFrshg1TrnACpA5M
2 changed files with 16 additions and 23 deletions

16
site.ts
View file

@ -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);