1
- const toc = document . querySelector ( "#observablehq-toc" ) ;
1
+ const toc = document . querySelector < HTMLElement > ( "#observablehq-toc" ) ;
2
2
if ( toc ) {
3
3
const highlight = toc . appendChild ( document . createElement ( "div" ) ) ;
4
4
highlight . classList . add ( "observablehq-secondary-link-highlight" ) ;
5
- const headings = Array . from ( document . querySelectorAll ( toc . dataset . selector ) ) . reverse ( ) ;
6
- const links = toc . querySelectorAll ( ".observablehq-secondary-link" ) ;
7
- const relink = ( ) => {
5
+ const headings = Array . from ( document . querySelectorAll < HTMLElement > ( toc . dataset . selector ! ) ) . reverse ( ) ;
6
+ const links = toc . querySelectorAll < HTMLElement > ( ".observablehq-secondary-link" ) ;
7
+ const relink = ( ) : HTMLElement | undefined => {
8
8
for ( const link of links ) {
9
9
link . classList . remove ( "observablehq-secondary-link-active" ) ;
10
10
}
11
11
// If there’s a location.hash, highlight that if it’s at the top of the viewport.
12
12
if ( location . hash ) {
13
13
for ( const heading of headings ) {
14
- const hash = heading . querySelector ( "a[href]" ) ?. hash ;
14
+ const hash = heading . querySelector < HTMLAnchorElement > ( "a[href]" ) ?. hash ;
15
15
if ( hash === location . hash ) {
16
16
const top = heading . getBoundingClientRect ( ) . top ;
17
17
if ( 0 < top && top < 40 ) {
18
18
for ( const link of links ) {
19
- if ( link . querySelector ( "a[href]" ) ?. hash === hash ) {
19
+ if ( link . querySelector < HTMLAnchorElement > ( "a[href]" ) ?. hash === hash ) {
20
20
link . classList . add ( "observablehq-secondary-link-active" ) ;
21
21
return link ;
22
22
}
@@ -30,9 +30,9 @@ if (toc) {
30
30
// Otherwise, highlight the last one that’s above the center of the viewport.
31
31
for ( const heading of headings ) {
32
32
if ( heading . getBoundingClientRect ( ) . top >= innerHeight * 0.5 ) continue ;
33
- const hash = heading . querySelector ( "a[href]" ) ?. hash ;
33
+ const hash = heading . querySelector < HTMLAnchorElement > ( "a[href]" ) ?. hash ;
34
34
for ( const link of links ) {
35
- if ( link . querySelector ( "a[href]" ) ?. hash === hash ) {
35
+ if ( link . querySelector < HTMLAnchorElement > ( "a[href]" ) ?. hash === hash ) {
36
36
link . classList . add ( "observablehq-secondary-link-active" ) ;
37
37
return link ;
38
38
}
@@ -42,7 +42,7 @@ if (toc) {
42
42
} ;
43
43
const intersected = ( ) => {
44
44
const link = relink ( ) ;
45
- highlight . style = link ? `top: ${ link . offsetTop } px; height: ${ link . offsetHeight } px;` : "" ;
45
+ highlight . style . cssText = link ? `top: ${ link . offsetTop } px; height: ${ link . offsetHeight } px;` : "" ;
46
46
} ;
47
47
const observer = new IntersectionObserver ( intersected , { rootMargin : "0px 0px -50% 0px" } ) ;
48
48
for ( const heading of headings ) observer . observe ( heading ) ;
0 commit comments