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)