From e715fb02d391e1950807e9a98ba379b47b435245 Mon Sep 17 00:00:00 2001 From: Julien Calixte Date: Thu, 30 Apr 2026 11:03:29 +0200 Subject: [PATCH] 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. --- src/hooks/useMarkdown.hook.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/hooks/useMarkdown.hook.ts b/src/hooks/useMarkdown.hook.ts index b577ea9..41a6d07 100644 --- a/src/hooks/useMarkdown.hook.ts +++ b/src/hooks/useMarkdown.hook.ts @@ -97,16 +97,11 @@ const md = new MarkdownIt({ slugify: (s: string) => slugger.slug(s) }) -let shikijiInitialized = false +let shikijiPromise: Promise | null = null -export const useShikiji = async () => { - if (shikijiInitialized) { - return - } - - shikijiInitialized = true - md.use( - await Shikiji({ +export const useShikiji = (): Promise => { + 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