1
1
import { svg } from '../svg.js' ;
2
2
3
3
// scroll to anchor while respecting the `user-content` prefix that exists on the target
4
- function scrollToAnchor ( encodedId , initial ) {
5
- // abort if the browser has already scrolled to another anchor during page load
6
- if ( ! encodedId || ( initial && document . querySelector ( ':target' ) ) ) return ;
4
+ function scrollToAnchor ( encodedId ) {
5
+ if ( ! encodedId ) return ;
7
6
const id = decodeURIComponent ( encodedId ) ;
8
7
let el = document . getElementById ( `user-content-${ id } ` ) ;
9
8
@@ -32,7 +31,7 @@ export function initMarkupAnchors() {
32
31
33
32
for ( const markupEl of markupEls ) {
34
33
// create link icons for markup headings, the resulting link href will remove `user-content-`
35
- for ( const heading of markupEl . querySelectorAll ( `:is( h1, h2, h3, h4, h5, h6` ) ) {
34
+ for ( const heading of markupEl . querySelectorAll ( ' h1, h2, h3, h4, h5, h6' ) ) {
36
35
const originalId = heading . id . replace ( / ^ u s e r - c o n t e n t - / , '' ) ;
37
36
const a = document . createElement ( 'a' ) ;
38
37
a . classList . add ( 'anchor' ) ;
@@ -59,10 +58,13 @@ export function initMarkupAnchors() {
59
58
60
59
for ( const a of markupEl . querySelectorAll ( 'a[href^="#"]' ) ) {
61
60
a . addEventListener ( 'click' , ( e ) => {
62
- scrollToAnchor ( e . currentTarget . getAttribute ( 'href' ) ?. substring ( 1 ) , false ) ;
61
+ scrollToAnchor ( e . currentTarget . getAttribute ( 'href' ) ?. substring ( 1 ) ) ;
63
62
} ) ;
64
63
}
65
64
}
66
65
67
- scrollToAnchor ( window . location . hash . substring ( 1 ) , true ) ;
66
+ // scroll to anchor unless browser has already scrolled somewhere during page load
67
+ if ( ! document . querySelector ( ':target' ) ) {
68
+ scrollToAnchor ( window . location . hash ?. substring ( 1 ) ) ;
69
+ }
68
70
}
0 commit comments