Skip to content

Commit 2e6a1be

Browse files
committed
Small refactors in anchors.js
1 parent 444460e commit 2e6a1be

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

web_src/js/markup/anchors.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import {svg} from '../svg.js';
22

33
// 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;
76
const id = decodeURIComponent(encodedId);
87
let el = document.getElementById(`user-content-${id}`);
98

@@ -32,7 +31,7 @@ export function initMarkupAnchors() {
3231

3332
for (const markupEl of markupEls) {
3433
// 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')) {
3635
const originalId = heading.id.replace(/^user-content-/, '');
3736
const a = document.createElement('a');
3837
a.classList.add('anchor');
@@ -59,10 +58,13 @@ export function initMarkupAnchors() {
5958

6059
for (const a of markupEl.querySelectorAll('a[href^="#"]')) {
6160
a.addEventListener('click', (e) => {
62-
scrollToAnchor(e.currentTarget.getAttribute('href')?.substring(1), false);
61+
scrollToAnchor(e.currentTarget.getAttribute('href')?.substring(1));
6362
});
6463
}
6564
}
6665

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+
}
6870
}

0 commit comments

Comments
 (0)