Skip to content

Commit e6d2846

Browse files
committed
Fix markdown anchor re-clicking
The hashchange event did not fire on re-click of a active anchor. Instead, use the click event. Fixes: go-gitea#21680
1 parent fc7a2d5 commit e6d2846

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
@@ -2,10 +2,10 @@ import {svg} from '../svg.js';
22

33
const headingSelector = '.markup h1, .markup h2, .markup h3, .markup h4, .markup h5, .markup h6';
44

5-
function scrollToAnchor() {
6-
if (document.querySelector(':target')) return;
7-
if (!window.location.hash || window.location.hash.length <= 1) return;
8-
const id = decodeURIComponent(window.location.hash.substring(1));
5+
function scrollToAnchor(hash) {
6+
if (document.querySelector(':target')) return; // something else on the page matches
7+
if (hash?.length <= 1) return;
8+
const id = decodeURIComponent(hash.substring(1));
99
const el = document.getElementById(`user-content-${id}`);
1010
if (el) {
1111
el.scrollIntoView();
@@ -24,9 +24,11 @@ export function initMarkupAnchors() {
2424
a.classList.add('anchor');
2525
a.setAttribute('href', `#${encodeURIComponent(originalId)}`);
2626
a.innerHTML = svg('octicon-link');
27+
a.addEventListener('click', (e) => {
28+
scrollToAnchor(e.currentTarget.getAttribute('href'));
29+
});
2730
heading.prepend(a);
2831
}
2932

30-
scrollToAnchor();
31-
window.addEventListener('hashchange', scrollToAnchor);
33+
scrollToAnchor(window.location.hash);
3234
}

0 commit comments

Comments
 (0)