fix(toc): post toc after rendering instead of during

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2023-06-06 22:08:12 +02:00
parent b04b5cc3e1
commit b0c3ed9f8b

View file

@ -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)
}
}