loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Fix and rewrite markup anchor processing (#29931)

Fix #29877

---------

Co-authored-by: silverwind <me@silverwind.io>
(cherry picked from commit 76ec5410510f09b3ea2bfd2602fcb8f3251087b6)

authored by

Lunny Xiao
silverwind
and committed by
Earl Warren
e8f3efe1 dda010cd

+50 -32
+50 -32
web_src/js/markup/anchors.js
··· 1 1 import {svg} from '../svg.js'; 2 2 3 - const headingSelector = '.markup h1, .markup h2, .markup h3, .markup h4, .markup h5, .markup h6'; 4 - 5 3 // scroll to anchor while respecting the `user-content` prefix that exists on the target 6 - function scrollToAnchor(hash, initial) { 4 + function scrollToAnchor(encodedId, initial) { 7 5 // abort if the browser has already scrolled to another anchor during page load 8 - if (initial && document.querySelector(':target')) return; 9 - if (hash?.length <= 1) return; 10 - const id = decodeURIComponent(hash.substring(1)); 11 - const el = document.getElementById(`user-content-${id}`); 6 + if (!encodedId || (initial && document.querySelector(':target'))) return; 7 + const id = decodeURIComponent(encodedId); 8 + let el = document.getElementById(`user-content-${id}`); 9 + 10 + // check for matching user-generated `a[name]` 11 + if (!el) { 12 + const nameAnchors = document.getElementsByName(`user-content-${id}`); 13 + if (nameAnchors.length) { 14 + el = nameAnchors[0]; 15 + } 16 + } 17 + 18 + // 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(); 22 + } 23 + 12 24 if (el) { 13 25 el.scrollIntoView(); 14 - } else if (id.startsWith('user-content-')) { // compat for links with old 'user-content-' prefixed hashes 15 - const el = document.getElementById(id); 16 - if (el) el.scrollIntoView(); 17 26 } 18 27 } 19 28 20 29 export function initMarkupAnchors() { 21 - if (!document.querySelector('.markup')) return; 30 + const markupEls = document.querySelectorAll('.markup'); 31 + if (!markupEls.length) return; 22 32 23 - // create link icons for markup headings, the resulting link href will remove `user-content-` 24 - for (const heading of document.querySelectorAll(headingSelector)) { 25 - const originalId = heading.id.replace(/^user-content-/, ''); 26 - const a = document.createElement('a'); 27 - a.classList.add('anchor'); 28 - a.setAttribute('href', `#${encodeURIComponent(originalId)}`); 29 - a.innerHTML = svg('octicon-link'); 30 - a.addEventListener('click', (e) => { 31 - scrollToAnchor(e.currentTarget.getAttribute('href'), false); 32 - }); 33 - heading.prepend(a); 34 - } 33 + for (const markupEl of markupEls) { 34 + // 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-/, ''); 37 + const a = document.createElement('a'); 38 + a.classList.add('anchor'); 39 + a.setAttribute('href', `#${encodeURIComponent(originalId)}`); 40 + a.innerHTML = svg('octicon-link'); 41 + heading.prepend(a); 42 + } 43 + 44 + // remove `user-content-` prefix from links so they don't show in url bar when clicked 45 + for (const a of markupEl.querySelectorAll('a[href^="#"]')) { 46 + const href = a.getAttribute('href'); 47 + if (!href.startsWith('#user-content-')) continue; 48 + const originalId = href.replace(/^#user-content-/, ''); 49 + a.setAttribute('href', `#${originalId}`); 50 + } 51 + 52 + // add `user-content-` prefix to user-generated `a[name]` link targets 53 + // TODO: this prefix should be added in backend instead 54 + for (const a of markupEl.querySelectorAll('a[name]')) { 55 + const name = a.getAttribute('name'); 56 + if (!name) continue; 57 + a.setAttribute('name', `user-content-${a.name}`); 58 + } 35 59 36 - // handle user-defined `name` anchors like `[Link](#link)` linking to `<a name="link"></a>Link` 37 - for (const a of document.querySelectorAll('.markup a[href^="#"]')) { 38 - const href = a.getAttribute('href'); 39 - if (!href.startsWith('#user-content-')) continue; 40 - const originalId = href.replace(/^#user-content-/, ''); 41 - a.setAttribute('href', `#${encodeURIComponent(originalId)}`); 42 - if (a.closest('.markup').querySelectorAll(`a[name="${originalId}"]`).length !== 1) { 60 + for (const a of markupEl.querySelectorAll('a[href^="#"]')) { 43 61 a.addEventListener('click', (e) => { 44 - scrollToAnchor(e.currentTarget.getAttribute('href'), false); 62 + scrollToAnchor(e.currentTarget.getAttribute('href')?.substring(1), false); 45 63 }); 46 64 } 47 65 } 48 66 49 - scrollToAnchor(window.location.hash, true); 67 + scrollToAnchor(window.location.hash.substring(1), true); 50 68 }