Skip to content

Fix highlighting of inline multi-file code under 'init' - #6

Merged
dav-is merged 1 commit into
mainfrom
fix/init-inline-extra-files
Sep 26, 2026
Merged

dav-is merged 1 commit into
mainfrom
fix/init-inline-extra-files

Conversation

@dav-is

@dav-is dav-is commented Sep 26, 2026

Copy link
Copy Markdown
Member

Summary

With highlightAfter: 'init' and inline multi-file code with no url, the extra files were never highlighted, and the server HTML showed every file plain, the main file included. Inline sources no longer need a url to be parsed.

Cause

  • Server: without a url, loadIsomorphicCodeVariant passed inline extraFiles through as raw strings. With strings left, the client holds the whole block's highlight gate until it can parse them, which it can't do during a server render, so every file rendered plain.
  • Client: areGrammarsRegistered read Starry Night's scopes(), which lists a grammar as soon as register() starts, while highlighting keeps using the previous registry until it resolves. Under CodeProviderLazy, a block rendering in that window parsed its string files into plain text and never re-parsed.

Fix

  • Without a url, inline source strings go through the same loadExtraFiles pipeline as with one (parse, enhance with each file's comments, TS→JS transforms, fallback, line counts). Already-parsed or empty inline sources are kept as before; the URL path is unchanged. Inline extras are now parsed for every highlightAfter value, as they always were with a url; visible timing for 'idle'/'hydration' is unchanged.
  • parseSource records which scopes are ready once createStarryNight or register() settles, and areGrammarsRegistered reads that. A failed registration still fails open.
  • Each file is parsed once on the server; the client has nothing left to parse. Server cost matches a url-based block (about 10 ms per small extra file, including compression).

Tests

  • loadIsomorphicCodeVariant: inline extra files without a url are parsed, and match the url result (updated one assertion that expected the unparsed string).
  • CodeSourceLoader: server-rendered output has every file highlighted under 'init', and plain under 'idle'.
  • areGrammarsRegistered stays false while a grammar is still registering; CodeHighlighterClient highlights string files when their grammars are mid-registration at mount.
  • New unlinked e2e fixture code-highlighter/demos/demo-init-inline-files: server HTML, hydration and the TS→JS toggle, with no console errors.

Verification

  • pnpm test --run, pnpm typescript, pnpm eslint, prettier
  • pnpm docs:lib, pnpm docs:build, pnpm docs:validate (0 changes, twice)
  • E2e on a production build: all pass except the known demo-variants flake (about 40%, same as main)

@dav-is dav-is added the bug Something isn't working label Sep 26, 2026
@vercel

vercel Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
fairgarden-docs Ready Ready Preview Sep 26, 2026 5:38pm UTC

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The implementation addresses both server and client failure modes with comprehensive regression coverage.

Review effort: Balanced
Findings: None

What changed in this PR

Fixes inline multi-file syntax highlighting when highlightAfter: 'init' is used without a source URL, while preventing clients from treating in-progress grammars as ready.

Changes:

  • Tracks grammar scopes only after Starry Night creation or registration settles.
  • Processes inline extra files through the standard parsing, transformation, enhancement, and fallback pipeline.
  • Adds unit, server-rendering, client hydration, and end-to-end regression coverage.
File Description
packages/​docs/​src/​pipeline/​parseSource/​parseSource.ts Tracks grammar scopes that are ready for highlighting.
packages/​docs/​src/​pipeline/​parseSource/​grammarCache.ts Reads ready scopes instead of prematurely registered scopes.
packages/​docs/​src/​pipeline/​parseSource/​ensureGrammars.test.ts Tests behavior during in-progress registration.
packages/​docs/​src/​pipeline/​loadIsomorphicCodeVariant/​loadIsomorphicCodeVariant.ts Parses inline extra files without requiring a base URL.
packages/​docs/​src/​pipeline/​loadIsomorphicCodeVariant/​loadIsomorphicCodeVariant.test.ts Covers parsing, transformations, disabled parsing, and pre-parsed sources.
packages/​docs/​src/​CodeHighlighter/​CodeSourceLoader.test.tsx Verifies server-rendered highlighting timing.
packages/​docs/​src/​CodeHighlighter/​CodeHighlighterClient.stringFiles.test.tsx Verifies client handling of string files and grammar-registration races.
docs/​app/​lib/​components/​code-highlighter/​demos/​demo-init-inline-files/​test.ts Adds end-to-end server, hydration, and transform checks.
docs/​app/​lib/​components/​code-highlighter/​demos/​demo-init-inline-files/​page.tsx Exposes the regression fixture as a route.
docs/​app/​lib/​components/​code-highlighter/​demos/​demo-init-inline-files/​InlineFiles.tsx Configures inline multi-file highlighting scenarios.
docs/​app/​lib/​components/​code-highlighter/​demos/​demo-init-inline-files/​FilesContent.tsx Renders all files and supports TS-to-JS toggling.
docs/​app/​lib/​components/​code-highlighter/​demos/​demo-init-inline-files/​code.ts Defines the inline multi-file fixture.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@dav-is
dav-is marked this pull request as ready for review September 26, 2026 17:44
@dav-is
dav-is merged commit 3e6159f into main Sep 26, 2026
10 checks passed
@dav-is
dav-is deleted the fix/init-inline-extra-files branch September 26, 2026 17:44

This branch was successfully deployed

1 active deployment
Preview — b3bcd186 Deployed Sep 26, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants