Skip to content

Commit f119bbe

Browse files
committed
fix(showcase/shell-docs): snippet validates reg.code shape + logs unknown langs
- Guard `reg.code` as a string before rendering; malformed `demo-content.json` (missing `code`, stale bundle) now produces a visible WarningBox instead of crashing React on `undefined`. - `resolveHljsLanguage` warns once per unknown language via a module-scope Set, so authors learn the right names to use and stop relying on hljs.highlightAuto guessing wrong.
1 parent bcbc827 commit f119bbe

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

showcase/shell-docs/src/components/snippet.tsx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ function WarningBox({ children }: { children: React.ReactNode }) {
120120
);
121121
}
122122

123+
// Track languages we've already warned about so each unknown language name
124+
// only produces one console message per process, regardless of how many
125+
// <Snippet>s reference it.
126+
const warnedUnknownLanguages = new Set<string>();
127+
123128
/** Map the bundler's coarse language hint to an hljs language name. */
124129
function resolveHljsLanguage(lang: string): string | null {
125130
const map: Record<string, string> = {
@@ -133,7 +138,16 @@ function resolveHljsLanguage(lang: string): string | null {
133138
markdown: "markdown",
134139
text: "plaintext",
135140
};
136-
return map[lang] ?? null;
141+
const mapped = map[lang];
142+
if (mapped) return mapped;
143+
if (lang && !warnedUnknownLanguages.has(lang)) {
144+
warnedUnknownLanguages.add(lang);
145+
console.warn(
146+
`[snippet] unknown language "${lang}" — falling back to hljs.highlightAuto. ` +
147+
`Add it to resolveHljsLanguage() for deterministic highlighting.`,
148+
);
149+
}
150+
return null;
137151
}
138152

139153
/**
@@ -321,6 +335,20 @@ export function Snippet({
321335
reg = result;
322336
}
323337

338+
// Guard against malformed bundler output — if `demo-content.json` is
339+
// produced from an in-flight build we could theoretically see a region
340+
// missing `code` or with non-string `code`. Render a warning rather than
341+
// letting React crash on `undefined`.
342+
if (typeof reg.code !== "string") {
343+
return (
344+
<WarningBox>
345+
Snippet for <code>{key}</code> has no <code>code</code> string — check{" "}
346+
<code>demo-content.json</code> (region/file may be malformed or the
347+
bundle is out of date).
348+
</WarningBox>
349+
);
350+
}
351+
324352
const hljsLang = resolveHljsLanguage(reg.language);
325353
let html: string;
326354
let highlightFailed = false;

0 commit comments

Comments
 (0)