Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/theme-option.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ondine': patch
---

Added the `theme` render option, which writes the theme into the SVG as literal values. The output has no `var()` or `color-mix()`, so renderers without CSS custom properties draw it, such as resvg, librsvg, and native SVG parsers. Named, `rgb()`, `rgba()`, hex, and `transparent` colors mix. Only `ondine/compiler` and the `all.fast` module support the option.
5 changes: 4 additions & 1 deletion crates/compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ rust-version.workspace = true
version.workspace = true

[features]
default = ["all"]
default = ["all", "theme"]
# Every diagram type, for native builds and tests.
all = [
"agentflow",
Expand Down Expand Up @@ -82,6 +82,9 @@ wardley = []
xychart = []
# Serializes parsed models as JSON for parity tests. Release modules leave it out.
model = []
# Writes the `theme` render option into the SVG as literal values. Only the speed-optimized `all`
# module includes it, so the size-optimized browser modules leave out the resolve pass.
theme = []

[dev-dependencies]
insta.workspace = true
Expand Down
3 changes: 3 additions & 0 deletions crates/compiler/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub enum Code {
Semantic,
/// The source does not match the diagram's grammar.
Syntax,
/// The options ask for something that this compiler module does not support.
Unsupported,
/// No supported diagram type was detected.
UnsupportedDiagram,
}
Expand All @@ -20,6 +22,7 @@ impl Code {
Self::Limit => "limit",
Self::Semantic => "semantic",
Self::Syntax => "syntax",
Self::Unsupported => "unsupported",
Self::UnsupportedDiagram => "unsupported-diagram",
}
}
Expand Down
20 changes: 19 additions & 1 deletion crates/compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ mod svg {
pub(crate) mod builder;
pub(crate) mod cluster;
pub(crate) mod edges;
#[cfg(feature = "theme")]
pub(crate) mod resolve;
pub(crate) mod root;
pub(crate) mod shapes;
pub(crate) mod theme;
Expand Down Expand Up @@ -171,6 +173,13 @@ pub fn render(source: &str, options: &str, now: f64) -> Result<Rendered, Error>
};

let drawn = (diagram.render)(&input, &options, &url)?;
#[cfg(feature = "theme")]
let document = match &options.theme {
Some(theme) => svg::resolve::resolve(&drawn.svg, theme)?,
None => drawn.svg,
};
#[cfg(not(feature = "theme"))]
let document = drawn.svg;
let mut rendered = Rendered {
height: drawn.height,
links: drawn
Expand All @@ -182,7 +191,7 @@ pub fn render(source: &str, options: &str, now: f64) -> Result<Rendered, Error>
internal: link.internal,
})
.collect(),
svg: drawn.svg,
svg: document,
r#type,
width: drawn.width,
};
Expand Down Expand Up @@ -287,6 +296,15 @@ fn options_from_json(json: &str) -> Result<svg::root::RenderOptions, Error> {
"outputLimit" => options.output_limit = number(value),
"padding" => options.padding = number(value),
"responsive" => options.responsive = boolean(value),
#[cfg(feature = "theme")]
"theme" => options.theme = svg::resolve::properties(value),
#[cfg(not(feature = "theme"))]
"theme" => {
return Err(Error::new(
Code::Unsupported,
"This module does not support the theme option. Use ondine/compiler.",
));
}
_ => {}
}
}
Expand Down
Loading