Skip to content

Conversation

@fasterthanlime
Copy link
Contributor

Summary

This PR adds syntax highlighting for non-Rust code blocks in rustdoc using arborium, a tree-sitter based highlighting library.

Currently, code blocks like ```python or ```javascript are rendered as plain text. This PR enables proper syntax highlighting for 15 languages: Python, JavaScript, TypeScript, Bash, C, C++, Go, Java, JSON, TOML, YAML, SQL, Ruby, CSS, and HTML.

Status

This is not intended to merge as-is, but as a basis for discussion.

There are other approaches being explored, such as post-processing the rustdoc HTML output rather than integrating directly into the rendering pipeline. See bearcove/arborium#36 for context on the different approaches.

Usage

Behind an unstable flag:

rustdoc -Z unstable-options --highlight-foreign-code lib.rs

Implementation

  • Adds arborium as a dependency with 15 language grammars
  • Integrates into CodeBlocks iterator in html/markdown.rs
  • Uses custom HTML elements (<a-k> for keywords, <a-s> for strings, etc.)
  • CSS styles match rustdoc's existing theme colors (light/dark/ayu)
  • Falls back to plain text for unsupported languages

Open questions

  • Is compile-time integration the right approach, or should this be a post-processing step?
  • Which languages should be included by default?
  • Binary size impact of tree-sitter grammars (~TBD)

Integrate arborium (tree-sitter based highlighting) to provide syntax
highlighting for non-Rust code blocks in documentation. Previously,
code blocks like ```python or ```javascript were rendered as plain text.

Supported languages: bash, c, cpp, css, go, html, java, javascript,
json, python, ruby, sql, toml, typescript, yaml.

The highlighting uses custom HTML elements (a-k for keywords, a-s for
strings, etc.) which are styled via CSS to match rustdoc's existing
color scheme across all themes (light, dark, ayu).
Gate the arborium-based syntax highlighting behind an unstable flag
(-Z unstable-options --highlight-foreign-code) so it can be tested
before becoming the default behavior.

The flag is threaded through:
- RenderOptions in config.rs
- SharedContext in context.rs
- Markdown/MarkdownWithToc structs in markdown.rs
- CodeBlocks iterator for actual highlighting
Test cases:
- Highlighting enabled: Python, JavaScript, JSON produce arborium tags
- Language aliases work (py -> python, js -> javascript)
- Highlighting disabled: no arborium tags produced
- Unsupported languages fall back to plain escaped text
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. labels Dec 13, 2025
@rust-log-analyzer

This comment has been minimized.

@rustbot rustbot added the A-run-make Area: port run-make Makefiles to rmake.rs label Dec 13, 2025
@miikkas
Copy link

miikkas commented Dec 14, 2025

  • Binary size impact of tree-sitter grammars (~TBD)

Cached crates, build artifacts, cloned repositories etc. quickly sum up to tens of gigabytes of used storage capacity for a developer. From that viewpoint, the question of including either a 171 MiB vs. 22 MiB (quoted from the blog post) binary is that it does not really matter that much. Of course, other reasons may be much more important.

  • Which languages should be included by default?

Going only by the viewpoint above, all would be convenient. If it's not feasible, at least assembly would be nice to have.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-run-make Area: port run-make Makefiles to rmake.rs S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants