Skip to content

Code block styling and syntax highlighting#4

Merged
genmon merged 2 commits intoinanimate-tech:mainfrom
lazyatom:feature/code-block-styling
Apr 10, 2026
Merged

Code block styling and syntax highlighting#4
genmon merged 2 commits intoinanimate-tech:mainfrom
lazyatom:feature/code-block-styling

Conversation

@lazyatom
Copy link
Copy Markdown
Contributor

@lazyatom lazyatom commented Apr 10, 2026

Fenced code blocks (``` ... ```) now render with a background colour, monospace font, and syntax highlighting — bringing them in line with how inline code is already styled. Fixes #3.

What changed

  • Code block detection — A new cross-node pass in the markdown decoration plugin identifies ``` fence pairs across paragraph boundaries, applying node-level decorations for
    background/font and suppressing inline markdown patterns (bold, italic, etc.) inside code blocks.
  • Syntax highlighting — Tokens inside code blocks are highlighted using sugar-high, with colours for keywords, strings, comments, properties, and more. Both light and dark
    themes are covered.
  • Tests — 20 new tests covering fence detection, token highlighting, and language preset resolution.

Why sugar-high?

The editor renders markdown as plain text with ProseMirror decorations — there are no structured CodeBlock nodes in the schema. This rules out the usual ProseMirror/TipTap syntax highlighting extensions (@tiptap/extension-code-block-lowlight, prosemirror-highlight, etc.) which all expect a dedicated code block node type.

sugar-high is a good fit because:

  • Tiny — ~1KB gzipped, zero runtime dependencies
  • Token API — tokenize() returns [type, text] tuples that map directly to Decoration.inline(), no HTML parsing needed
  • Language presets — built-in support for JS/TS/JSX (default), Python, Rust, Go, C/C++, Java, and CSS
  • Graceful fallback — unrecognised languages get JS-style tokenization, which handles most C-family syntax reasonably

If you later move to a richer document schema with actual code block nodes, we could swap in lowlight or Shiki, but sugar-high fits the current decoration-based architecture well.

Fenced code blocks (``` ... ```) now get a background color and
monospace font via the existing markdown decoration system, matching
the inline code style. Fence delimiters are dimmed, inline markdown
patterns are suppressed inside code blocks, and the max-width
constraint is lifted for code content.
Integrates sugar-high (~1KB gzipped, zero deps) for syntax
highlighting inside fenced code blocks. Tokens are applied as
ProseMirror inline decorations (sh-keyword, sh-string, sh-comment,
etc.) with colours for both light and dark themes.

Built-in language presets for JS/TS/JSX (default), Python, Rust, Go,
C/C++, Java, and CSS. Unrecognised languages fall back to the default
JS-oriented tokenizer which handles most C-family syntax reasonably.
@genmon
Copy link
Copy Markdown
Contributor

genmon commented Apr 10, 2026

Looks great, thank you!

@genmon genmon merged commit 787276e into inanimate-tech:main Apr 10, 2026
3 checks passed
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.

Render code blocks properly

2 participants