Repository navigation
Highlight shell and other chat fences with sugar-high v2 - #2175
Merged
Merged
Conversation
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]>
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.
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.tsmapped only rust/python/go/c/java/css fences to v1 presets. Every other fence language (sh,bash,zsh,console,yaml,toml,sql,diff,json, ...) gotpreset = undefinedand ran through the JavaScript lexer. In a```shblock,# install the pluginbecame asignplus threeidentifiers painted in the full foreground color, and./plugins/monokaibecame a greenstringbecause the JS lexer read/plugins/monokaias 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: bumpsugar-highfrom^1.2.1to^2.0.1. v2 ships 25 language tokenizers (shell, yaml, toml, sql, json, diff, html, markdown, dockerfile, kotlin, cpp, ...) and alang()alias resolver. It is a major version: the root export droppedtokenize/SugarHigh(now insugar-high/core) andsugar-high/presetsis gone. These two files are the only importers in the repo. The lockfile diff is only the twosugar-highentries.apps/app/src/components/ui/markdown-code-highlight.ts: replace the preset map withlang(language)plus a smallEXTRA_LANGUAGE_ALIASEStable 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 roothighlight()mapslang: undefinedto itsjavascriptconfig).apps/mobile/src/markdown/code.ts: same resolution, usingtokenizefromsugar-high/coreand the per-languageconfigfromsugar-high/lang. A fence with no language or an unresolved language resolves to thejavascriptconfig explicitly. This matters in v2:tokenize(code, undefined)fromsugar-high/coreis now a generic lexer with an empty keyword set and no comment handlers (in v1 it was the JavaScript lexer), so passingundefinedwould 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 theas unknown as string[]cast onSugarHigh.TokenTypes, which v2 types as{ [key: number]: string }.sh__line/sh__token--*classes and--sh-*variables, somarkdown-code-highlight.cssand 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.tsand new cases inapps/mobile/src/markdown/code.test.ts. Both files include anull/ruby(unknown language) case assertingconstis akeywordand// hiis acomment.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; thenull/rubycases pass, so they guard v1 behavior:On the first revision of this branch (46ba725,
undefinedconfig on mobile), the new mobilenull/rubycases fail:On this branch both files pass (10/10 and 11/11). From the committed tree (
git status --porcelainempty):pnpm install --frozen-lockfilepasses with the lockfile containing only thesugar-highhunks.Manual (web, first revision; the web source is unchanged since): started my own dev instance, spawned a claude-code thread whose prompt held a
```shand a```pythonfence with the same# install the pluginfirst line, and dumped the DOM with doobie. Both blocks render the first line as onesh__token--comment(computed coloroklch(0.5 0 0), the subtle tier) and theshblock has nosh__token--stringfor./plugins/monokai. Before the fix the report shows the same line assign+ identifiers at full foreground and the path in green.Fixes #1751
Independent verification
Verified at
b09884bfe(round 2) in a separate worktree, rebased state confirmed withgit 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; v2lib/index.jsconfigFor(name)resolvesname || 'javascript', whilelib/core.jstokenize(code, undefined)is the generic no-keyword/no-comment lexer. The revised mobiletokenizerConfigForresolvesnull/unknown to thejavascriptconfig, matching the web path. v2 still emitssh__line/sh__token--*/var(--sh-*)with identical shape for empty lines and still HTML-escapes (<script>-><script>), somarkdown-code-highlight.cssanddangerouslySetInnerHTMLare unaffected. Metro already resolves sugar-high subpath exports (./presetson v1), so./coreand./langresolve 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), thenpnpm exec vitest runon the PR's two test files: app8 failed | 2 passed (10)withAssertionError: expected [ [ 'sign', '#' ], …(14) ] to deep equally contain [ 'comment', '# install the plugin' ]; mobile5 failed | 6 passed (11)withexpected [ { text: '#', type: 'sign' }, …(6) ] to deeply equal [ { …(2) } ]. The null/ruby cases pass on main, so they guard v1 behavior.apps/mobile/src/markdown/code.tsat46ba725f5(v2 installed), mobile2 failed | 9 passedwithexpected { text: 'const', type: 'identifier' } to deeply equal { text: 'const', type: 'keyword' }.HEADfiles, reinstalled (2.0.1): mobile11 passed (11), app10 passed (10).Turbo (forced, from the committed tree,
git status --porcelainempty).pnpm exec turbo run typecheck --filter=@bb/app --filter=@bb/mobile --force->Tasks: 4 successful, 4 totalpnpm 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), projectproj_w583gwhs6b, threadthr_qaybud2d5f(claude-code, "Reply only with ok." plus the report'ssh andpython fences). Headless Chromium DOM dump of the rendered user message: SH block first line is a singlesh__token--comment"# install the plugin" with computed coloroklch(0.5 0 0)(same as the PYTHON control), and the block contains nosh__token--string(./plugins/monokaiis no longer a regex literal). No longer reproduces.CI. All checks on
b09884bfepass (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--commentspan at the start of the line after a#comment; harmless markup. Bundle grows by 25 small language tables.