From f36ea641726705d00f9b4b1b62f50831afbb7e6f 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.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/plugins/links.ts b/src/plugins/links.ts index 7664091331a..3375054a850 100644 --- a/src/plugins/links.ts +++ b/src/plugins/links.ts @@ -188,8 +188,14 @@ export function linkClicking(openLink: (href: string) => void = (href) => { event.preventDefault() if (isLinkToSelfWithHash(linkEl.href)) { - // Open anchor links directly - location.href = linkEl.href + // 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 directly on Ctrl/Cmd + left click openLink(linkEl.href)