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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user