From b873ab4a394c27c963e22a66b53b983e22f1ef19 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 | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/plugins/links.ts b/src/plugins/links.ts index 1a8b5a11d59..ff9166e3ffd 100644 --- a/src/plugins/links.ts +++ b/src/plugins/links.ts @@ -191,8 +191,17 @@ export function linkClicking( 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)