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 { export class TableOfContentsMarkdownExtension extends EventMarkdownRendererExtension {
public static readonly EVENT_NAME = 'TocChange' public static readonly EVENT_NAME = 'TocChange'
private lastAst: TocAst | undefined = undefined private lastAst: TocAst | undefined = undefined
private postAst = false
public configureMarkdownIt(markdownIt: MarkdownIt): void { public configureMarkdownIt(markdownIt: MarkdownIt): void {
const eventEmitter = this.eventEmitter toc(markdownIt, {
if (eventEmitter !== undefined) { listType: 'ul',
toc(markdownIt, { level: [1, 2, 3],
listType: 'ul', callback: (ast: TocAst): void => {
level: [1, 2, 3], if (equal(ast, this.lastAst)) {
callback: (ast: TocAst): void => { return
if (equal(ast, this.lastAst)) { }
return this.lastAst = ast
} this.postAst = true
this.lastAst = ast },
eventEmitter.emit(TableOfContentsMarkdownExtension.EVENT_NAME, ast) slugify: tocSlugify
}, })
slugify: tocSlugify }
})
public doAfterRendering(): void {
if (!this.postAst) {
return
} }
this.postAst = false
this.eventEmitter.emit(TableOfContentsMarkdownExtension.EVENT_NAME, this.lastAst)
} }
} }