Skip to content

Commit 28d4704

Browse files
committed
fix: handle unknown Shiki languages gracefully
Fixes TANSTACK-COM-M6 - Add eslintrc alias to jsonc - Return effectiveLang from getHighlighter to ensure failed languages fallback to plaintext
1 parent 0d84d32 commit 28d4704

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/components/markdown/CodeBlock.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@ const LANG_ALIASES: Record<string, string> = {
2121
text: 'plaintext',
2222
yml: 'yaml',
2323
json5: 'jsonc',
24+
eslintrc: 'jsonc',
2425
}
2526

2627
// Lazy highlighter singleton
2728
let highlighterPromise: Promise<HighlighterGeneric<any, any>> | null = null
2829
let mermaidInstance: Mermaid | null = null
2930
const genSvgMap = new Map<string, string>()
31+
const failedLanguages = new Set<string>()
3032

31-
async function getHighlighter(language: string) {
33+
async function getHighlighter(language: string): Promise<{
34+
highlighter: HighlighterGeneric<any, any>
35+
effectiveLang: string
36+
}> {
3237
if (!highlighterPromise) {
3338
highlighterPromise = createHighlighter({
3439
themes: ['github-light', 'vitesse-dark'],
@@ -62,16 +67,23 @@ async function getHighlighter(language: string) {
6267
const normalizedLang = LANG_ALIASES[language] || language
6368
const langToLoad = normalizedLang === 'mermaid' ? 'plaintext' : normalizedLang
6469

70+
// Return plaintext for known failed languages
71+
if (failedLanguages.has(langToLoad)) {
72+
return { highlighter, effectiveLang: 'plaintext' }
73+
}
74+
6575
// Load language if not already loaded
6676
if (!highlighter.getLoadedLanguages().includes(langToLoad as any)) {
6777
try {
6878
await highlighter.loadLanguage(langToLoad as any)
6979
} catch {
7080
console.warn(`Shiki: Language "${langToLoad}" not found, using plaintext`)
81+
failedLanguages.add(langToLoad)
82+
return { highlighter, effectiveLang: 'plaintext' }
7183
}
7284
}
7385

74-
return highlighter
86+
return { highlighter, effectiveLang: langToLoad }
7587
}
7688

7789
// Lazy load mermaid only when needed
@@ -153,11 +165,8 @@ export function CodeBlock({
153165
;(async () => {
154166
const themes = ['github-light', 'vitesse-dark']
155167
const langStr = lang || 'plaintext'
156-
const normalizedLang = LANG_ALIASES[langStr] || langStr
157-
const effectiveLang =
158-
normalizedLang === 'mermaid' ? 'plaintext' : normalizedLang
159168

160-
const highlighter = await getHighlighter(langStr)
169+
const { highlighter, effectiveLang } = await getHighlighter(langStr)
161170
// Trim trailing newlines to prevent empty lines at end of code block
162171
const trimmedCode = (code || '').trimEnd()
163172

0 commit comments

Comments
 (0)