2026-04-30 06:06:36 +02:00
|
|
|
import { codeBlockOptions } from '@blocknote/code-block'
|
|
|
|
|
import type { CodeBlockOptions } from '@blocknote/core'
|
|
|
|
|
|
2026-05-03 12:28:00 +02:00
|
|
|
const LIGHT_CODE_THEME = 'github-light'
|
|
|
|
|
const DARK_CODE_THEME = 'github-dark'
|
|
|
|
|
|
|
|
|
|
type TolariaCodeHighlighter = Awaited<ReturnType<NonNullable<typeof codeBlockOptions.createHighlighter>>>
|
|
|
|
|
|
2026-04-30 06:06:36 +02:00
|
|
|
function supportsShikiPrecompiledRegexFlags() {
|
|
|
|
|
try {
|
|
|
|
|
new RegExp('', 'd')
|
|
|
|
|
new RegExp('[[]]', 'v')
|
|
|
|
|
return true
|
|
|
|
|
} catch {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-03 12:28:00 +02:00
|
|
|
function currentCodeBlockTheme() {
|
|
|
|
|
if (typeof document === 'undefined') return LIGHT_CODE_THEME
|
|
|
|
|
|
|
|
|
|
const root = document.documentElement
|
|
|
|
|
return root.classList.contains('dark') || root.dataset.theme === 'dark'
|
|
|
|
|
? DARK_CODE_THEME
|
|
|
|
|
: LIGHT_CODE_THEME
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function prioritizeTheme(themes: string[], theme: string) {
|
|
|
|
|
return [theme, ...themes.filter((candidate) => candidate !== theme)]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function createTolariaCodeHighlighter(): Promise<TolariaCodeHighlighter> {
|
|
|
|
|
const highlighter = await codeBlockOptions.createHighlighter()
|
|
|
|
|
return {
|
|
|
|
|
...highlighter,
|
|
|
|
|
getLoadedThemes: () => prioritizeTheme(highlighter.getLoadedThemes(), currentCodeBlockTheme()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-30 06:06:36 +02:00
|
|
|
export function createTolariaCodeBlockOptions(): Partial<CodeBlockOptions> {
|
|
|
|
|
const options: Partial<CodeBlockOptions> = {
|
|
|
|
|
...codeBlockOptions,
|
2026-05-03 12:28:00 +02:00
|
|
|
createHighlighter: createTolariaCodeHighlighter,
|
2026-04-30 06:06:36 +02:00
|
|
|
defaultLanguage: 'text',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (supportsShikiPrecompiledRegexFlags()) return options
|
|
|
|
|
|
|
|
|
|
delete options.createHighlighter
|
|
|
|
|
return options
|
|
|
|
|
}
|