2024-02-15 10:43:09 +03:00
|
|
|
import type { BuiltinLanguage } from 'shiki'
|
2024-09-30 11:11:56 +03:00
|
|
|
import { createParser, type Parser } from 'prosemirror-highlight/shiki'
|
2023-12-20 21:54:40 +03:00
|
|
|
|
|
|
|
let parser: Parser | undefined
|
|
|
|
|
2024-02-15 10:43:09 +03:00
|
|
|
export const shikiParser: Parser = (options) => {
|
2023-12-20 21:54:40 +03:00
|
|
|
const lang = options.language ?? 'text'
|
|
|
|
|
|
|
|
// Register the language if it's not yet registered
|
2024-01-01 19:46:37 +03:00
|
|
|
const { highlighter, promise } = useHighlighter(lang as BuiltinLanguage)
|
2023-12-20 21:54:40 +03:00
|
|
|
|
2024-01-01 19:46:37 +03: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 21:54:40 +03:00
|
|
|
if (!highlighter)
|
2024-01-01 19:46:37 +03:00
|
|
|
return promise ?? []
|
2023-12-20 21:54:40 +03:00
|
|
|
|
|
|
|
if (!parser)
|
|
|
|
parser = createParser(highlighter)
|
|
|
|
|
|
|
|
return parser(options)
|
|
|
|
}
|