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)