2023-12-20 19:54:40 +01:00
|
|
|
import { type Parser, createParser } from 'prosemirror-highlight/shikiji'
|
|
|
|
import type { BuiltinLanguage } from 'shikiji/langs'
|
|
|
|
|
|
|
|
let parser: Parser | undefined
|
|
|
|
|
|
|
|
export const shikijiParser: Parser = (options) => {
|
|
|
|
const lang = options.language ?? 'text'
|
|
|
|
|
|
|
|
// Register the language if it's not yet registered
|
2024-01-01 17:46:37 +01:00
|
|
|
const { highlighter, promise } = useHighlighter(lang as BuiltinLanguage)
|
2023-12-20 19:54:40 +01:00
|
|
|
|
2024-01-01 17:46:37 +01:00
|
|
|
// If the highlighter or the language is not available, return a promise that
|
|
|
|
// will resolve when it's ready. When the promise resolves, the editor will
|
|
|
|
// re-parse the code block.
|
2023-12-20 19:54:40 +01:00
|
|
|
if (!highlighter)
|
2024-01-01 17:46:37 +01:00
|
|
|
return promise ?? []
|
2023-12-20 19:54:40 +01:00
|
|
|
|
|
|
|
if (!parser)
|
|
|
|
parser = createParser(highlighter)
|
|
|
|
|
|
|
|
return parser(options)
|
|
|
|
}
|