From 2ce76455a47b94e1800cc58e1bedfd74aef146d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A6=BE=E5=87=A0=E6=B5=B7?= Date: Fri, 9 Oct 2020 17:52:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=94=9A=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/view/article/article.component.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/app/view/article/article.component.ts b/src/app/view/article/article.component.ts index a1d4316..0cf5810 100644 --- a/src/app/view/article/article.component.ts +++ b/src/app/view/article/article.component.ts @@ -58,7 +58,7 @@ export class ArticleComponent implements OnInit { parseMd(md: string) { const option: IPreviewOptions = { - anchor: 2, + anchor: 1, hljs: { lineNumber: true }, @@ -66,11 +66,22 @@ export class ArticleComponent implements OnInit { autoSpace: true, fixTermTypo: true, chinesePunct: true, + linkBase: 'location.href', toc: true, - linkBase: location.href + mark: true + }, + after: () => { + // 处理锚点 + const tocList: HTMLCollection = this.divElement.nativeElement + .getElementsByClassName('vditor-toc')[0] + .getElementsByTagName(`a`); + for (let i = 0; i < tocList.length; i++) { + const linkValue = tocList.item(i).getAttribute('href'); + tocList.item(i).setAttribute('href', window.location.pathname + linkValue); + } } }; - VditorPreview.preview(this.divElement.nativeElement, md); + VditorPreview.preview(this.divElement.nativeElement, md, option); } toArticle(id: number) {