Skip to content

Commit 410907d

Browse files
committed
Readme: Implement syntax highlighting via giallo
Signed-off-by: Nico Burns <nico@nicoburns.com>
1 parent e95896d commit 410907d

5 files changed

Lines changed: 140 additions & 7 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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ rust-version.workspace = true
77
publish = false
88

99
[features]
10-
default = ["gpu", "comrak", "floats"]
10+
default = ["gpu", "comrak", "syntax-highlighting-giallo", "floats"]
1111

1212
# Renderers
1313
gpu = ["dep:anyrender_vello"]
@@ -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.49", 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: 69 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::{Options, markdown_to_html_with_plugins, options};
42

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

0 commit comments

Comments
 (0)