Skip to content

Add a hook for syntax highlighting of code blocks - #24

Merged
servercimen merged 1 commit into
feature/tag-imagefrom
feature/code-highlighting
Nov 20, 2026
Merged

servercimen merged 1 commit into
feature/tag-imagefrom
feature/code-highlighting

Conversation

@servercimen

@servercimen servercimen commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

Summary

Code blocks showed in one color. The parser kept the fence's language, but layout did not use it.

  • TranscriptTextView.codeHighlighter and TranscriptText.codeHighlighter(_:) take an async function. It gets a code block's text and the fence's language. It returns TranscriptCodeHighlights: UTF-16 ranges with a TranscriptColor, and bold or italic if you want them.
  • The image store keeps the colors as it keeps pictures. A code block shows in the text color until its colors arrive. Then TranscriptKit lays that block out again, and no other block.
  • The highlighter runs off the main thread, at most once at a time for each block. The newest text waits for its turn, and texts in between are skipped. While code streams in, a block keeps its newest colors on the text they were made for.
  • Colors are TranscriptColors, so a highlighter can give a light and a dark value.
  • Code colors have their own generation in the store, so a new highlighter does not drop images that are loading.
  • The package keeps no dependency: the app supplies the highlighter. The README has an example.

Closes #8.

Stack

This PR is part of a stack. It is based on #23 (feature/tag-image), so its diff shows only this change. Merge the stack in order, starting with #17.

Tests

  • Five new unit tests: colors arrive and bold changes the font, only the colored block is laid out again, a streaming block keeps its last colors and skips the texts in between, no highlighter gives no colors, and a new highlighter keeps images that are loading. swift test: 145 tests pass.
  • The package builds for iOS.
  • On an iPhone 12 mini, a local demo app colors Swift, Python and JSON blocks with HighlightSwift through this hook, with its light theme in light mode and its dark theme in dark mode. The demo app is not in this PR.

🤖 Generated with Claude Code

Code blocks showed in one color. The parser kept the fence's language, but
layout did not use it.

The app gives TranscriptTextView.codeHighlighter, or
TranscriptText.codeHighlighter(_:), an async function: it gets a code
block's text and language and returns colored ranges, with bold or italic.
The image store keeps the results, the way it keeps pictures: a code block
is drawn in the text color until its colors arrive, and then laid out
again. The highlighter runs off the main thread, at most once at a time for
each block; the newest text waits for its turn, and texts in between are
skipped. While code streams in, a block keeps its newest colors on the text
they were made for. The package keeps no dependency: the app supplies the
highlighter.

Code colors have their own generation in the store, so a new highlighter
does not drop images that are loading.

Closes #8.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

Add syntax highlighting for fenced code blocks

1 participant