From b0c3ed9f8bd6bd7a8359663a147dcc276b15d2e3 Mon Sep 17 00:00:00 2001 From: Tilman Vatteroth Date: Tue, 6 Jun 2023 22:08:12 +0200 Subject: [PATCH] fix(toc): post toc after rendering instead of during Signed-off-by: Tilman Vatteroth --- .../table-of-contents-markdown-extension.ts | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/frontend/src/extensions/essential-app-extensions/table-of-contents/table-of-contents-markdown-extension.ts b/frontend/src/extensions/essential-app-extensions/table-of-contents/table-of-contents-markdown-extension.ts index f6f1b9404..fa9e96c4e 100644 --- a/frontend/src/extensions/essential-app-extensions/table-of-contents/table-of-contents-markdown-extension.ts +++ b/frontend/src/extensions/essential-app-extensions/table-of-contents/table-of-contents-markdown-extension.ts @@ -16,22 +16,28 @@ import type MarkdownIt from 'markdown-it' export class TableOfContentsMarkdownExtension extends EventMarkdownRendererExtension { public static readonly EVENT_NAME = 'TocChange' private lastAst: TocAst | undefined = undefined + private postAst = false public configureMarkdownIt(markdownIt: MarkdownIt): void { - const eventEmitter = this.eventEmitter - if (eventEmitter !== undefined) { - toc(markdownIt, { - listType: 'ul', - level: [1, 2, 3], - callback: (ast: TocAst): void => { - if (equal(ast, this.lastAst)) { - return - } - this.lastAst = ast - eventEmitter.emit(TableOfContentsMarkdownExtension.EVENT_NAME, ast) - }, - slugify: tocSlugify - }) + toc(markdownIt, { + listType: 'ul', + level: [1, 2, 3], + callback: (ast: TocAst): void => { + if (equal(ast, this.lastAst)) { + return + } + this.lastAst = ast + this.postAst = true + }, + slugify: tocSlugify + }) + } + + public doAfterRendering(): void { + if (!this.postAst) { + return } + this.postAst = false + this.eventEmitter.emit(TableOfContentsMarkdownExtension.EVENT_NAME, this.lastAst) } }