Skip to content

refactor: render body code blocks with the shared highlighter - #154

Closed
ArtMin96 wants to merge 8 commits into
dev-null-sh:productionfrom
ArtMin96:feat/shared-code-highlighting
Closed

ArtMin96 wants to merge 8 commits into
dev-null-sh:productionfrom
ArtMin96:feat/shared-code-highlighting

Conversation

@ArtMin96

@ArtMin96 ArtMin96 commented Oct 15, 2026 •

Copy link
Copy Markdown

Stacked on #153 (the commit types). Review that first. This PR's one commit, and nothing from #153, is in the compare view: ArtMin96/dev-null.sh@feat/commit-types...feat/shared-code-highlighting

Fenced code blocks in commit bodies (added in #124) now render through the CodeHighlighter that #153 introduced for snippets, so there is one code-rendering path instead of two. Body blocks gain a language label and a copy button, the change hardens code-block rendering, and the code theme is tuned for contrast.

Summary

  • CodeSpans::block() highlights through CodeHighlighter instead of calling tempest/highlight directly.
  • A body block now carries its language label and its raw code, and renders through the shared CodeBlock component (label + copy), the same one snippets use.
  • Fence aliases (js, ts, yml, sh, console, …) map to the snippet languages; SnippetLanguage gains a Terminal case and tryFromName(). A fence the app doesn't know keeps its own name as the label.
  • Code token colors become their own CSS variables, darkened in light mode to meet 4.5:1 on the muted block background.
  • 40 tests added (1262 → 1302), 100% line and type coverage, PHPStan level: max.

Why this design

#124 highlighted body blocks on their own code path, next to the one snippets use since #153. Routing body blocks through CodeHighlighter means:

  • One implementation. CodeSpans no longer depends on Tempest\Highlight\Highlighter; it calls CodeHighlighter::highlight(), which both snippets and body blocks now share, including its cache.
  • Hardened rendering. The highlighter's output is validated, and a block falls back to its escaped code when the output isn't clean.
  • The same UI. Body blocks render through CodeBlock (language label, copy of the raw code), so a block in a body and a snippet card look and behave alike.

What's included

  • Shared highlighting: CodeSpans::block() resolves the fence's language through SnippetLanguage::tryFromName() (name or alias, any case) and highlights through CodeHighlighter. An unknown fence is labelled with its own name and left unhighlighted; a bare fence is plain text. Each block segment now carries language_label, code (raw, for copying) and validated html.
  • Aliases and Terminal: SnippetLanguage::tryFromName() maps js/node, ts, py, yml, sh/shell, console/term, tf/hcl, and others to their language; a new Terminal case covers shell session transcripts.
  • Frontend: commit-body.tsx renders each block with <CodeBlock> instead of its own markup, passing code, html and languageLabel.
  • Code theme: The hl-* token colors move to --code-* CSS variables, with light-mode values darkened to read at 4.5:1 or more on the muted block background, and separate dark-mode values.
Light Dark
js block, label + Copy
After Copy
Plain-text block

Mobile (390px): js block light / dark; plain block light / dark.

Performance & security

  • Body blocks now share the snippet cache, so identical code in identical languages is highlighted once.
  • This hardens code-block rendering: the highlighter output is validated and falls back to escaped code.
  • No new routes, endpoints or user input: the change is in how an existing body is rendered.

Testing

40 tests added (1262 → 1302), 100% line and type coverage, PHPStan level: max.

  • CommitCodeBlockTest: a block is labelled by its fence (name or alias), an unknown fence keeps its name and stays unhighlighted, a bare fence is plain text, and the raw code travels beside the escaped HTML for copying.
  • CodeSpansTest: alias mapping across the languages, the language label and raw code on each segment, and a fallback to escaped plain code when the highlighter output is not clean.
  • CommitControllerTest: the body segments of a commit with a code block carry the label and raw code.

Related work

Notes for reviewers

  • Stacked on feat: polls, snippets, quotes and questions #153; the compare link at the top is only this one commit.
  • Behaviour-preserving for valid code: a js block still highlights as before, now with a label and a copy button and a slightly higher-contrast theme.

🤖 Generated with Claude Code

ArtMin96 and others added 8 commits October 16, 2026 01:49
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@brendt

brendt commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

Can you rebase?

@ArtMin96 ArtMin96 closed this Oct 16, 2026
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.

2 participants