Skip to content

Highlight shell and other chat fences with sugar-high v2 - #2175

Merged
SawyerHood merged 2 commits into
mainfrom
bb/fix-1751-shell-fence-lexing
Aug 21, 2026
Merged

SawyerHood merged 2 commits into
mainfrom
bb/fix-1751-shell-fence-lexing

Conversation

@SawyerHood

@SawyerHood SawyerHood commented Aug 21, 2026 •

Copy link
Copy Markdown
Collaborator

What was wrong

Fenced code blocks in chat are highlighted client-side by sugar-high 1.2.1, whose core lexer only knows JavaScript. apps/app/src/components/ui/markdown-code-highlight.ts mapped only rust/python/go/c/java/css fences to v1 presets. Every other fence language (sh, bash, zsh, console, yaml, toml, sql, diff, json, ...) got preset = undefined and ran through the JavaScript lexer. In a ```sh block, # install the plugin became a sign plus three identifiers painted in the full foreground color, and ./plugins/monokai became a green string because the JS lexer read /plugins/monokai as a regex literal. v1 ships no shell preset, so the map could not be extended without writing a lexer. The mobile renderer (apps/mobile/src/markdown/code.ts) mirrored the same map and had the same gap.

Issue: #1751. Report: https://get-bb.github.io/reports/issues/1751.html

What changed

  • apps/app/package.json, apps/mobile/package.json, pnpm-lock.yaml: bump sugar-high from ^1.2.1 to ^2.0.1. v2 ships 25 language tokenizers (shell, yaml, toml, sql, json, diff, html, markdown, dockerfile, kotlin, cpp, ...) and a lang() alias resolver. It is a major version: the root export dropped tokenize/SugarHigh (now in sugar-high/core) and sugar-high/presets is gone. These two files are the only importers in the repo. The lockfile diff is only the two sugar-high entries.
  • apps/app/src/components/ui/markdown-code-highlight.ts: replace the preset map with lang(language) plus a small EXTRA_LANGUAGE_ALIASES table for fence names v2 does not resolve but the old map (or agents) use: console/shellscript -> shell, h -> c, hpp/hh/hxx -> cpp, less -> css. A fence with no language or an unresolved language still goes through the JavaScript lexer as before (sugar-high's root highlight() maps lang: undefined to its javascript config).
  • apps/mobile/src/markdown/code.ts: same resolution, using tokenize from sugar-high/core and the per-language config from sugar-high/lang. A fence with no language or an unresolved language resolves to the javascript config explicitly. This matters in v2: tokenize(code, undefined) from sugar-high/core is now a generic lexer with an empty keyword set and no comment handlers (in v1 it was the JavaScript lexer), so passing undefined would have stripped keyword and comment highlighting from every plain ``` block on mobile. The first revision of this PR did exactly that; the verifier caught it and this revision fixes it. Also drops the as unknown as string[] cast on SugarHigh.TokenTypes, which v2 types as { [key: number]: string }.
  • v2 output keeps the sh__line / sh__token--* classes and --sh-* variables, so markdown-code-highlight.css and the mobile palette are unchanged.

No wire change, no CLI change.

How you verified

New test apps/app/src/components/ui/markdown-code-highlight.test.ts and new cases in apps/mobile/src/markdown/code.test.ts. Both files include a null / ruby (unknown language) case asserting const is a keyword and // hi is a comment.

On origin/main source with sugar-high 1.2.1 installed (git checkout origin/main -- <source files + package.json + lockfile> + pnpm install --frozen-lockfile), only the shell/new-language cases fail; the null/ruby cases pass, so they guard v1 behavior:

@bb/app    Tests  8 failed | 2 passed (10)
AssertionError: expected [ [ 'sign', '#' ], …(14) ] to deep equally contain [ 'comment', '# install the plugin' ]
@bb/mobile Tests  5 failed | 6 passed (11)
AssertionError: expected [ { text: '#', type: 'sign' }, …(6) ] to deeply equal [ { …(2) } ]

On the first revision of this branch (46ba725, undefined config on mobile), the new mobile null/ruby cases fail:

@bb/mobile Tests  2 failed | 9 passed (11)
AssertionError: expected { text: 'const', type: 'identifier' } to deeply equal { text: 'const', type: 'keyword' }

On this branch both files pass (10/10 and 11/11). From the committed tree (git status --porcelain empty):

pnpm exec turbo run typecheck --filter=@bb/app --filter=@bb/mobile   # Tasks: 4 successful, 4 total
pnpm exec turbo run test --filter=@bb/app --filter=@bb/mobile        # Tasks: 5 successful, 5 total
                                                                      # @bb/app 3166 passed | 3 skipped; @bb/mobile 833 passed

pnpm install --frozen-lockfile passes with the lockfile containing only the sugar-high hunks.

Manual (web, first revision; the web source is unchanged since): started my own dev instance, spawned a claude-code thread whose prompt held a ```sh and a ```python fence with the same # install the plugin first line, and dumped the DOM with doobie. Both blocks render the first line as one sh__token--comment (computed color oklch(0.5 0 0), the subtle tier) and the sh block has no sh__token--string for ./plugins/monokai. Before the fix the report shows the same line as sign + identifiers at full foreground and the path in green.

Fixes #1751

AGENT GENERATED: by Claude Opus 5

Independent verification

Verified at b09884bfe (round 2) in a separate worktree, rebased state confirmed with git merge-base --is-ancestor origin/main HEAD.

Root cause check. Confirmed independently against sugar-high 1.2.1 and 2.0.1 sources: v1 highlight(code, undefined) is the JS lexer and v1 ships no shell preset; v2 lib/index.js configFor(name) resolves name || 'javascript', while lib/core.js tokenize(code, undefined) is the generic no-keyword/no-comment lexer. The revised mobile tokenizerConfigFor resolves null/unknown to the javascript config, matching the web path. v2 still emits sh__line / sh__token--* / var(--sh-*) with identical shape for empty lines and still HTML-escapes (<script> -> &lt;script&gt;), so markdown-code-highlight.css and dangerouslySetInnerHTML are unaffected. Metro already resolves sugar-high subpath exports (./presets on v1), so ./core and ./lang resolve the same way. No other importers of sugar-high exist in the repo.

Fail-before / pass-after.

  • git checkout origin/main -- apps/app/src/components/ui/markdown-code-highlight.ts apps/mobile/src/markdown/code.ts apps/app/package.json apps/mobile/package.json pnpm-lock.yaml && pnpm install --frozen-lockfile --prefer-offline (sugar-high 1.2.1 installed), then pnpm exec vitest run on the PR's two test files: app 8 failed | 2 passed (10) with AssertionError: expected [ [ 'sign', '#' ], …(14) ] to deep equally contain [ 'comment', '# install the plugin' ]; mobile 5 failed | 6 passed (11) with expected [ { text: '#', type: 'sign' }, …(6) ] to deeply equal [ { …(2) } ]. The null/ruby cases pass on main, so they guard v1 behavior.
  • Round-1 regression guard: with only apps/mobile/src/markdown/code.ts at 46ba725f5 (v2 installed), mobile 2 failed | 9 passed with expected { text: 'const', type: 'identifier' } to deeply equal { text: 'const', type: 'keyword' }.
  • Restored HEAD files, reinstalled (2.0.1): mobile 11 passed (11), app 10 passed (10).

Turbo (forced, from the committed tree, git status --porcelain empty).

  • pnpm exec turbo run typecheck --filter=@bb/app --filter=@bb/mobile --force -> Tasks: 4 successful, 4 total
  • pnpm exec turbo run test --filter=@bb/app --filter=@bb/mobile --force -> Tasks: 5 successful, 5 total (@bb/app 3166 passed | 3 skipped, @bb/mobile 833 passed)

Repro on the fixed branch. Own dev instance (scripts/bb-dev-app current, app :17771), project proj_w583gwhs6b, thread thr_qaybud2d5f (claude-code, "Reply only with ok." plus the report's sh and python fences). Headless Chromium DOM dump of the rendered user message: SH block first line is a single sh__token--comment "# install the plugin" with computed color oklch(0.5 0 0) (same as the PYTHON control), and the block contains no sh__token--string (./plugins/monokai is no longer a regex literal). No longer reproduces.

CI. All checks on b09884bfe pass (Checks, Package Smoke ubuntu + macOS, Tests app-1/2/3, integration, server, packages, version check); iOS simulator flows and Node compatibility smoke skipped as usual.

Residual risks. Major bump of sugar-high: token assignment for already-mapped languages can shift slightly (colors, not content). Languages v2 still lacks (ruby, lua, ini, ...) stay on the JS lexer as before. v2 emits an empty sh__token--comment span at the start of the line after a # comment; harmless markup. Bundle grows by 25 small language tables.

AGENT GENERATED: by Claude Opus 5

SawyerHood and others added 2 commits August 21, 2026 07:49
Fenced code blocks in chat were highlighted by sugar-high v1, whose core
lexer only knows JavaScript. bb mapped rust/python/go/c/java/css fences to
v1 presets; every other language (sh, bash, zsh, yaml, toml, sql, diff,
json, ...) fell through to the JS lexer, so `# comment` in a shell block
rendered as punctuation plus identifiers and `./plugins/monokai` became a
regex-literal string.

sugar-high v2 ships 25 language tokenizers and a `lang()` alias resolver
that maps sh/bash/zsh to shell, py to python, c++/cc to cpp, yml to yaml,
and so on. Use it in the web highlighter and the mobile tokenizer, and keep
a small alias table for the fence names v2 does not resolve (console,
shellscript, h, hpp, less). The HTML output still uses sh__token--* classes
and --sh-* variables, so the palette CSS is unchanged.

Fixes #1751

Co-Authored-By: Claude <[email protected]>
sugar-high v2's `tokenize(code, undefined)` from `sugar-high/core` is a
generic lexer with an empty keyword set and no comment handlers, unlike
v1 where it was the JavaScript lexer. The mobile port passed `undefined`
for fences with no language and for languages v2 does not know, so plain
``` blocks lost keyword and comment highlighting. Resolve those to the
`javascript` config, exactly as sugar-high's root `highlight()` does.

Also drop the unrelated bunyan engines normalization from the lockfile.

Co-Authored-By: Claude <[email protected]>
@SawyerHood
SawyerHood marked this pull request as ready for review August 21, 2026 08:14
@SawyerHood
SawyerHood merged commit cf00cfe into main Aug 21, 2026
13 checks passed
@SawyerHood
SawyerHood deleted the bb/fix-1751-shell-fence-lexing branch August 21, 2026 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Shell fences in chat code blocks are lexed as JavaScript

1 participant