Theming
A bundled stylesheet is embedded automatically. It covers standard tree-sitter and nvim-treesitter capture names and follows mdBook’s built-in colour schemes, so most books need no CSS of their own.
The embedded stylesheet sits in the mdbook-tsitter cascade layer, so any rule
in your own [output.html] additional-css overrides it, whatever its
specificity. Add a stylesheet only for what you want to change, such as custom
capture names or colours for a custom mdBook theme:
[output.html]
additional-css = ["theme/highlighting.css"]
Each capture gets the ts- prefix, dots become hyphens, and every prefix is
emitted so broad rules can cascade into more specific ones:
| capture | generated classes |
|---|---|
keyword | ts-keyword |
keyword.operator | ts-keyword ts-keyword-operator |
string.regexp | ts-string ts-string-regexp |
For example:
.ts-comment {
font-style: italic;
}
code[data-language="rust"] .ts-function-macro {
color: var(--ts-purple);
}
Each highlighted block carries its fence tag as data-language, so a
code[data-language="…"] selector scopes a rule to one language.
Everforest
Include the bundled Everforest dark theme (medium contrast):
[preprocessor.tsitter]
everforest = true
Select Everforest from the book’s theme picker. This includes the book
interface, highlight.js colours for blocks left to mdBook, and tree-sitter
capture styles with language-specific overrides, italic comments, bold
headings, and underlined URLs. No extra CSS, script, or template is needed.
The theme is included on every chapter, even pages without code blocks.
It can also supply the highlights for books that already use an .everforest
theme. Custom highlight_ts.css and default-css still control the base
highlighting stylesheet; the Everforest option supplies its own styles.
Theme directory
Like mdBook’s own theme files,
the highlighting stylesheet can be replaced from the book’s theme directory
(theme/, or [output.html] theme). A highlight_ts.css there is embedded
instead of the bundled stylesheet, as mdBook uses a highlight.css there
instead of its highlight.js theme. Start from the bundled one:
mdbook-tsitter css > theme/highlight_ts.css
Without a highlight_ts.css, a theme that only replaces mdBook’s highlight.js
stylesheets still colours tree-sitter blocks: the bundled stylesheet takes each
role’s colour from the matching highlight.js class (.hljs-keyword for
keywords, .hljs-string for strings, and so on). A replaced highlight.css
applies to the light, rust, and custom themes, tomorrow-night.css to coal and
navy, and ayu-highlight.css to ayu, as in mdBook.
Set default-css = false under [preprocessor.tsitter] to embed no
highlighting stylesheet at all.
There is no fixed capture list in the preprocessor. Capture names come directly
from each grammar’s queries; names beginning with _ are treated as internal
and left unstyled.
This book
This book enables everforest = true in [preprocessor.tsitter]. Choose
Everforest from the brush icon in the menu bar to see the bundled theme.
Its only additional stylesheet is theme/comparison.css, which lays out the
side-by-side comparisons.