fix: disable unsafe shiki highlighting on old webkit

This commit is contained in:
lucaronin
2026-04-30 06:06:36 +02:00
parent bc56eec35e
commit 2c06c8dcc2
3 changed files with 80 additions and 6 deletions

View File

@@ -0,0 +1,24 @@
import { codeBlockOptions } from '@blocknote/code-block'
import type { CodeBlockOptions } from '@blocknote/core'
function supportsShikiPrecompiledRegexFlags() {
try {
new RegExp('', 'd')
new RegExp('[[]]', 'v')
return true
} catch {
return false
}
}
export function createTolariaCodeBlockOptions(): Partial<CodeBlockOptions> {
const options: Partial<CodeBlockOptions> = {
...codeBlockOptions,
defaultLanguage: 'text',
}
if (supportsShikiPrecompiledRegexFlags()) return options
delete options.createHighlighter
return options
}

View File

@@ -1,12 +1,16 @@
/* eslint-disable react-refresh/only-export-components -- module-level schema, not a component file */
import { createCodeBlockSpec, BlockNoteSchema, defaultInlineContentSpecs } from '@blocknote/core'
import { codeBlockOptions } from '@blocknote/code-block'
import {
createCodeBlockSpec,
BlockNoteSchema,
defaultInlineContentSpecs,
} from '@blocknote/core'
import { createReactBlockSpec, createReactInlineContentSpec } from '@blocknote/react'
import { resolveWikilinkColor as resolveColor } from '../utils/wikilinkColors'
import { resolveEntry } from '../utils/wikilink'
import { MATH_BLOCK_TYPE, MATH_INLINE_TYPE, renderMathToHtml } from '../utils/mathMarkdown'
import { MERMAID_BLOCK_TYPE, mermaidFenceSource } from '../utils/mermaidMarkdown'
import type { VaultEntry } from '../types'
import { createTolariaCodeBlockOptions } from './codeBlockOptions'
import { NoteTitleIcon } from './NoteTitleIcon'
import { MermaidDiagram } from './MermaidDiagram'
@@ -153,10 +157,7 @@ const MermaidBlock = createReactBlockSpec(
},
)
const codeBlock = createCodeBlockSpec({
...codeBlockOptions,
defaultLanguage: 'text',
})
const codeBlock = createCodeBlockSpec(createTolariaCodeBlockOptions())
const mathBlock = MathBlock()
const mermaidBlock = MermaidBlock()

View File

@@ -0,0 +1,49 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
const nativeRegExpDescriptor = Object.getOwnPropertyDescriptor(globalThis, 'RegExp')
const NativeRegExp = RegExp
function setRegExpConstructor(value: RegExpConstructor) {
Object.defineProperty(globalThis, 'RegExp', {
configurable: true,
writable: true,
value,
})
}
function restoreRegExpConstructor() {
if (nativeRegExpDescriptor) {
Object.defineProperty(globalThis, 'RegExp', nativeRegExpDescriptor)
}
}
function installLegacyWebKitRegExp() {
const LegacyWebKitRegExp = function (pattern?: string | RegExp, flags?: string) {
if (flags?.includes('d') || flags?.includes('v')) {
throw new SyntaxError('Invalid flags supplied to RegExp constructor')
}
return new NativeRegExp(pattern, flags)
} as RegExpConstructor
Object.setPrototypeOf(LegacyWebKitRegExp, NativeRegExp)
LegacyWebKitRegExp.prototype = NativeRegExp.prototype
setRegExpConstructor(LegacyWebKitRegExp)
}
afterEach(() => {
restoreRegExpConstructor()
vi.resetModules()
})
describe('editor schema code block highlighting', () => {
it('omits the Shiki highlighter when WebKit lacks precompiled regex flags', async () => {
installLegacyWebKitRegExp()
vi.resetModules()
const { createTolariaCodeBlockOptions } = await import('./codeBlockOptions')
expect(createTolariaCodeBlockOptions()).not.toHaveProperty('createHighlighter')
})
})