1- //! in nav.on-this-page, sets data-active=true on the link to the header you're currently looking at
1+ //! For elements with `[data-highlight-intrapage-link]`, sets
2+ //! `data-highlight-intrapage-link-active=true` to the descendant `<a>` elements
3+ //! (for intra-page navigation) that link to the top-most visible heading.
24
35if ( window . location . hash == "" ) {
46 otp_set_active ( document . querySelector ( "main h2" ) || document . querySelector ( "main h3" ) ) ;
@@ -7,21 +9,26 @@ if(window.location.hash == ""){
79}
810
911/**
10- * remember which elements are on screen. IntersectionObserver only sends us updates.
12+ * Remember which elements are on screen. ` IntersectionObserver` only sends us updates.
1113 * @type {Map<Element, boolean> }
1214 */
1315let otp_state = new Map ( ) ;
1416
1517/**
16- * give data-active=true to the page nav link which points to the given id; set all others to false.
17- * accepts an id string or a DOM node which it will automatically retrieve the id of
18- * @param { string | HTMLElement } id_or_node
18+ * Set ` data-highlight-intrapage-link- active=true` to the page links which point
19+ * to the given id; set all others to `false`. Accepts an id string or a DOM
20+ * node which it will automatically retrieve the id of.
1921 */
2022function otp_set_active ( id_or_node ) {
2123 let id = `#${ id_or_node instanceof HTMLElement ? id_or_node . getAttribute ( "id" ) : id_or_node } ` ;
2224
23- document . querySelectorAll ( ".on-this-page a" ) . forEach ( a => {
24- a . setAttribute ( "data-active" , a . getAttribute ( "href" ) == id ) ;
25+ document . querySelectorAll ( "[data-highlight-intrapage-link] a" ) . forEach ( a => {
26+ const href = a . getAttribute ( "href" ) ;
27+
28+ if ( href ?. includes ( '#' ) ) {
29+ const fragment = href . substring ( href . indexOf ( '#' ) ) ;
30+ a . setAttribute ( "data-highlight-intrapage-link-active" , String ( id === fragment ) ) ;
31+ }
2532 } ) ;
2633}
2734
0 commit comments