Skip to content

Commit f316e68

Browse files
committed
Generate IDs for sectionless headings, and improve/fix existing IDs
1 parent 157eaa6 commit f316e68

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

11ty/CustomLiquid.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { type Options as SlugifyOptions, slugifyWithCounter } from "@sindresorhus/slugify";
12
import { Liquid, type Template } from "liquidjs";
23
import type { LiquidOptions, RenderOptions } from "liquidjs/dist/liquid-options";
34
import compact from "lodash-es/compact";
@@ -9,7 +10,6 @@ import type { GlobalData } from "eleventy.config";
910

1011
import { biblioPattern, getBiblio, getXmlBiblio } from "./biblio";
1112
import { flattenDom, load, type CheerioAnyNode } from "./cheerio";
12-
import { generateId } from "./common";
1313
import { getAcknowledgementsForVersion, type TermsMap } from "./guidelines";
1414
import { resolveTechniqueIdFromHref, understandingToTechniqueLinkSelector } from "./techniques";
1515
import { techniqueToUnderstandingLinkSelector } from "./understanding";
@@ -638,6 +638,9 @@ export class CustomLiquid extends Liquid {
638638
} else $("section#references").remove();
639639
}
640640

641+
const slugify = slugifyWithCounter();
642+
const slugifyOptions: SlugifyOptions = { decamelize: false };
643+
641644
// Allow autogenerating missing top-level section IDs in understanding docs,
642645
// but don't pick up incorrectly-nested sections in some techniques pages (e.g. H91)
643646
const sectionSelector = scope.isUnderstanding ? "section" : "section[id]:not(.obsolete)";
@@ -648,7 +651,8 @@ export class CustomLiquid extends Liquid {
648651
// when we have sections and sidebar skeleton already reordered
649652
const $tocList = $(".sidebar nav ul");
650653
$h2Sections.each((_, el) => {
651-
if (!el.attribs.id) el.attribs.id = generateId($(el).find(sectionH2Selector).text());
654+
if (!el.attribs.id)
655+
el.attribs.id = slugify($(el).find(sectionH2Selector).text(), slugifyOptions);
652656
$("<a></a>")
653657
.attr("href", `#${el.attribs.id}`)
654658
.text(normalizeTocLabel($(el).find(sectionH2Selector).text()))
@@ -672,9 +676,14 @@ export class CustomLiquid extends Liquid {
672676
$(autoIdSectionSelectors.join(", "))
673677
.filter(`:has(${sectionHeadingSelector})`)
674678
.each((_, el) => {
675-
el.attribs.id = generateId($(el).find(sectionHeadingSelector).text());
679+
el.attribs.id = slugify($(el).find(sectionHeadingSelector).text(), slugifyOptions);
676680
});
677681

682+
// Also autogenerate IDs for any headings with no dedicated section nor explicit ID
683+
$(":is(h3, h4, h5):not(:first-child):not([id])").each((_, el) => {
684+
el.attribs.id = slugify($(el).text(), slugifyOptions);
685+
});
686+
678687
return stripHtmlComments($.html());
679688
}
680689
}

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"license": "W3C",
1717
"dependencies": {
1818
"@11ty/eleventy": "^3.1.2",
19+
"@sindresorhus/slugify": "^2.2.1",
1920
"axios": "^1.11.0",
2021
"cheerio": "^1.0.0",
2122
"glob": "^10.3.16",

0 commit comments

Comments
 (0)