import { codeBlockOptions } from '@blocknote/code-block' import type { CodeBlockOptions } from '@blocknote/core' const LIGHT_CODE_THEME = 'github-light' const DARK_CODE_THEME = 'github-dark' type TolariaCodeHighlighter = Awaited>> function supportsShikiPrecompiledRegexFlags() { try { new RegExp('', 'd') new RegExp('[[]]', 'v') return true } catch { return false } } 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 { const highlighter = await codeBlockOptions.createHighlighter() return { ...highlighter, getLoadedThemes: () => prioritizeTheme(highlighter.getLoadedThemes(), currentCodeBlockTheme()), } } export function createTolariaCodeBlockOptions(): Partial { const options: Partial = { ...codeBlockOptions, createHighlighter: createTolariaCodeHighlighter, defaultLanguage: 'text', } if (supportsShikiPrecompiledRegexFlags()) return options delete options.createHighlighter return options }