From 80456ca52163aa57b31311234a9b53e306bba128 Mon Sep 17 00:00:00 2001 From: Jonas Date: Sun, 12 Jul 2026 18:11:03 +0200 Subject: [PATCH] fix(links): scroll to anchor links directly Setting location.href doesn't work reliably as the window may hold several editors with conflicting IDs (e.g. reader and editor in Collectives). Instead, scroll to the heading in `view.dom`. Fixes: #8804 Signed-off-by: Jonas --- src/plugins/links.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/plugins/links.js b/src/plugins/links.js index 16dd1a55593..5b750e4f8f7 100644 --- a/src/plugins/links.js +++ b/src/plugins/links.js @@ -177,8 +177,17 @@ export function linkClicking() { event.preventDefault() if (isLinkToSelfWithHash(linkEl.attributes.href?.value)) { - // Open anchor links directly - location.href = linkEl.attributes.href.value + // Directly scroll to anchor links + const url = new URL(linkEl.href, window.location.href) + const hash = url.hash + if (hash) { + const target = view.dom.querySelector(hash) + target?.scrollIntoView({ + block: 'start', + behavior: 'smooth', + }) + } + window.history.replaceState({}, '', url.href) } else if (event.ctrlKey || event.metaKey) { // Open link in new tab on Ctrl/Cmd + left click window.open(linkEl.href, '_blank')