From d1a7a6dbab1e59936c0dcd4278e584fe739e9aaa Mon Sep 17 00:00:00 2001 From: Laurenz Date: Fri, 30 Jun 2023 16:17:52 +0200 Subject: [PATCH] Non-Typst highlighting in documentation --- Cargo.lock | 1 + docs/Cargo.toml | 1 + docs/src/html.rs | 16 +++++++++++++--- library/src/text/raw.rs | 2 +- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 92cb90562..1f5412980 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2495,6 +2495,7 @@ dependencies = [ "pulldown-cmark", "serde", "serde_yaml", + "syntect", "typed-arena", "typst", "typst-library", diff --git a/docs/Cargo.toml b/docs/Cargo.toml index c84a24abb..b6015d01f 100644 --- a/docs/Cargo.toml +++ b/docs/Cargo.toml @@ -20,6 +20,7 @@ once_cell = "1" pulldown-cmark = "0.9" serde = { version = "1", features = ["derive"] } serde_yaml = "0.8" +syntect = { version = "5", default-features = false, features = ["parsing", "html", "regex-fancy"] } typed-arena = "2" unicode_names2 = "0.6.0" unscanny = "0.1" diff --git a/docs/src/html.rs b/docs/src/html.rs index e1f294ee4..f5fceb12a 100644 --- a/docs/src/html.rs +++ b/docs/src/html.rs @@ -1,7 +1,6 @@ use std::ops::Range; use comemo::Prehashed; -use md::escape::escape_html; use pulldown_cmark as md; use typed_arena::Arena; use typst::diag::FileResult; @@ -402,11 +401,22 @@ fn code_block(resolver: &dyn Resolver, lang: &str, text: &str) -> Html { } } - if !matches!(lang, "example" | "typ") { + if lang.is_empty() { let mut buf = String::from("
");
-        escape_html(&mut buf, &display).unwrap();
+        md::escape::escape_html(&mut buf, &display).unwrap();
         buf.push_str("
"); return Html::new(buf); + } else if !matches!(lang, "example" | "typ") { + let set = &*typst_library::text::SYNTAXES; + let buf = syntect::html::highlighted_html_for_string( + &display, + set, + set.find_syntax_by_token(lang) + .unwrap_or_else(|| panic!("unsupported highlighting language: {lang}")), + &typst_library::text::THEME, + ) + .expect("failed to highlight code"); + return Html::new(buf); } let root = typst::syntax::parse(&display); diff --git a/library/src/text/raw.rs b/library/src/text/raw.rs index 1cb3cab1c..24f39bb63 100644 --- a/library/src/text/raw.rs +++ b/library/src/text/raw.rs @@ -343,7 +343,7 @@ fn to_syn(RgbaColor { r, g, b, a }: RgbaColor) -> synt::Color { /// syntaxes/02_Extra/VimHelp.sublime-syntax /// syntaxes/02_Extra/cmd-help/syntaxes/cmd-help.sublime-syntax /// ``` -static SYNTAXES: Lazy = +pub static SYNTAXES: Lazy = Lazy::new(|| syntect::dumps::from_binary(include_bytes!("../../assets/syntect.bin"))); /// The default theme used for syntax highlighting.