Skip to content

Commit e70aaa8

Browse files
committed
Catch lighter error
1 parent bc2b6ac commit e70aaa8

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

packages/mdx/src/highlighter/index.tsx

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,23 @@ export async function highlight({
2222
}
2323
}
2424

25-
const r = await light(code, lang as any, theme)
25+
try {
26+
const r = await light(code, lang as any, theme)
2627

27-
const lines = r.lines.map(line => ({
28-
tokens: line.map(token => ({
29-
content: token.content,
30-
props: { style: token.style },
31-
})),
32-
}))
28+
const lines = r.lines.map(line => ({
29+
tokens: line.map(token => ({
30+
content: token.content,
31+
props: { style: token.style },
32+
})),
33+
}))
3334

34-
return { lines, lang }
35+
return { lines, lang }
36+
} catch (e) {
37+
// TODO check error is "missing grammar"
38+
console.warn(
39+
"[Code Hike warning]",
40+
`${lang} is not a valid language, no syntax highlighting will be applied.`
41+
)
42+
return highlight({ code, lang: "text", theme })
43+
}
3544
}

0 commit comments

Comments
 (0)