Skip to content

Commit 69a2c51

Browse files
committed
Readme: Implement syntax highlighting via giallo
Signed-off-by: Nico Burns <nico@nicoburns.com>
1 parent 60be177 commit 69a2c51

5 files changed

Lines changed: 138 additions & 6 deletions

File tree

Cargo.lock

Lines changed: 58 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/readme/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ cpu-base = ["dep:anyrender_vello_cpu"]
2323
avif = ["dep:image", "image?/avif-native"]
2424
comrak = ["dep:comrak"]
2525
pulldown_cmark = ["dep:pulldown-cmark"]
26+
syntax-highlighting-giallo = ["dep:giallo"]
2627
floats = ["blitz-dom/floats"]
2728
cache = ["blitz-net/cache"]
2829
log_frame_times = [
@@ -51,6 +52,7 @@ reqwest = { workspace = true }
5152
url = { workspace = true }
5253
winit = { workspace = true }
5354
comrak = { version = "0.43", default-features = false, optional = true }
55+
giallo = { version = "0.1", features = ["dump"], optional = true }
5456
pulldown-cmark = { version = "0.13", default-features = false, features = ["html"], optional = true }
5557
image = { workspace = true, default-features = false, optional = true }
5658
notify = "8.0.0"

apps/readme/assets/blitz-markdown-overrides.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,8 @@
2828
[src$="#gh-dark-mode-only"] {
2929
display: none !important;
3030
}
31+
}
32+
33+
.giallo {
34+
background: #f6f8fa !important;
3135
}

apps/readme/src/main.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ fn main() {
9090
html = markdown_to_html(html);
9191
stylesheets.push(String::from(GITHUB_MD_STYLES));
9292
stylesheets.push(String::from(BLITZ_MD_STYLES));
93+
94+
#[cfg(feature = "syntax-highlighting-giallo")]
95+
{
96+
stylesheets.push(String::from(giallo::GIALLO_CSS))
97+
}
98+
9399
title = format!(
94100
"README for {}",
95101
base_url.rsplit("/").find(|s| !s.is_empty()).unwrap()

apps/readme/src/markdown/comrak.rs

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
//! Render the readme.md using the gpu renderer
2-
31
use comrak::{ExtensionOptions, Options, Plugins, RenderOptions, markdown_to_html_with_plugins};
42

53
pub(crate) fn markdown_to_html(contents: String) -> String {
6-
let plugins = Plugins::default();
7-
// let syntax_highligher = CustomSyntectAdapter(SyntectAdapter::new(Some("InspiredGitHub")));
8-
// plugins.render.codefence_syntax_highlighter = Some(&syntax_highligher as _);
4+
#[allow(unused_mut)]
5+
let mut plugins = Plugins::default();
6+
7+
#[cfg(feature = "syntax-highlighting-giallo")]
8+
use giallo_highlighter::{GialloAdapter, ThemeVariant};
9+
#[cfg(feature = "syntax-highlighting-giallo")]
10+
let syntax_highligher = GialloAdapter(ThemeVariant::Single("github-light"));
11+
#[cfg(feature = "syntax-highlighting-giallo")]
12+
{
13+
plugins.render.codefence_syntax_highlighter = Some(&syntax_highligher as _);
14+
}
915

1016
let body_html = markdown_to_html_with_plugins(
1117
&contents,
@@ -50,7 +56,63 @@ pub(crate) fn markdown_to_html(contents: String) -> String {
5056
)
5157
}
5258

53-
// #[allow(unused)]
59+
#[cfg(feature = "syntax-highlighting-giallo")]
60+
mod giallo_highlighter {
61+
use comrak::adapters::SyntaxHighlighterAdapter;
62+
pub(crate) use giallo::ThemeVariant;
63+
use giallo::{HighlightOptions, HtmlRenderer, PLAIN_GRAMMAR_NAME, RenderOptions};
64+
use std::collections::HashMap;
65+
66+
static GIALLO_REGISTRY: std::sync::LazyLock<giallo::Registry> =
67+
std::sync::LazyLock::new(|| {
68+
let mut registry = giallo::Registry::builtin().unwrap();
69+
registry.link_grammars();
70+
registry
71+
});
72+
73+
pub(crate) struct GialloAdapter(pub(crate) ThemeVariant<&'static str>);
74+
75+
impl SyntaxHighlighterAdapter for GialloAdapter {
76+
fn write_highlighted(
77+
&self,
78+
output: &mut dyn std::fmt::Write,
79+
lang: Option<&str>,
80+
code: &str,
81+
) -> std::fmt::Result {
82+
let norm_lang = lang.map(|l| l.split_once(',').map(|(lang, _)| lang).unwrap_or(l));
83+
let norm_lang = norm_lang.unwrap_or(PLAIN_GRAMMAR_NAME);
84+
let options = HighlightOptions::new(&norm_lang, self.0);
85+
let highlighted = GIALLO_REGISTRY.highlight(code, options).unwrap();
86+
let render_options = RenderOptions {
87+
show_line_numbers: false,
88+
..Default::default()
89+
};
90+
let html = HtmlRenderer::default().render(&highlighted, &render_options);
91+
println!("{}", &html);
92+
output.write_str(&html)
93+
}
94+
95+
fn write_pre_tag(
96+
&self,
97+
output: &mut dyn std::fmt::Write,
98+
attributes: HashMap<String, String>,
99+
) -> std::fmt::Result {
100+
let _ = attributes;
101+
output.write_str("")
102+
}
103+
104+
fn write_code_tag(
105+
&self,
106+
output: &mut dyn std::fmt::Write,
107+
attributes: HashMap<String, String>,
108+
) -> std::fmt::Result {
109+
let _ = attributes;
110+
output.write_str("")
111+
}
112+
}
113+
}
114+
115+
// #[cfg(feature = "syntax-highlighting-syntect")]
54116
// mod syntax_highlighter {
55117
// use comrak::adapters::SyntaxHighlighterAdapter;
56118
// use comrak::plugins::syntect::SyntectAdapter;

0 commit comments

Comments
 (0)