From 5803fdaa56d1c9d9148151ded2f025af70577849 Mon Sep 17 00:00:00 2001 From: purocean Date: Mon, 6 Sep 2021 15:20:43 +0800 Subject: [PATCH] =?UTF-8?q?upd:=20=E7=A7=BB=E9=99=A4=E6=A0=87=E9=A2=98?= =?UTF-8?q?=E9=93=BE=E6=8E=A5=E4=B8=AD=E7=9A=84=20`h-`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/renderer/plugins/markdown-link.ts | 9 ++++++--- src/renderer/plugins/markdown-toc.ts | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/renderer/plugins/markdown-link.ts b/src/renderer/plugins/markdown-link.ts index bc781ab8..deb7cdd0 100644 --- a/src/renderer/plugins/markdown-link.ts +++ b/src/renderer/plugins/markdown-link.ts @@ -50,14 +50,17 @@ const handleLink = (link: HTMLAnchorElement) => { if (hash) { await sleep(50) const el = document.getElementById(hash) || - document.getElementById(encodeURIComponent(hash)) + document.getElementById(encodeURIComponent(hash)) || + document.getElementById(hash.replace(/^h-/, '')) || + document.getElementById(encodeURIComponent(hash.replace(/^h-/, ''))) if (el) { + el.scrollIntoView() + // 如果是标题的话,也顺便将编辑器滚动到可视区域 - if (hash.startsWith('h-')) { + if (['H1', 'H2', 'H3', 'H4', 'H5', 'H6'].includes(el.tagName)) { el.click() } - el.scrollIntoView() } } }) diff --git a/src/renderer/plugins/markdown-toc.ts b/src/renderer/plugins/markdown-toc.ts index 0278b61a..4283bbbb 100644 --- a/src/renderer/plugins/markdown-toc.ts +++ b/src/renderer/plugins/markdown-toc.ts @@ -7,7 +7,7 @@ import { Plugin } from '@fe/context/plugin' import { injectLineNumbers } from './markdown-source-line' -const slugify = (s: string) => 'h-' + encodeURIComponent(String(s).trim().toLowerCase().replace(/\s+/g, '-')) +const slugify = (s: string) => encodeURIComponent(String(s).trim().toLowerCase().replace(/\s+/g, '-')) const defaults = { level: [2, 3],