Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix user-defined markup links targets #29305

Merged
merged 11 commits into from
Mar 8, 2024
14 changes: 14 additions & 0 deletions web_src/js/markup/anchors.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ export function initMarkupAnchors() {
});
heading.prepend(a);
}
for (const anchor of document.querySelectorAll('.markup [dir="auto"] > a[href^="#"][rel="nofollow"]')) {
DanielMatiasCarvalho marked this conversation as resolved.
Show resolved Hide resolved
const originalId = anchor.getAttribute('href').replace(/^#user-content-/, '');
DanielMatiasCarvalho marked this conversation as resolved.
Show resolved Hide resolved
anchor.setAttribute('href', `#${encodeURIComponent(originalId)}`);
const anchorDest = document.getElementsByName(originalId);
if (anchorDest && anchorDest.length === 1) {
DanielMatiasCarvalho marked this conversation as resolved.
Show resolved Hide resolved
anchor.addEventListener('click', () => {
anchorDest[0].scrollIntoView();
});
} else {
anchor.addEventListener('click', (e) => {
scrollToAnchor(e.currentTarget.getAttribute('href'), false);
});
}
}

scrollToAnchor(window.location.hash, true);
}
Loading