From 681ed0520f3fdeff3cf7b52b22bf3385617c8f32 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 20 Mar 2024 15:29:10 +0800 Subject: [PATCH 1/5] Fix wiki toc link borken for non-English languages --- web_src/js/markup/anchors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_src/js/markup/anchors.js b/web_src/js/markup/anchors.js index 6cf83eb428b85..a2a2a46b2220c 100644 --- a/web_src/js/markup/anchors.js +++ b/web_src/js/markup/anchors.js @@ -38,7 +38,7 @@ export function initMarkupAnchors() { const href = a.getAttribute('href'); if (!href.startsWith('#user-content-')) continue; const originalId = href.replace(/^#user-content-/, ''); - a.setAttribute('href', `#${encodeURIComponent(originalId)}`); + a.setAttribute('href', `#${originalId}`); if (a.closest('.markup').querySelectorAll(`a[name="${originalId}"]`).length !== 1) { a.addEventListener('click', (e) => { scrollToAnchor(e.currentTarget.getAttribute('href'), false); From 3fb120de818311792128a8ae2ece0c87c1b4aa27 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 20 Mar 2024 18:35:57 +0800 Subject: [PATCH 2/5] Use decodeURIComponent but not encodeURIComponent --- web_src/js/markup/anchors.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/web_src/js/markup/anchors.js b/web_src/js/markup/anchors.js index a2a2a46b2220c..2214cf3a6756b 100644 --- a/web_src/js/markup/anchors.js +++ b/web_src/js/markup/anchors.js @@ -25,7 +25,7 @@ export function initMarkupAnchors() { const originalId = heading.id.replace(/^user-content-/, ''); const a = document.createElement('a'); a.classList.add('anchor'); - a.setAttribute('href', `#${encodeURIComponent(originalId)}`); + a.setAttribute('href', `#${decodeURIComponent(originalId)}`); a.innerHTML = svg('octicon-link'); a.addEventListener('click', (e) => { scrollToAnchor(e.currentTarget.getAttribute('href'), false); @@ -38,8 +38,8 @@ export function initMarkupAnchors() { const href = a.getAttribute('href'); if (!href.startsWith('#user-content-')) continue; const originalId = href.replace(/^#user-content-/, ''); - a.setAttribute('href', `#${originalId}`); - if (a.closest('.markup').querySelectorAll(`a[name="${originalId}"]`).length !== 1) { + a.setAttribute('href', `#${decodeURIComponent(originalId)}`); + if (a.closest('.markup').querySelectorAll(`a[name="${decodeURIComponent(originalId)}"]`).length !== 1) { a.addEventListener('click', (e) => { scrollToAnchor(e.currentTarget.getAttribute('href'), false); }); From ce8a023bce4a6c338478c790f52998c202b1c1ae Mon Sep 17 00:00:00 2001 From: silverwind Date: Wed, 20 Mar 2024 22:17:39 +0100 Subject: [PATCH 3/5] rewrite the function to single click listener --- web_src/js/markup/anchors.js | 83 ++++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 33 deletions(-) diff --git a/web_src/js/markup/anchors.js b/web_src/js/markup/anchors.js index 2214cf3a6756b..10d8a534e74cc 100644 --- a/web_src/js/markup/anchors.js +++ b/web_src/js/markup/anchors.js @@ -1,50 +1,67 @@ import {svg} from '../svg.js'; -const headingSelector = '.markup h1, .markup h2, .markup h3, .markup h4, .markup h5, .markup h6'; - // scroll to anchor while respecting the `user-content` prefix that exists on the target -function scrollToAnchor(hash, initial) { +function scrollToAnchor(encodedId, initial) { // abort if the browser has already scrolled to another anchor during page load - if (initial && document.querySelector(':target')) return; - if (hash?.length <= 1) return; - const id = decodeURIComponent(hash.substring(1)); - const el = document.getElementById(`user-content-${id}`); - if (el) { - el.scrollIntoView(); - } else if (id.startsWith('user-content-')) { // compat for links with old 'user-content-' prefixed hashes + if (!encodedId || (initial && document.querySelector(':target'))) return; + const id = decodeURIComponent(encodedId); + let el = document.getElementById(`user-content-${id}`); + + // check for matching user-generated `a[name]` + if (!el) { + const nameAnchors = document.getElementsByName(`user-content-${id}`); + if (nameAnchors.length) { + el = nameAnchors[0]; + } + } + + // compat for links with old 'user-content-' prefixed hashes + if (!el && id.startsWith('user-content-')) { const el = document.getElementById(id); if (el) el.scrollIntoView(); } + + if (el) { + el.scrollIntoView(); + } } export function initMarkupAnchors() { - if (!document.querySelector('.markup')) return; - - // create link icons for markup headings, the resulting link href will remove `user-content-` - for (const heading of document.querySelectorAll(headingSelector)) { - const originalId = heading.id.replace(/^user-content-/, ''); - const a = document.createElement('a'); - a.classList.add('anchor'); - a.setAttribute('href', `#${decodeURIComponent(originalId)}`); - a.innerHTML = svg('octicon-link'); - a.addEventListener('click', (e) => { - scrollToAnchor(e.currentTarget.getAttribute('href'), false); - }); - heading.prepend(a); - } + const markupEls = document.querySelectorAll('.markup'); + if (!markupEls) return; + + for (const markupEl of markupEls) { + // create link icons for markup headings, the resulting link href will remove `user-content-` + for (const heading of markupEl.querySelectorAll(`:is(h1, h2, h3, h4, h5, h6`)) { + const originalId = heading.id.replace(/^user-content-/, ''); + const a = document.createElement('a'); + a.classList.add('anchor'); + a.setAttribute('href', `#${encodeURIComponent(originalId)}`); + a.innerHTML = svg('octicon-link'); + heading.prepend(a); + } + + // remove `user-content-` prefix from links so they don't show in url bar when clicked + for (const a of markupEl.querySelectorAll('a[href^="#"]')) { + const href = a.getAttribute('href'); + if (!href.startsWith('#user-content-')) continue; + const originalId = href.replace(/^#user-content-/, ''); + a.setAttribute('href', `#${originalId}`); + } + + // add `user-content-` prefix to user-generated `a[name]` link targets + for (const a of markupEl.querySelectorAll('a[name]')) { + const name = a.getAttribute('name'); + if (!name) continue; + a.setAttribute('name', `user-content-${a.name}`); + } - // handle user-defined `name` anchors like `[Link](#link)` linking to `Link` - for (const a of document.querySelectorAll('.markup a[href^="#"]')) { - const href = a.getAttribute('href'); - if (!href.startsWith('#user-content-')) continue; - const originalId = href.replace(/^#user-content-/, ''); - a.setAttribute('href', `#${decodeURIComponent(originalId)}`); - if (a.closest('.markup').querySelectorAll(`a[name="${decodeURIComponent(originalId)}"]`).length !== 1) { + for (const a of markupEl.querySelectorAll('a[href^="#"]')) { a.addEventListener('click', (e) => { - scrollToAnchor(e.currentTarget.getAttribute('href'), false); + scrollToAnchor(e.currentTarget.getAttribute('href')?.substring(1), false); }); } } - scrollToAnchor(window.location.hash, true); + scrollToAnchor(window.location.hash.substring(1), true); } From 6689ef6b7429127584dfc98db523fb73ca97fbd2 Mon Sep 17 00:00:00 2001 From: silverwind Date: Wed, 20 Mar 2024 22:21:42 +0100 Subject: [PATCH 4/5] fix early exit --- web_src/js/markup/anchors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_src/js/markup/anchors.js b/web_src/js/markup/anchors.js index 10d8a534e74cc..78e16ce2a49a9 100644 --- a/web_src/js/markup/anchors.js +++ b/web_src/js/markup/anchors.js @@ -28,7 +28,7 @@ function scrollToAnchor(encodedId, initial) { export function initMarkupAnchors() { const markupEls = document.querySelectorAll('.markup'); - if (!markupEls) return; + if (!markupEls.length) return; for (const markupEl of markupEls) { // create link icons for markup headings, the resulting link href will remove `user-content-` From cb2e65d9afd8d3d7bf414ec5758518bc9d96cb1a Mon Sep 17 00:00:00 2001 From: silverwind Date: Wed, 20 Mar 2024 23:27:20 +0100 Subject: [PATCH 5/5] add TODO --- web_src/js/markup/anchors.js | 1 + 1 file changed, 1 insertion(+) diff --git a/web_src/js/markup/anchors.js b/web_src/js/markup/anchors.js index 78e16ce2a49a9..dac877fd99aff 100644 --- a/web_src/js/markup/anchors.js +++ b/web_src/js/markup/anchors.js @@ -50,6 +50,7 @@ export function initMarkupAnchors() { } // add `user-content-` prefix to user-generated `a[name]` link targets + // TODO: this prefix should be added in backend instead for (const a of markupEl.querySelectorAll('a[name]')) { const name = a.getAttribute('name'); if (!name) continue;