1
+ import { type Options as SlugifyOptions , slugifyWithCounter } from "@sindresorhus/slugify" ;
1
2
import { Liquid , type Template } from "liquidjs" ;
2
3
import type { LiquidOptions , RenderOptions } from "liquidjs/dist/liquid-options" ;
3
4
import compact from "lodash-es/compact" ;
@@ -9,7 +10,6 @@ import type { GlobalData } from "eleventy.config";
9
10
10
11
import { biblioPattern , getBiblio , getXmlBiblio } from "./biblio" ;
11
12
import { flattenDom , load , type CheerioAnyNode } from "./cheerio" ;
12
- import { generateId } from "./common" ;
13
13
import { getAcknowledgementsForVersion , type TermsMap } from "./guidelines" ;
14
14
import { resolveTechniqueIdFromHref , understandingToTechniqueLinkSelector } from "./techniques" ;
15
15
import { techniqueToUnderstandingLinkSelector } from "./understanding" ;
@@ -638,6 +638,9 @@ export class CustomLiquid extends Liquid {
638
638
} else $ ( "section#references" ) . remove ( ) ;
639
639
}
640
640
641
+ const slugify = slugifyWithCounter ( ) ;
642
+ const slugifyOptions : SlugifyOptions = { decamelize : false } ;
643
+
641
644
// Allow autogenerating missing top-level section IDs in understanding docs,
642
645
// but don't pick up incorrectly-nested sections in some techniques pages (e.g. H91)
643
646
const sectionSelector = scope . isUnderstanding ? "section" : "section[id]:not(.obsolete)" ;
@@ -648,7 +651,8 @@ export class CustomLiquid extends Liquid {
648
651
// when we have sections and sidebar skeleton already reordered
649
652
const $tocList = $ ( ".sidebar nav ul" ) ;
650
653
$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 ) ;
652
656
$ ( "<a></a>" )
653
657
. attr ( "href" , `#${ el . attribs . id } ` )
654
658
. text ( normalizeTocLabel ( $ ( el ) . find ( sectionH2Selector ) . text ( ) ) )
@@ -672,9 +676,14 @@ export class CustomLiquid extends Liquid {
672
676
$ ( autoIdSectionSelectors . join ( ", " ) )
673
677
. filter ( `:has(${ sectionHeadingSelector } )` )
674
678
. each ( ( _ , el ) => {
675
- el . attribs . id = generateId ( $ ( el ) . find ( sectionHeadingSelector ) . text ( ) ) ;
679
+ el . attribs . id = slugify ( $ ( el ) . find ( sectionHeadingSelector ) . text ( ) , slugifyOptions ) ;
676
680
} ) ;
677
681
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
+
678
687
return stripHtmlComments ( $ . html ( ) ) ;
679
688
}
680
689
}
0 commit comments