fix(markdown): cache Shikiji init promise to avoid race on parallel callers

The boolean guard flipped synchronously before the async plugin load
resolved, so concurrent callers (e.g. multiple stacked non-markdown
notes mounting on reload) returned early and rendered before
markdown-it-shikiji was attached to the shared md instance. Cache the
in-flight promise instead so all callers await the same resolution.
This commit is contained in:
Julien Calixte
2026-04-30 11:03:29 +02:00
parent 4c7c688688
commit e715fb02d3

View File

@@ -97,16 +97,11 @@ const md = new MarkdownIt({
slugify: (s: string) => slugger.slug(s) slugify: (s: string) => slugger.slug(s)
}) })
let shikijiInitialized = false let shikijiPromise: Promise<void> | null = null
export const useShikiji = async () => { export const useShikiji = (): Promise<void> => {
if (shikijiInitialized) { if (!shikijiPromise) {
return shikijiPromise = Shikiji({
}
shikijiInitialized = true
md.use(
await Shikiji({
themes: { themes: {
light: "vitesse-light", light: "vitesse-light",
dark: "vitesse-black" dark: "vitesse-black"
@@ -126,8 +121,11 @@ export const useShikiji = async () => {
aliases: ["als"] aliases: ["als"]
} as unknown as LanguageRegistration } as unknown as LanguageRegistration
] ]
}).then((plugin) => {
md.use(plugin)
}) })
) }
return shikijiPromise
} }
let mermaidInitialized = false let mermaidInitialized = false