Skip to content

Commit 6881076

Browse files
GiteaBotsilverwind
andauthored
Small refactors in anchors.js (#29947) (#30003)
Backport #29947 by @silverwind Some minor refactors, remove unnecessary `:is` selector and move the `:target` check out of the function. Might as well backport for the rare browser that does not support `:is`. Co-authored-by: silverwind <[email protected]>
1 parent 24c66c5 commit 6881076

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

web_src/js/markup/anchors.js

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

3+
const addPrefix = (str) => `user-content-${str}`;
4+
const removePrefix = (str) => str.replace(/^user-content-/, '');
5+
const hasPrefix = (str) => str.startsWith('user-content-');
6+
37
// 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;
8+
function scrollToAnchor(encodedId) {
9+
if (!encodedId) return;
710
const id = decodeURIComponent(encodedId);
8-
let el = document.getElementById(`user-content-${id}`);
11+
const prefixedId = addPrefix(id);
12+
let el = document.getElementById(prefixedId);
913

1014
// check for matching user-generated `a[name]`
1115
if (!el) {
12-
const nameAnchors = document.getElementsByName(`user-content-${id}`);
16+
const nameAnchors = document.getElementsByName(prefixedId);
1317
if (nameAnchors.length) {
1418
el = nameAnchors[0];
1519
}
1620
}
1721

1822
// compat for links with old 'user-content-' prefixed hashes
19-
if (!el && id.startsWith('user-content-')) {
20-
const el = document.getElementById(id);
21-
if (el) el.scrollIntoView();
23+
if (!el && hasPrefix(id)) {
24+
return document.getElementById(id)?.scrollIntoView();
2225
}
2326

24-
if (el) {
25-
el.scrollIntoView();
26-
}
27+
el?.scrollIntoView();
2728
}
2829

2930
export function initMarkupAnchors() {
@@ -32,11 +33,10 @@ export function initMarkupAnchors() {
3233

3334
for (const markupEl of markupEls) {
3435
// 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`)) {
36-
const originalId = heading.id.replace(/^user-content-/, '');
36+
for (const heading of markupEl.querySelectorAll('h1, h2, h3, h4, h5, h6')) {
3737
const a = document.createElement('a');
3838
a.classList.add('anchor');
39-
a.setAttribute('href', `#${encodeURIComponent(originalId)}`);
39+
a.setAttribute('href', `#${encodeURIComponent(removePrefix(heading.id))}`);
4040
a.innerHTML = svg('octicon-link');
4141
heading.prepend(a);
4242
}
@@ -45,24 +45,26 @@ export function initMarkupAnchors() {
4545
for (const a of markupEl.querySelectorAll('a[href^="#"]')) {
4646
const href = a.getAttribute('href');
4747
if (!href.startsWith('#user-content-')) continue;
48-
const originalId = href.replace(/^#user-content-/, '');
49-
a.setAttribute('href', `#${originalId}`);
48+
a.setAttribute('href', `#${removePrefix(href.substring(1))}`);
5049
}
5150

5251
// add `user-content-` prefix to user-generated `a[name]` link targets
5352
// TODO: this prefix should be added in backend instead
5453
for (const a of markupEl.querySelectorAll('a[name]')) {
5554
const name = a.getAttribute('name');
5655
if (!name) continue;
57-
a.setAttribute('name', `user-content-${a.name}`);
56+
a.setAttribute('name', addPrefix(a.name));
5857
}
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 the 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)