fix: render unknown code fence languages as plain text - #169
Open
rsbh wants to merge 1 commit into
Open
Conversation
Code fences using a language Shiki does not bundle threw at build time, for example "Language `logql` not found, you may need to load it first". Shiki's rehype integration only loads a language when it is in the bundle, and otherwise falls through to `fallbackLanguage`. That option was unset, so the unknown language reached `codeToHast` and threw. Set it to `text` so those blocks render unhighlighted instead of failing the page. Fumadocs' defaults are spread because the config type is not `Partial`; the highlighter factory merges them anyway, so themes, notation transformers and meta parsing are unaffected. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughSummary by CodeRabbit
WalkthroughThe Vite configuration updates MDX code rendering. It uses the default rehype code options and falls back to ChangesMDX code rendering
Estimated code review effort: 1 (Trivial) | ~5 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A code fence using a language Shiki doesn't bundle crashes the page at build time:
logqlisn't one of the 347 grammars in@shikijs/langs, but the same applies to any unbundled language — including a simple typo like```typescirpt.Cause
Shiki's rehype integration only loads a language when it's in the bundle, and otherwise falls through to
fallbackLanguage:fallbackLanguagewas unset, so neither branch was taken, the unknown language reachedcodeToHast, and it threw.Fix
Set
fallbackLanguage: 'text'so unbundled languages render unhighlighted instead of failing the page.'text'is safe as the fallback value: it's a Shiki special language, soisLanguageLoadedshort-circuits andloadLanguage('text')is never called — that call would itself throw, since plain languages aren't in the bundle.Fumadocs' defaults are spread because the config type is
RehypeCodeOptionsrather thanPartial<RehypeCodeOptions>, and theCodeOptionsThemesunion inside requirestheme/themes. This is a runtime no-op — the highlighter factory already merges defaults over the passed options.Verification
Checked against real SSR output from a dev server, on a page exercising each case:
```logql```definitely-not-a-language```tstitle="example.js"// [!code highlight]has-highlightedset, marker strippedDual themes intact (
shiki-themes github-light github-dark); dev server log clean.bun run build:cliandbiome lintpass; typecheck error count unchanged at 62 pre-existing before and after.Note
The fallback is silent by design —
fallbackLanguageswaps the language without throwing, soonErrornever fires and there's no hook to log it. A mistyped language now degrades to plain text with no warning. Happy to add a warning in a follow-up if that tradeoff isn't wanted.🤖 Generated with Claude Code