Highlight code blocks in article bodies

arborium (tree-sitter) renders each token as a flat <span class="hl-…">,
which the X4's no-descendant-selector stylesheet can still target.

The sanitizer strips class attributes, so language hints never reached
the database. annotate_hints now copies them onto <pre data-lang> before
readability and sanitizing: pre/code classes, Rouge and GitHub wrapper
divs, SyntaxHighlighter's brush:, and data-lang style attributes.
ammonia keeps only canonical values.

Highlighting runs at render time for the web article page and both EPUB
editions, so stored articles benefit too. Unhinted blocks are guessed
from content (JSON that parses, shebangs, leading <script>/<style>, then
betlang at >= 0.5). A borderline block also takes the article's prevailing
language when that language is among its top candidates. Anything else
stays plain, since wrong colours are worse than none.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
thalladaandClaude Opus 5.5 committed 2026-10-01 02:27:23 +00:00
1 parent 8d0516bedc
commit 78fc38e99d
7 files changed
+1744 -7

No files matched your search

+1 -1
View File
@@ -1415,7 +1415,7 @@ fn thousands(value: i64) -> String {
/// Sanitize article HTML exactly as the EPUB does, then add browser-only image
/// loading/referrer attributes without converting the fragment to XHTML.
fn prepare_body(html: &str) -> String {
let clean = ammonia::clean(html);
let clean = crate::highlight::clean_and_highlight(html);
let mut output = String::with_capacity(clean.len() + 64);
let mut cursor = 0usize;
while let Some(relative) = clean[cursor..].find('<') {