From 59565d0ebfa458678c7ca4e2bb60c2f89ccc26e2 Mon Sep 17 00:00:00 2001 From: Daniel Carvalho Date: Tue, 20 Feb 2024 23:34:59 +0000 Subject: [PATCH 1/4] Changed file anchor.js to fix the bug in issue #29196 --- web_src/js/markup/anchors.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/web_src/js/markup/anchors.js b/web_src/js/markup/anchors.js index 53dfa2980c0a1..72a5bc4536470 100644 --- a/web_src/js/markup/anchors.js +++ b/web_src/js/markup/anchors.js @@ -30,6 +30,20 @@ export function initMarkupAnchors() { }); heading.prepend(a); } + for (const anchor of document.querySelectorAll('.markup [dir="auto"] > a[href^="#"][rel="nofollow"]')) { + const originalId = anchor.getAttribute('href').replace(/^#user-content-/, ''); + anchor.setAttribute('href', `#${encodeURIComponent(originalId)}`); + const anchorDest = document.getElementsByName(originalId); + if (anchorDest && anchorDest.length === 1) { + anchor.addEventListener('click', () => { + anchorDest[0].scrollIntoView(); + }); + } else { + anchor.addEventListener('click', (e) => { + scrollToAnchor(e.currentTarget.getAttribute('href'), false); + }); + } + } scrollToAnchor(window.location.hash, true); } From 9d86a6318351a3cc5adc95e945f9df5069861b4f Mon Sep 17 00:00:00 2001 From: Daniel Carvalho Date: Thu, 22 Feb 2024 08:17:08 +0000 Subject: [PATCH 2/4] Refactor the code --- web_src/js/markup/anchors.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/web_src/js/markup/anchors.js b/web_src/js/markup/anchors.js index 72a5bc4536470..887c432414cd7 100644 --- a/web_src/js/markup/anchors.js +++ b/web_src/js/markup/anchors.js @@ -30,11 +30,15 @@ export function initMarkupAnchors() { }); heading.prepend(a); } - for (const anchor of document.querySelectorAll('.markup [dir="auto"] > a[href^="#"][rel="nofollow"]')) { - const originalId = anchor.getAttribute('href').replace(/^#user-content-/, ''); + for (const anchor of document.querySelectorAll('.markup a[href^="#"]')) { + const href = anchor.getAttribute('href'); + if (!href.startsWith('#user-content-')) { + continue; + } + const originalId = href.replace(/^#user-content-/, ''); anchor.setAttribute('href', `#${encodeURIComponent(originalId)}`); const anchorDest = document.getElementsByName(originalId); - if (anchorDest && anchorDest.length === 1) { + if (anchorDest?.length === 1) { anchor.addEventListener('click', () => { anchorDest[0].scrollIntoView(); }); From 34f7e91be4ed1356159b9b7f228ac56c8bd364a2 Mon Sep 17 00:00:00 2001 From: silverwind Date: Tue, 5 Mar 2024 20:31:22 +0100 Subject: [PATCH 3/4] add comments, style tweaks --- web_src/js/markup/anchors.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/web_src/js/markup/anchors.js b/web_src/js/markup/anchors.js index 887c432414cd7..04c6d42ea0503 100644 --- a/web_src/js/markup/anchors.js +++ b/web_src/js/markup/anchors.js @@ -2,6 +2,7 @@ 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) { // abort if the browser has already scrolled to another anchor during page load if (initial && document.querySelector(':target')) return; @@ -19,6 +20,7 @@ function scrollToAnchor(hash, initial) { 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'); @@ -30,20 +32,20 @@ export function initMarkupAnchors() { }); heading.prepend(a); } - for (const anchor of document.querySelectorAll('.markup a[href^="#"]')) { - const href = anchor.getAttribute('href'); - if (!href.startsWith('#user-content-')) { - continue; - } + + // 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-/, ''); - anchor.setAttribute('href', `#${encodeURIComponent(originalId)}`); + a.setAttribute('href', `#${encodeURIComponent(originalId)}`); const anchorDest = document.getElementsByName(originalId); - if (anchorDest?.length === 1) { - anchor.addEventListener('click', () => { + if (anchorDest.length === 1) { + a.addEventListener('click', () => { anchorDest[0].scrollIntoView(); }); } else { - anchor.addEventListener('click', (e) => { + a.addEventListener('click', (e) => { scrollToAnchor(e.currentTarget.getAttribute('href'), false); }); } From 02d3ae85bf886125ea8c5f3748b77edd8bc21329 Mon Sep 17 00:00:00 2001 From: Daniel Carvalho Date: Thu, 7 Mar 2024 22:21:59 +0000 Subject: [PATCH 4/4] Implemented the recommended changes in the Pull Request --- web_src/js/markup/anchors.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/web_src/js/markup/anchors.js b/web_src/js/markup/anchors.js index 04c6d42ea0503..03934ea215fab 100644 --- a/web_src/js/markup/anchors.js +++ b/web_src/js/markup/anchors.js @@ -39,12 +39,7 @@ export function initMarkupAnchors() { if (!href.startsWith('#user-content-')) continue; const originalId = href.replace(/^#user-content-/, ''); a.setAttribute('href', `#${encodeURIComponent(originalId)}`); - const anchorDest = document.getElementsByName(originalId); - if (anchorDest.length === 1) { - a.addEventListener('click', () => { - anchorDest[0].scrollIntoView(); - }); - } else { + if (document.getElementsByName(originalId).length !== 1) { a.addEventListener('click', (e) => { scrollToAnchor(e.currentTarget.getAttribute('href'), false); });