fix(markdown)!: return empty string for non-string content in preprocessors

Related to a9c5201485 - when switching views during active preview run,
the markdown preprocessors could receive non-string content (e.g., frozen
arrays from immer). Returning the original value caused ReactMarkdown to
fail with "Cannot assign to read only property" error.

Now both preprocessLaTeX and preprocessThinkTag return '' for non-string
input, preventing runtime errors during view switches.
This commit is contained in:
yyh 2026-01-27 01:10:00 +08:00
parent a0188bd9b5
commit b7f1eb9b7b
No known key found for this signature in database

View File

@ -8,7 +8,7 @@ import { ALLOW_UNSAFE_DATA_SCHEME } from '@/config'
export const preprocessLaTeX = (content: string) => { export const preprocessLaTeX = (content: string) => {
if (typeof content !== 'string') if (typeof content !== 'string')
return content return ''
const codeBlockRegex = /```[\s\S]*?```/g const codeBlockRegex = /```[\s\S]*?```/g
const codeBlocks = content.match(codeBlockRegex) || [] const codeBlocks = content.match(codeBlockRegex) || []
@ -32,6 +32,9 @@ export const preprocessLaTeX = (content: string) => {
} }
export const preprocessThinkTag = (content: string) => { export const preprocessThinkTag = (content: string) => {
if (typeof content !== 'string')
return ''
const thinkOpenTagRegex = /(<think>\s*)+/g const thinkOpenTagRegex = /(<think>\s*)+/g
const thinkCloseTagRegex = /(\s*<\/think>)+/g const thinkCloseTagRegex = /(\s*<\/think>)+/g
return flow([ return flow([