Skip to content

Commit 0255b48

Browse files
committed
fix: catch shiki magic move error while language or theme are being updated
1 parent 810aa02 commit 0255b48

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

packages/shiki-magic-move/src/solid/ShikiMagicMove.tsx

+29-18
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,42 @@ export interface ShikiMagicMoveProps {
1919
}
2020

2121
export function ShikiMagicMove(props: ShikiMagicMoveProps) {
22-
const codeToTokens = (code: string, lineNumbers?: boolean) =>
23-
codeToKeyedTokens(
24-
props.highlighter,
25-
code,
26-
{
27-
lang: props.lang,
28-
theme: props.theme,
29-
},
30-
lineNumbers,
31-
)
22+
const codeToTokens = createMemo(() => {
23+
return (code: string, lineNumbers?: boolean) =>
24+
codeToKeyedTokens(
25+
props.highlighter,
26+
code,
27+
{
28+
lang: props.lang,
29+
theme: props.theme,
30+
},
31+
lineNumbers,
32+
)
33+
})
3234

33-
const machine = createMagicMoveMachine((code, lineNumbers) => codeToTokens(code, lineNumbers))
35+
const machine = createMemo(() => {
36+
const newCodeToTokens = codeToTokens()
37+
return createMagicMoveMachine((code, lineNumbers) =>
38+
newCodeToTokens(code, lineNumbers),
39+
)
40+
})
3441

3542
const result = createMemo(() => {
3643
const lineNumbers = props.options?.lineNumbers ?? false
3744
if (
38-
props.code === machine.current.code &&
39-
props.theme === machine.current.themeName &&
40-
props.lang === machine.current.lang &&
41-
lineNumbers === machine.current.lineNumbers
45+
props.code === machine().current.code &&
46+
props.theme === machine().current.themeName &&
47+
props.lang === machine().current.lang &&
48+
lineNumbers === machine().current.lineNumbers
4249
) {
43-
return machine
50+
return machine()
51+
}
52+
try {
53+
return machine().commit(props.code, props.options)
54+
// eslint-disable-next-line unused-imports/no-unused-vars
55+
} catch (e) {
56+
return machine()
4457
}
45-
46-
return machine.commit(props.code, props.options)
4758
})
4859

4960
return (

0 commit comments

Comments
 (0)