use std::fs;
use std::path::Path;
use hikari_components::basic::{Card, CardProps, Image, ImageProps, Link, LinkProps};
use hikari_components::display::{
Empty, EmptyProps, Skeleton, SkeletonProps, Tag, TagProps, TagVariant, Timeline, TimelineProps,
};
use hikari_components::layout::divider::{DividerOrientation, DividerType};
use hikari_components::layout::{Col, ColProps, Divider, DividerProps};
use hikari_components::navigation::{Menu, MenuProps};
use tairitsu_vdom::{el, txt, VNode};
use crate::markdown::{Block, Inline, SpannedBlock};
pub fn render_to_html(blocks: &[Block]) -> String {
let wrapped = wrap_unspanned(blocks);
render_blocks(&wrapped).render_to_html()
}
pub fn render_to_html_with_live(
blocks: &[SpannedBlock],
live_html: &std::collections::HashMap<String, String>,
live_config: &crate::config::LiveConfig,
out_dir: &Path,
) -> String {
let inner = render_blocks_with_live(blocks, live_html, live_config, out_dir);
inner.render_to_html()
}
fn wrap_unspanned(blocks: &[Block]) -> Vec<SpannedBlock> {
blocks
.iter()
.enumerate()
.map(|(i, b)| SpannedBlock {
block: b.clone(),
span: Default::default(),
id: format!("wrap-{}", i + 1),
heading_path: Vec::new(),
})
.collect()
}
pub fn render_blocks(blocks: &[SpannedBlock]) -> VNode {
VNode::Fragment(blocks.iter().map(render_block).collect())
}
fn render_blocks_with_live(
blocks: &[SpannedBlock],
live_html: &std::collections::HashMap<String, String>,
live_config: &crate::config::LiveConfig,
out_dir: &Path,
) -> VNode {
let mut out: Vec<VNode> = Vec::new();
let mut i = 0;
while i < blocks.len() {
let Block::LiveComponent { source, lang } = &blocks[i].block else {
out.push(render_block_with_live(&blocks[i], live_html));
i += 1;
continue;
};
let mut rust_src: Option<&str> = None;
let mut tsx_src: Option<&str> = None;
if lang.as_deref() == Some("rust") {
rust_src = Some(source.as_str());
} else if lang.as_deref() == Some("tsx") {
tsx_src = Some(source.as_str());
} else {
rust_src = Some(source.as_str());
}
if i + 1 < blocks.len() {
if let Block::LiveComponent {
source: next_src,
lang: next_lang,
} = &blocks[i + 1].block
{
if next_lang.as_deref() == Some("tsx") && tsx_src.is_none() {
tsx_src = Some(next_src.as_str());
i += 1;
} else if next_lang.as_deref() == Some("rust") && rust_src.is_none() {
rust_src = Some(next_src.as_str());
i += 1;
}
}
}
out.push(render_live_pair(
rust_src,
tsx_src,
live_html,
live_config,
out_dir,
));
i += 1;
}
VNode::Fragment(out)
}
fn render_block(sb: &SpannedBlock) -> VNode {
render_block_with_live(sb, &std::collections::HashMap::new())
}
fn render_block_with_live(
sb: &SpannedBlock,
live_html: &std::collections::HashMap<String, String>,
) -> VNode {
let b = &sb.block;
match b {
Block::Heading { level, text } => {
el_node(&format!("h{level}"), render_inlines(text))
}
Block::Paragraph(inlines) => el_node("p", render_inlines(inlines)),
Block::CodeBlock { lang, code } => {
let lang_class = format!("language-{}", lang.as_deref().unwrap_or(""));
el_pre_code(&lang_class, code)
}
Block::LiveComponent { source, .. } => render_live_block(source, live_html.get(source)),
Block::Diagram { kind, source } => render_diagram_block(*kind, source),
Block::List {
ordered,
start,
items,
} => {
let tag = if *ordered { "ol" } else { "ul" };
let lis: Vec<VNode> = items
.iter()
.map(|item| {
let mut children = vec![VNode::Fragment(render_inlines(&item.inlines))];
if !item.children.is_empty() {
let kids = wrap_unspanned(&item.children);
children.push(VNode::Fragment(
kids.iter()
.map(|sb| render_block_with_live(sb, live_html))
.collect(),
));
}
el_node("li", children)
})
.collect();
let mut list = el(tag);
if *ordered && *start != 1 {
list = list.attr("start", start.to_string());
}
VNode::Element(Box::new(list.children(lis)))
}
Block::Blockquote(inner) => {
el_node("blockquote", vec![render_blocks(inner)])
}
Block::Table { headers, rows } => {
let ths: Vec<VNode> = headers
.iter()
.map(|h| el_node("th", render_inlines(h)))
.collect();
let thead = VNode::Element(Box::new(
el("thead").child(VNode::Element(Box::new(el("tr").children(ths)))),
));
let mut trs = Vec::new();
for row in rows {
let tds: Vec<VNode> = row
.iter()
.map(|c| el_node("td", render_inlines(c)))
.collect();
trs.push(VNode::Element(Box::new(el("tr").children(tds))));
}
let tbody = VNode::Element(Box::new(el("tbody").children(trs)));
el_node("table", vec![thead, tbody])
}
Block::ThematicBreak => Divider(DividerProps {
text: None,
orientation: DividerOrientation::Horizontal,
divider_type: DividerType::Solid,
text_align: "center".to_string(),
rtl: None,
..Default::default()
}),
Block::Center(inner) => VNode::Element(Box::new(
el("div")
.attr("style", "text-align:center")
.children(vec![render_blocks(inner)]),
)),
Block::Div { attrs, children } => {
let style = extract_style_attr(attrs);
let mut d = el("div");
if let Some(s) = style {
d = d.attr("style", &s);
}
VNode::Element(Box::new(d.children(vec![render_blocks(children)])))
}
Block::Html(raw) => VNode::Element(Box::new(el("div").dangerous_inner_html(raw))),
}
}
fn extract_style_attr(attrs: &str) -> Option<String> {
let needle = "style=\"";
let start = attrs.find(needle)?;
let rest = &attrs[start + needle.len()..];
let end = rest.find('"')?;
Some(rest[..end].to_string())
}
fn render_inlines(inlines: &[Inline]) -> Vec<VNode> {
inlines.iter().map(render_inline).collect()
}
fn render_inline(i: &Inline) -> VNode {
match i {
Inline::Text(s) => txt(s),
Inline::Strong(inner) => el_node("strong", render_inlines(inner)),
Inline::Emphasis(inner) => el_node("em", render_inlines(inner)),
Inline::Code(s) => Tag(TagProps {
variant: TagVariant::Default,
closable: false,
on_close: None,
class: "hi-tag-code".to_string(),
style: String::new(),
children: txt(s),
}),
Inline::Link { text, url } => Link(LinkProps {
href: rewrite_link(url),
children: VNode::Fragment(render_inlines(text)),
..Default::default()
}),
Inline::Image { alt, url } => match split_image_theme_fragment(url) {
(src, Some(scheme)) => VNode::Element(Box::new(
el("img")
.attr("src", src)
.attr("alt", alt)
.attr("class", scheme.class()),
)),
(_, None) => Image(ImageProps {
src: Some(url.clone()),
alt: alt.clone(),
..Default::default()
}),
},
Inline::InlineHtml(raw) => VNode::Element(Box::new(el("span").dangerous_inner_html(raw))),
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ImageScheme {
Light,
Dark,
}
impl ImageScheme {
fn class(self) -> &'static str {
match self {
ImageScheme::Light => "lg-img-only-light",
ImageScheme::Dark => "lg-img-only-dark",
}
}
}
fn split_image_theme_fragment(url: &str) -> (&str, Option<ImageScheme>) {
const SUFFIXES: [(&str, ImageScheme); 4] = [
("#gh-light-mode-only", ImageScheme::Light),
("#gh-dark-mode-only", ImageScheme::Dark),
("#only-light", ImageScheme::Light),
("#only-dark", ImageScheme::Dark),
];
for (suffix, scheme) in SUFFIXES {
if let Some(stripped) = url.strip_suffix(suffix) {
return (stripped, Some(scheme));
}
}
(url, None)
}
fn render_live_block(source: &str, rendered_html: Option<&String>) -> VNode {
let live_html = rendered_html
.map(|html| std::collections::HashMap::from([(source.to_string(), html.clone())]))
.unwrap_or_default();
render_live_pair(
Some(source),
None,
&live_html,
&crate::config::LiveConfig::default(),
Path::new(""),
)
}
fn render_live_pair(
rust_src: Option<&str>,
tsx_src: Option<&str>,
live_html: &std::collections::HashMap<String, String>,
_live_config: &crate::config::LiveConfig,
out_dir: &Path,
) -> VNode {
let has_rust = rust_src.is_some();
let has_tsx = tsx_src.is_some();
let mut children = Vec::new();
children.push(VNode::Element(Box::new(
el("div").attr("class", "lg-live-langs").children(vec![
VNode::Element(Box::new(
el("button")
.attr(
"class",
if has_tsx {
"lg-live-lang"
} else {
"lg-live-lang active"
},
)
.attr("data-lang", "rust")
.child(txt("Rust")),
)),
VNode::Element(Box::new(
el("button")
.attr(
"class",
if has_tsx {
"lg-live-lang active"
} else {
"lg-live-lang"
},
)
.attr("data-lang", "tsx")
.child(txt("TSX")),
)),
]),
)));
children.push(VNode::Element(Box::new(
el("div").attr("class", "lg-live-tabs").children(vec![
VNode::Element(Box::new(
el("button")
.attr("class", "lg-live-tab active")
.attr("data-tab", "preview")
.child(txt("Preview")),
)),
VNode::Element(Box::new(
el("button")
.attr("class", "lg-live-tab")
.attr("data-tab", "source")
.child(txt("Source")),
)),
]),
)));
let rust_html = rust_src.and_then(|s| live_html.get(s));
let rust_preview = if let Some(html) = rust_html {
Card(CardProps {
children: VNode::Element(Box::new(
el("div")
.attr("class", "lg-live-preview-inner")
.dangerous_inner_html(html),
)),
..Default::default()
})
} else if has_rust {
Empty(EmptyProps {
description: "(live preview unavailable)".to_string(),
..Default::default()
})
} else {
Card(CardProps { children: VNode::Element(Box::new(el("div").attr("class", "lg-live-preview-inner").attr("style","display:flex;align-items:center;justify-content:center;min-height:80px;color:var(--fg-sec)").child(txt("No Rust source provided")))), ..Default::default() })
};
children.push(VNode::Element(Box::new(
el("div")
.attr("class", "lg-live-preview")
.attr("data-lang-group", "rust")
.attr("style", if has_tsx { "display:none" } else { "" })
.child(rust_preview),
)));
let tsx_content = if let Some(src) = tsx_src {
let hash = crate::live::short_hash(src);
let demo_dir = out_dir.join("static").join("demo").join(&hash);
let _ = fs::create_dir_all(&demo_dir);
let doc = format!(
"<!doctype html><html lang=en><head><meta charset=utf-8><meta name=viewport content=\"width=device-width,initial-scale=1\"><style>body{{margin:0;padding:1rem;font-family:system-ui;background:#0b1220;color:#cfe3ff}}{lcss}</style></head><body>{body}</body></html>",
lcss = crate::theme::build_css(&crate::config::ThemeConfig::default()),
body = src,
);
let _ = fs::write(demo_dir.join("index.html"), &doc);
let url = format!("/static/demo/{hash}/");
VNode::Element(Box::new(
el("iframe")
.attr("src", &url)
.attr(
"style",
"width:100%;height:300px;border:none;border-radius:4px",
)
.attr("sandbox", "allow-scripts"),
))
} else if has_tsx {
VNode::Element(Box::new(
el("div")
.attr("class", "lg-live-preview-inner")
.attr("style", "display:flex;align-items:center;justify-content:center;min-height:80px;color:var(--fg-sec)")
.child(txt("TSX preview — set [live] preview_url in lagrange.toml to enable")),
))
} else {
VNode::Element(Box::new(
el("div")
.attr("class", "lg-live-preview-inner")
.attr("style", "display:flex;align-items:center;justify-content:center;min-height:80px;color:var(--fg-sec)")
.child(txt("Add a ```hikari:tsx block after ```hikari:rust to enable TSX preview")),
))
};
children.push(VNode::Element(Box::new(
el("div")
.attr("class", "lg-live-preview")
.attr("data-lang-group", "tsx")
.attr("style", if has_tsx { "" } else { "display:none" })
.child(Card(CardProps {
children: tsx_content,
..Default::default()
})),
)));
if let Some(src) = rust_src {
let highlighted = syntax_highlight(src, "rust");
let lc = src.lines().count().max(1);
let nums: Vec<VNode> = (1..=lc)
.map(|i| {
VNode::Element(Box::new(
el("div")
.attr("class", "hi-code-highlight-line-number")
.child(txt(&i.to_string())),
))
})
.collect();
let source_div = el("div")
.attr("class", "hi-code-highlight-content")
.child(VNode::Element(Box::new(
el("div")
.attr("class", "hi-code-highlight-line-numbers")
.children(nums),
)))
.child(VNode::Element(Box::new(
el("pre")
.attr("class", "hi-code-highlight-code hi-scroll-container")
.child(VNode::Element(Box::new(
el("code")
.attr("class", "language-rust")
.dangerous_inner_html(&highlighted),
))),
)));
let wrapper = el("div")
.attr("class", "hi-code-highlight")
.attr("style", "display:none")
.attr("data-lg-source", "")
.attr("data-lang-group", "rust")
.child(VNode::Element(Box::new(source_div)));
children.push(VNode::Element(Box::new(wrapper)));
}
if let Some(src) = tsx_src {
let highlighted = syntax_highlight(src, "tsx");
let lc = src.lines().count().max(1);
let nums: Vec<VNode> = (1..=lc)
.map(|i| {
VNode::Element(Box::new(
el("div")
.attr("class", "hi-code-highlight-line-number")
.child(txt(&i.to_string())),
))
})
.collect();
let source_div = el("div")
.attr("class", "hi-code-highlight-content")
.child(VNode::Element(Box::new(
el("div")
.attr("class", "hi-code-highlight-line-numbers")
.children(nums),
)))
.child(VNode::Element(Box::new(
el("pre")
.attr("class", "hi-code-highlight-code hi-scroll-container")
.child(VNode::Element(Box::new(
el("code")
.attr("class", "language-tsx")
.dangerous_inner_html(&highlighted),
))),
)));
let wrapper = el("div")
.attr("class", "hi-code-highlight")
.attr("style", "display:none")
.attr("data-lg-source", "")
.attr("data-lang-group", "tsx")
.child(VNode::Element(Box::new(source_div)));
children.push(VNode::Element(Box::new(wrapper)));
}
VNode::Element(Box::new(
el("div").attr("class", "lg-live-block").children(children),
))
}
fn render_diagram_block(kind: crate::markdown::DiagramKind, source: &str) -> VNode {
let lang = kind.source_lang();
let highlighted = syntax_highlight(source, lang);
let line_count = source.lines().count().max(1);
let line_num_nodes: Vec<VNode> = (1..=line_count)
.map(|i| {
VNode::Element(Box::new(
el("div")
.attr("class", "hi-code-highlight-line-number")
.child(txt(&i.to_string())),
))
})
.collect();
let copy_btn = VNode::Element(Box::new(
el("button")
.attr("class", "hi-code-highlight-copy")
.attr("type", "button")
.attr("data-copy", source)
.attr("aria-label", "Copy code")
.attr("title", "Copy code")
.child(VNode::Element(Box::new(
el("span")
.attr("class", "hi-code-highlight-copy-icon")
.attr("aria-hidden", "true")
.dangerous_inner_html(crate::icons::icon_svg("content-copy", 14)),
)))
.child(VNode::Element(Box::new(
el("span")
.attr("class", "hi-code-highlight-check")
.attr("aria-hidden", "true")
.dangerous_inner_html(crate::icons::icon_svg("check", 14)),
))),
));
let toggle = VNode::Element(Box::new(
el("div")
.attr("class", "lg-diagram-toggle")
.attr("role", "tablist")
.children(vec![
VNode::Element(Box::new(
el("button")
.attr("type", "button")
.attr("class", "lg-diagram-toggle-btn active")
.attr("data-pane", "preview")
.child(txt("Preview")),
)),
VNode::Element(Box::new(
el("button")
.attr("type", "button")
.attr("class", "lg-diagram-toggle-btn")
.attr("data-pane", "source")
.child(txt("Source")),
)),
]),
));
VNode::Element(Box::new(
el("div")
.attr("class", "lg-diagram")
.attr("data-diagram-kind", kind.attr())
.children(vec![
VNode::Element(Box::new(
el("div")
.attr("class", "hi-code-highlight-header lg-diagram-header")
.children(vec![
VNode::Element(Box::new(
el("span")
.attr("class", "hi-code-highlight-language")
.child(txt(lang)),
)),
VNode::Element(Box::new(
el("div")
.attr("class", "lg-diagram-actions")
.children(vec![toggle, copy_btn]),
)),
]),
)),
VNode::Element(Box::new(
el("div")
.attr("class", "lg-diagram-preview")
.attr("data-pane", "preview")
.children(vec![
VNode::Element(Box::new(el("div").attr("class", "lg-diagram-canvas"))),
VNode::Element(Box::new(
el("pre")
.attr("class", "lg-diagram-raw")
.attr("style", "display:none")
.child(txt(source)),
)),
]),
)),
VNode::Element(Box::new(
el("div")
.attr("class", "lg-diagram-source")
.attr("data-pane", "source")
.attr("style", "display:none")
.child(VNode::Element(Box::new(
el("div")
.attr("class", "hi-code-highlight-content")
.children(vec![
VNode::Element(Box::new(
el("div")
.attr("class", "hi-code-highlight-line-numbers")
.children(line_num_nodes),
)),
VNode::Element(Box::new(
el("pre")
.attr(
"class",
"hi-code-highlight-code hi-scroll-container",
)
.child(VNode::Element(Box::new(
el("code")
.attr("class", format!("language-{lang}"))
.dangerous_inner_html(&highlighted),
))),
)),
]),
))),
)),
]),
))
}
fn el_node(tag: &str, children: Vec<VNode>) -> VNode {
VNode::Element(Box::new(el(tag).children(children)))
}
fn el_pre_code(lang_class: &str, code: &str) -> VNode {
let lang = lang_class.trim_start_matches("language-");
let highlighted = syntax_highlight(code, lang);
let line_count = code.lines().count().max(1);
let line_num_nodes: Vec<VNode> = (1..=line_count)
.map(|i| {
VNode::Element(Box::new(
el("div")
.attr("class", "hi-code-highlight-line-number")
.child(txt(&i.to_string())),
))
})
.collect();
let mut code_el = el("code");
if !lang_class.is_empty() {
code_el = code_el.attr("class", lang_class);
}
code_el = code_el.dangerous_inner_html(&highlighted);
let display_lang = if lang.is_empty() { "text" } else { lang };
let header = el("div")
.attr("class", "hi-code-highlight-header")
.children(vec![
VNode::Element(Box::new(
el("span")
.attr("class", "hi-code-highlight-language")
.child(txt(display_lang)),
)),
VNode::Element(Box::new(
el("button")
.attr("class", "hi-code-highlight-copy")
.attr("type", "button")
.attr("data-copy", code)
.attr("aria-label", "Copy code")
.attr("title", "Copy code")
.child(VNode::Element(Box::new(
el("span")
.attr("class", "hi-code-highlight-copy-icon")
.attr("aria-hidden", "true")
.dangerous_inner_html(crate::icons::icon_svg("content-copy", 14)),
)))
.child(VNode::Element(Box::new(
el("span")
.attr("class", "hi-code-highlight-check")
.attr("aria-hidden", "true")
.dangerous_inner_html(crate::icons::icon_svg("check", 14)),
))),
)),
]);
VNode::Element(Box::new(
el("div")
.attr("class", "hi-code-highlight")
.child(VNode::Element(Box::new(header)))
.child(VNode::Element(Box::new(
el("div")
.attr("class", "hi-code-highlight-content")
.child(VNode::Element(Box::new(
el("div")
.attr("class", "hi-code-highlight-line-numbers")
.children(line_num_nodes),
)))
.child(VNode::Element(Box::new(
el("pre")
.attr("class", "hi-code-highlight-code hi-scroll-container")
.child(VNode::Element(Box::new(code_el))),
))),
))),
))
}
fn syntax_highlight(code: &str, lang: &str) -> String {
use syntect::html::{ClassStyle, ClassedHTMLGenerator};
use syntect::parsing::SyntaxSet;
use syntect::util::LinesWithEndings;
let ps = SyntaxSet::load_defaults_newlines();
let syntax = ps
.find_syntax_by_token(lang)
.or_else(|| ps.find_syntax_by_extension(lang));
match syntax {
Some(syntax) => {
let mut generator =
ClassedHTMLGenerator::new_with_class_style(syntax, &ps, ClassStyle::Spaced);
for line in LinesWithEndings::from(code) {
let _ = generator.parse_html_for_line_which_includes_newline(line);
}
generator.finalize()
}
None => html_escape(code),
}
}
fn html_escape(s: &str) -> String {
s.replace('&', "&")
.replace('<', "<")
.replace('>', ">")
}
fn rewrite_link(url: &str) -> String {
if url.starts_with("http://")
|| url.starts_with("https://")
|| url.starts_with("mailto:")
|| url.starts_with('#')
{
return url.to_string();
}
let (path, fragment) = match url.split_once('#') {
Some((p, f)) => (p, Some(f)),
None => (url, None),
};
if path.is_empty() {
return url.to_string();
}
let stripped = path.strip_prefix("./").unwrap_or(path);
let rewritten = if std::path::Path::new(stripped)
.file_name()
.map(|f| f == "README.md" || f == "readme.md")
.unwrap_or(false)
{
let dir = std::path::Path::new(stripped)
.parent()
.map(|p| p.to_path_buf());
match dir {
Some(d) if !d.as_os_str().is_empty() => format!("{}/index.html", d.display()),
_ => "index.html".to_string(),
}
} else {
stripped
.strip_suffix(".md")
.map(|p| format!("{p}.html"))
.unwrap_or_else(|| stripped.to_string())
};
match fragment {
Some(f) => format!("{rewritten}#{f}"),
None => rewritten,
}
}
pub fn render_skeleton() -> VNode {
Skeleton(SkeletonProps {
..Default::default()
})
}
pub fn render_menu() -> VNode {
Menu(MenuProps {
..Default::default()
})
}
pub fn render_col(children: VNode) -> VNode {
Col(ColProps {
children: Some(children),
..Default::default()
})
}
pub fn render_timeline() -> VNode {
Timeline(TimelineProps {
..Default::default()
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::markdown::parse;
#[test]
fn ordered_list_numbers_once_via_the_list_element() {
let html = render_to_html(&parse("1. one\n2. two\n"));
assert!(
html.contains("<ol><li>one</li>"),
"unexpected markup: {html}"
);
assert!(!html.contains("hk-badge"), "badge still rendered: {html}");
assert_eq!(html.matches("<ol>").count(), 1, "one list expected: {html}");
}
#[test]
fn ordered_list_start_number_reaches_the_list_element() {
let html = render_to_html(&parse("3. three\n4. four\n"));
assert!(
html.contains("<ol start=\"3\">"),
"start number lost: {html}"
);
assert!(html.contains("<li>three</li>"), "{html}");
}
#[test]
fn nested_list_renders_inside_the_parent_item() {
let html = render_to_html(&parse("1. parent\n - child\n2. second\n"));
assert!(
html.contains("<li>parent<ul><li>child</li></ul></li>"),
"nesting lost: {html}"
);
}
#[test]
fn unordered_list_keeps_plain_items() {
let html = render_to_html(&parse("- a\n- b\n"));
assert_eq!(html, "<ul><li>a</li><li>b</li></ul>");
}
#[test]
fn theme_fragment_github_spelling() {
assert_eq!(
split_image_theme_fragment("shot.webp#gh-light-mode-only"),
("shot.webp", Some(ImageScheme::Light))
);
assert_eq!(
split_image_theme_fragment("shot.webp#gh-dark-mode-only"),
("shot.webp", Some(ImageScheme::Dark))
);
}
#[test]
fn theme_fragment_mkdocs_aliases() {
assert_eq!(
split_image_theme_fragment("../a/b.webp#only-light"),
("../a/b.webp", Some(ImageScheme::Light))
);
assert_eq!(
split_image_theme_fragment("../a/b.webp#only-dark"),
("../a/b.webp", Some(ImageScheme::Dark))
);
}
#[test]
fn theme_fragment_untagged_passthrough() {
assert_eq!(split_image_theme_fragment("shot.webp"), ("shot.webp", None));
assert_eq!(
split_image_theme_fragment("sprite.svg#icon"),
("sprite.svg#icon", None)
);
assert_eq!(
split_image_theme_fragment("a.webp#only-dark/b.webp"),
("a.webp#only-dark/b.webp", None)
);
}
#[test]
fn theme_tagged_image_renders_variant_class_and_strips_fragment() {
let blocks = parse("");
let html = render_to_html(&blocks);
assert!(
html.contains("class=\"lg-img-only-light\""),
"variant class missing in: {html}"
);
assert!(
html.contains("src=\"../shots/dashboard.webp\""),
"fragment not stripped from src in: {html}"
);
assert!(
!html.contains("gh-light-mode-only"),
"URL fragment leaked into the output: {html}"
);
}
#[test]
fn untagged_image_keeps_default_render() {
let blocks = parse("");
let html = render_to_html(&blocks);
assert!(
!html.contains("lg-img-only-"),
"unexpected variant class: {html}"
);
assert!(
html.contains("src=\"../shots/dashboard.webp\""),
"image src missing in: {html}"
);
}
}