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