Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion apps/readme/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ rust-version.workspace = true
publish = false

[features]
default = ["gpu", "comrak", "floats"]
default = ["gpu", "comrak", "syntax-highlighting-giallo", "floats"]

# Renderers
gpu = ["dep:anyrender_vello"]
Expand All @@ -23,6 +23,7 @@ cpu-base = ["dep:anyrender_vello_cpu"]
avif = ["dep:image", "image?/avif-native"]
comrak = ["dep:comrak"]
pulldown_cmark = ["dep:pulldown-cmark"]
syntax-highlighting-giallo = ["dep:giallo"]
floats = ["blitz-dom/floats"]
cache = ["blitz-net/cache"]
log_frame_times = [
Expand Down Expand Up @@ -51,6 +52,7 @@ reqwest = { workspace = true }
url = { workspace = true }
winit = { workspace = true }
comrak = { version = "0.49", default-features = false, optional = true }
giallo = { version = "0.1", features = ["dump"], optional = true }
pulldown-cmark = { version = "0.13", default-features = false, features = ["html"], optional = true }
image = { workspace = true, default-features = false, optional = true }
notify = "8.0.0"
4 changes: 4 additions & 0 deletions apps/readme/assets/blitz-markdown-overrides.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@
[src$="#gh-dark-mode-only"] {
display: none !important;
}
}

.giallo {
background: #f6f8fa !important;
}
6 changes: 6 additions & 0 deletions apps/readme/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ fn main() {
html = markdown_to_html(html);
stylesheets.push(String::from(GITHUB_MD_STYLES));
stylesheets.push(String::from(BLITZ_MD_STYLES));

#[cfg(feature = "syntax-highlighting-giallo")]
{
stylesheets.push(String::from(giallo::GIALLO_CSS))
}

title = format!(
"README for {}",
base_url.rsplit("/").find(|s| !s.is_empty()).unwrap()
Expand Down
75 changes: 69 additions & 6 deletions apps/readme/src/markdown/comrak.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
//! Render the readme.md using the gpu renderer

use comrak::{Options, markdown_to_html_with_plugins, options};

pub(crate) fn markdown_to_html(contents: String) -> String {
let plugins = options::Plugins::default();
// let syntax_highligher = CustomSyntectAdapter(SyntectAdapter::new(Some("InspiredGitHub")));
// plugins.render.codefence_syntax_highlighter = Some(&syntax_highligher as _);
#[allow(unused_mut)]
let mut plugins = options::Plugins::default();

#[cfg(feature = "syntax-highlighting-giallo")]
use giallo_highlighter::{GialloAdapter, ThemeVariant};
#[cfg(feature = "syntax-highlighting-giallo")]
let syntax_highligher = GialloAdapter(ThemeVariant::Single("github-light"));
#[cfg(feature = "syntax-highlighting-giallo")]
{
plugins.render.codefence_syntax_highlighter = Some(&syntax_highligher as _);
}

let body_html = markdown_to_html_with_plugins(
&contents,
Expand Down Expand Up @@ -50,7 +56,64 @@ pub(crate) fn markdown_to_html(contents: String) -> String {
)
}

// #[allow(unused)]
#[cfg(feature = "syntax-highlighting-giallo")]
mod giallo_highlighter {
use comrak::adapters::SyntaxHighlighterAdapter;
pub(crate) use giallo::ThemeVariant;
use giallo::{HighlightOptions, HtmlRenderer, PLAIN_GRAMMAR_NAME, RenderOptions};
use std::borrow::Cow;
use std::collections::HashMap;

static GIALLO_REGISTRY: std::sync::LazyLock<giallo::Registry> =
std::sync::LazyLock::new(|| {
let mut registry = giallo::Registry::builtin().unwrap();
registry.link_grammars();
registry
});

pub(crate) struct GialloAdapter(pub(crate) ThemeVariant<&'static str>);

impl SyntaxHighlighterAdapter for GialloAdapter {
fn write_highlighted(
&self,
output: &mut dyn std::fmt::Write,
lang: Option<&str>,
code: &str,
) -> std::fmt::Result {
let norm_lang = lang.map(|l| l.split_once(',').map(|(lang, _)| lang).unwrap_or(l));
let norm_lang = norm_lang.unwrap_or(PLAIN_GRAMMAR_NAME);
let options = HighlightOptions::new(&norm_lang, self.0);
let highlighted = GIALLO_REGISTRY.highlight(code, options).unwrap();
let render_options = RenderOptions {
show_line_numbers: false,
..Default::default()
};
let html = HtmlRenderer::default().render(&highlighted, &render_options);
println!("{}", &html);
output.write_str(&html)
}

fn write_pre_tag(
&self,
output: &mut dyn std::fmt::Write,
attributes: HashMap<&'static str, Cow<'_, str>>,
) -> std::fmt::Result {
let _ = attributes;
output.write_str("")
}

fn write_code_tag(
&self,
output: &mut dyn std::fmt::Write,
attributes: HashMap<&'static str, Cow<'_, str>>,
) -> std::fmt::Result {
let _ = attributes;
output.write_str("")
}
}
}

// #[cfg(feature = "syntax-highlighting-syntect")]
// mod syntax_highlighter {
// use comrak::adapters::SyntaxHighlighterAdapter;
// use comrak::plugins::syntect::SyntectAdapter;
Expand Down
Loading