From a753cbbbbeb3d7f45117a796e95b1ef096d97aff Mon Sep 17 00:00:00 2001 From: purocean Date: Wed, 9 Mar 2022 23:55:06 +0800 Subject: [PATCH] refactor: anchor click --- src/renderer/plugins/markdown-link.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/renderer/plugins/markdown-link.ts b/src/renderer/plugins/markdown-link.ts index c808e7e7..538ee608 100644 --- a/src/renderer/plugins/markdown-link.ts +++ b/src/renderer/plugins/markdown-link.ts @@ -9,6 +9,16 @@ import { basename, dirname, join, resolve } from '@fe/utils/path' import { switchDoc } from '@fe/services/document' import { getRepo, openExternal, openPath } from '@fe/services/base' +function getElement (id: string) { + id = id.replaceAll('%28', '(').replaceAll('%29', ')') + return document.getElementById(id) || + document.getElementById(decodeURIComponent(id)) || + document.getElementById(encodeURIComponent(id)) || + document.getElementById(id.replace(/^h-/, '')) || + document.getElementById(decodeURIComponent(id.replace(/^h-/, ''))) || + document.getElementById(encodeURIComponent(id.replace(/^h-/, ''))) +} + const handleLink = (link: HTMLAnchorElement, view: HTMLElement) => { const { currentFile } = store.state if (!currentFile) { @@ -66,10 +76,7 @@ const handleLink = (link: HTMLAnchorElement, view: HTMLElement) => { // jump anchor if (hash) { await sleep(50) - const el = document.getElementById(hash) || - document.getElementById(encodeURIComponent(hash)) || - document.getElementById(hash.replace(/^h-/, '')) || - document.getElementById(encodeURIComponent(hash.replace(/^h-/, ''))) + const el = getElement(hash) if (el) { await sleep(0) @@ -85,7 +92,7 @@ const handleLink = (link: HTMLAnchorElement, view: HTMLElement) => { return true } else if (href && href.startsWith('#')) { // for anchor - const el = document.getElementById(href.replace(/^#/, '')) + const el = getElement(href.replace(/^#/, '')) el && scrollIntoView(el) return true }