@@ -31,6 +31,14 @@ const autoCreatePagetoc = () => {
3131 )
3232 return document . querySelector ( ".pagetoc" )
3333}
34+
35+ const getHeadingText = ( heading ) => {
36+ if ( ! heading ) return ""
37+ const clone = heading . cloneNode ( true )
38+ const headerAnchor = clone . querySelector ( "a.header" )
39+ if ( headerAnchor ) headerAnchor . remove ( )
40+ return clone . textContent . replace ( / \s + / g, " " ) . trim ( )
41+ }
3442const updateFunction = ( ) => {
3543 if ( scrollTimeout ) return // Skip updates if within the cooldown period from a click
3644 const headers = [ ...document . getElementsByClassName ( "header" ) ]
@@ -60,14 +68,12 @@ window.addEventListener("load", () => {
6068 const pagetoc = getPagetoc ( )
6169 const headers = [ ...document . getElementsByClassName ( "header" ) ]
6270 headers . forEach ( ( header ) => {
71+ const headingText = getHeadingText ( header . parentElement )
6372 const link = Object . assign ( document . createElement ( "a" ) , {
64- textContent : header . text ,
73+ textContent : headingText || header . text ,
6574 href : header . href ,
6675 className : `pagetoc-${ header . parentElement . tagName } ` ,
6776 } )
68- if ( header . parentElement . querySelectorAll ( "a" ) . length === 2 ) {
69- link . textContent = header . parentElement . querySelectorAll ( "a" ) [ 1 ] . text
70- }
7177 pagetoc . appendChild ( link )
7278 } )
7379 updateFunction ( )
0 commit comments