Skip to content

Add syntax highlighting to code blocks in rendered markdown #386

Description

@geier

Context

The Markdown component renders assistant messages using marked. Code blocks get <pre><code class="language-X"> tags but display as plain monochrome text with no syntax coloring. This makes code output harder to read compared to the diff/file viewer panels which already use Shiki.

Goal

Code blocks inside rendered markdown should have syntax-highlighted coloring, using the same Shiki setup already in the codebase.

Approach

Use async post-render highlighting (a brief flash of unstyled code is acceptable):

  1. After marked renders HTML and it's inserted into the DOM, walk the <code class="language-*"> elements
  2. Use Shiki's codeToHtml() to highlight each block (reuse the theme config and cache pattern from content-code.tsx)
  3. Replace the plain <code> innerHTML with the highlighted output
  4. Respect light/dark theme (github-light / github-dark as in content-code.tsx)

Alternative: Use marked's async API or a custom renderer that calls Shiki during parsing. Either approach is fine as long as code blocks end up colored.

Files to modify

  • app-prefixable/src/components/markdown.tsx — main change: add Shiki highlighting to code blocks
  • app-prefixable/src/index.css — may need minor CSS adjustments for Shiki's output (e.g. .shiki styles)
  • Reuse highlight logic from app-prefixable/src/components/diff/content-code.tsx (cache, theme config, bundledLanguages check)

Acceptance Criteria

  • Fenced code blocks with a language tag (e.g. ```typescript) render with syntax colors
  • Fenced code blocks without a language tag render as plain text (no error)
  • Light and dark themes both work correctly
  • Copy button still works
  • No significant rendering delay (cache repeated highlights)
  • DOMPurify sanitization does not strip Shiki's <span> color styles

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority:mediumNormal priority (default)readyWell-scoped and ready for implementation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions