mmdflux 2.5.0

Render Mermaid diagrams as Unicode text, ASCII, SVG, and MMDS JSON.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! Serde-friendly config input for JSON-based consumers (Wasm, API, etc.).
//!
//! [`RuntimeConfigInput`] mirrors [`RenderConfig`] but uses `Option<String>`
//! for enum fields so that it can be deserialized from camelCase JSON.
//! Call [`RuntimeConfigInput::into_render_config`] to validate and convert.

use serde::Deserialize;

use crate::engines::graph::{EngineAlgorithmId, Ranker, SubgraphTitleMargin};
use crate::errors::RenderError;
use crate::format::{
    ColorWhen, Curve, EdgePreset, OutputFormat, RoutingStyle, normalize_enum_token,
};
use crate::graph::GeometryLevel;
use crate::graph::measure::{
    DEFAULT_GRAPH_FONT_FAMILY, DEFAULT_PROPORTIONAL_FONT_SIZE, font_family_compare_key,
    validate_text_metrics_profile_id,
};
use crate::runtime::config::{GraphTextStyleConfig, RenderConfig, SvgThemeConfig, SvgThemeMode};
use crate::simplification::PathSimplification;

/// Serde-friendly render config accepted from JSON callers.
///
/// All enum-valued fields are `Option<String>` so that consumers can pass
/// normalized or user-typed values. Conversion to the typed [`RenderConfig`]
/// happens in [`into_render_config`](Self::into_render_config).
#[derive(Debug, Default, Deserialize)]
#[serde(default, rename_all = "camelCase", deny_unknown_fields)]
pub struct RuntimeConfigInput {
    pub layout_engine: Option<String>,
    pub cluster_ranksep: Option<f64>,
    pub padding: Option<usize>,
    pub svg_scale: Option<f64>,
    pub svg_theme: Option<SvgThemeConfigInput>,
    pub edge_preset: Option<String>,
    pub routing_style: Option<String>,
    pub curve: Option<String>,
    pub edge_radius: Option<f64>,
    pub svg_diagram_padding: Option<f64>,
    pub svg_node_padding_x: Option<f64>,
    pub svg_node_padding_y: Option<f64>,
    pub font_metrics_profile: Option<String>,
    pub font_family: Option<String>,
    pub font_size: Option<f64>,
    pub theme_variables: Option<ThemeVariablesInput>,
    pub show_ids: Option<bool>,
    pub color: Option<String>,
    pub geometry_level: Option<String>,
    pub path_simplification: Option<String>,
    pub layout: Option<LayoutConfigInput>,
}

/// Serde-friendly layout config nested inside [`RuntimeConfigInput`].
#[derive(Debug, Default, Deserialize)]
#[serde(default, rename_all = "camelCase", deny_unknown_fields)]
pub struct LayoutConfigInput {
    pub node_sep: Option<f64>,
    pub edge_sep: Option<f64>,
    pub rank_sep: Option<f64>,
    pub margin: Option<f64>,
    pub ranker: Option<String>,
    /// Subgraph title margin in pixels, mirroring Mermaid's
    /// `flowchart.subGraphTitleMargin: { top, bottom }`. Both fields
    /// default to `0` when the object is provided. Omit entirely (or set
    /// to `null`) to keep mmdflux's measurement-driven default.
    ///
    /// Accepts Mermaid's `subGraphTitleMargin` (capital `G`) alongside the
    /// camelCase `subgraphTitleMargin` form produced by `rename_all`.
    #[serde(alias = "subGraphTitleMargin")]
    pub subgraph_title_margin: Option<SubgraphTitleMarginInput>,
}

/// Serde-friendly subgraph title margin, mirroring Mermaid's
/// `flowchart.subGraphTitleMargin: { top, bottom }`.
#[derive(Debug, Default, Deserialize)]
#[serde(default, rename_all = "camelCase", deny_unknown_fields)]
pub struct SubgraphTitleMarginInput {
    /// Pixels above the subgraph title (Mermaid `subGraphTitleMargin.top`).
    pub top: Option<f64>,
    /// Pixels below the subgraph title (Mermaid `subGraphTitleMargin.bottom`).
    pub bottom: Option<f64>,
}

impl SubgraphTitleMarginInput {
    fn into_config(self) -> Result<SubgraphTitleMargin, RenderError> {
        let top = self.top.unwrap_or(0.0);
        let bottom = self.bottom.unwrap_or(0.0);
        for (label, value) in [("top", top), ("bottom", bottom)] {
            if !value.is_finite() {
                return Err(RenderError {
                    message: format!("subGraphTitleMargin.{label} must be a finite number"),
                });
            }
            if value < 0.0 {
                return Err(RenderError {
                    message: format!(
                        "subGraphTitleMargin.{label} must be non-negative, got {value}"
                    ),
                });
            }
        }
        Ok(SubgraphTitleMargin { top, bottom })
    }
}

/// Serde-friendly SVG theme config nested inside [`RuntimeConfigInput`].
#[derive(Debug, Default, Deserialize)]
#[serde(default, rename_all = "camelCase", deny_unknown_fields)]
pub struct SvgThemeConfigInput {
    pub name: Option<String>,
    pub mode: Option<String>,
    pub bg: Option<String>,
    pub fg: Option<String>,
    pub line: Option<String>,
    pub accent: Option<String>,
    pub muted: Option<String>,
    pub surface: Option<String>,
    pub border: Option<String>,
}

/// Narrow Mermaid-compatible themeVariables subset for graph font style.
#[derive(Debug, Default, Deserialize)]
#[serde(default, rename_all = "camelCase", deny_unknown_fields)]
pub struct ThemeVariablesInput {
    pub font_family: Option<String>,
    pub font_size: Option<ThemeFontSizeInput>,
}

/// Mermaid-compatible font-size alias accepted under themeVariables.
#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum ThemeFontSizeInput {
    Number(f64),
    String(String),
}

impl RuntimeConfigInput {
    /// Validate and convert into a typed [`RenderConfig`].
    pub fn into_render_config(self) -> Result<RenderConfig, RenderError> {
        let mut config = RenderConfig {
            cluster_ranksep: self.cluster_ranksep,
            padding: self.padding,
            svg_scale: self.svg_scale,
            edge_radius: self.edge_radius,
            svg_diagram_padding: self.svg_diagram_padding,
            svg_node_padding_x: self.svg_node_padding_x,
            svg_node_padding_y: self.svg_node_padding_y,
            ..RenderConfig::default()
        };

        if let Some(svg_theme) = self.svg_theme {
            config.svg_theme = Some(svg_theme.into_svg_theme_config()?);
        }
        if let Some(font_metrics_profile) = self.font_metrics_profile {
            validate_text_metrics_profile_id(&font_metrics_profile).map_err(|error| {
                RenderError {
                    message: error.to_string(),
                }
            })?;
            config.font_metrics_profile = Some(font_metrics_profile);
        }
        config.graph_text_style =
            graph_text_style_from_input(self.font_family, self.font_size, self.theme_variables)?;
        if let Some(layout_engine) = self.layout_engine {
            config.layout_engine = Some(EngineAlgorithmId::parse(&layout_engine)?);
        }
        if let Some(show_ids) = self.show_ids {
            config.show_ids = show_ids;
        }
        if let Some(color) = self.color {
            config.text_color_mode = color.parse::<ColorWhen>()?.resolve(false);
        }
        if let Some(edge_preset) = self.edge_preset {
            config.edge_preset = Some(EdgePreset::parse(&edge_preset)?);
        }
        if let Some(routing_style) = self.routing_style {
            config.routing_style = Some(routing_style.parse::<RoutingStyle>()?);
        }
        if let Some(curve) = self.curve {
            config.curve = Some(curve.parse::<Curve>()?);
        }
        if let Some(geometry_level) = self.geometry_level {
            config.geometry_level = geometry_level.parse::<GeometryLevel>()?;
        }
        if let Some(path_simplification) = self.path_simplification {
            config.path_simplification = path_simplification.parse::<PathSimplification>()?;
        }
        if let Some(layout) = self.layout {
            if let Some(node_sep) = layout.node_sep {
                config.layout.node_sep = node_sep;
            }
            if let Some(edge_sep) = layout.edge_sep {
                config.layout.edge_sep = edge_sep;
            }
            if let Some(rank_sep) = layout.rank_sep {
                config.layout.rank_sep = rank_sep;
            }
            if let Some(margin) = layout.margin {
                config.layout.margin = margin;
            }
            if let Some(ranker) = layout.ranker {
                config.layout.ranker = parse_ranker(&ranker)?;
            }
            if let Some(subgraph_title_margin) = layout.subgraph_title_margin {
                config.layout.subgraph_title_margin = Some(subgraph_title_margin.into_config()?);
            }
        }

        Ok(config)
    }
}

fn graph_text_style_from_input(
    font_family: Option<String>,
    font_size: Option<f64>,
    theme_variables: Option<ThemeVariablesInput>,
) -> Result<Option<GraphTextStyleConfig>, RenderError> {
    let theme_family = theme_variables
        .as_ref()
        .and_then(|theme| theme.font_family.as_ref());
    let theme_size = theme_variables
        .as_ref()
        .and_then(|theme| theme.font_size.as_ref());

    if font_family.is_none()
        && font_size.is_none()
        && theme_family.is_none()
        && theme_size.is_none()
    {
        return Ok(None);
    }

    let canonical_family = font_family
        .as_ref()
        .map(|value| normalize_font_family("fontFamily", value))
        .transpose()?;
    let theme_family = theme_family
        .map(|value| normalize_font_family("themeVariables.fontFamily", value))
        .transpose()?;
    if let (Some(canonical), Some(theme)) = (&canonical_family, &theme_family)
        && canonical.compare_key != theme.compare_key
    {
        return Err(RenderError {
            message: "conflicting fontFamily and themeVariables.fontFamily".to_string(),
        });
    }

    let canonical_size = font_size
        .map(|value| validate_font_size_px("fontSize", value))
        .transpose()?;
    let theme_size = theme_variables
        .and_then(|theme| theme.font_size)
        .map(theme_font_size_to_px)
        .transpose()?;
    if let (Some(canonical), Some(theme)) = (canonical_size, theme_size)
        && (canonical - theme).abs() > f64::EPSILON
    {
        return Err(RenderError {
            message: "conflicting fontSize and themeVariables.fontSize".to_string(),
        });
    }

    let font_family = match canonical_family.or(theme_family) {
        Some(value) => value.display,
        None => DEFAULT_GRAPH_FONT_FAMILY.to_string(),
    };
    let font_size_px = match canonical_size.or(theme_size) {
        Some(value) => value,
        None => DEFAULT_PROPORTIONAL_FONT_SIZE,
    };

    Ok(Some(GraphTextStyleConfig {
        font_family,
        font_size_px,
    }))
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct NormalizedFontFamily {
    display: String,
    compare_key: Vec<String>,
}

fn normalize_font_family(field: &str, value: &str) -> Result<NormalizedFontFamily, RenderError> {
    let trimmed = value.trim();
    let compare_key = font_family_compare_key_for_field(field, trimmed)?;
    Ok(NormalizedFontFamily {
        display: trimmed.to_string(),
        compare_key,
    })
}

fn validate_font_size_px(field: &str, value: f64) -> Result<f64, RenderError> {
    if value.is_finite() && value > 0.0 {
        Ok(value)
    } else {
        Err(RenderError {
            message: format!("{field} must be a finite positive number"),
        })
    }
}

fn theme_font_size_to_px(input: ThemeFontSizeInput) -> Result<f64, RenderError> {
    match input {
        ThemeFontSizeInput::Number(value) => {
            validate_font_size_px("themeVariables.fontSize", value)
        }
        ThemeFontSizeInput::String(value) => parse_theme_font_size_string(&value),
    }
}

fn parse_theme_font_size_string(value: &str) -> Result<f64, RenderError> {
    let field = "themeVariables.fontSize";
    let trimmed = value.trim();
    let numeric = if trimmed
        .get(trimmed.len().saturating_sub(2)..)
        .is_some_and(|suffix| suffix.eq_ignore_ascii_case("px"))
    {
        trimmed[..trimmed.len() - 2].trim_end()
    } else {
        trimmed
    };

    if !is_plain_decimal_number(numeric) {
        return Err(RenderError {
            message: format!("{field} must be a positive number or px value"),
        });
    }

    let value = numeric.parse::<f64>().map_err(|_| RenderError {
        message: format!("{field} must be a positive number or px value"),
    })?;
    validate_font_size_px(field, value)
}

fn is_plain_decimal_number(value: &str) -> bool {
    let Some((head, tail)) = value.split_once('.') else {
        return !value.is_empty() && value.chars().all(|ch| ch.is_ascii_digit());
    };

    !head.is_empty()
        && !tail.is_empty()
        && head.chars().all(|ch| ch.is_ascii_digit())
        && tail.chars().all(|ch| ch.is_ascii_digit())
        && !tail.contains('.')
}

fn font_family_compare_key_for_field(field: &str, value: &str) -> Result<Vec<String>, RenderError> {
    font_family_compare_key(value).map_err(|message| RenderError {
        message: format!("{field} {message}"),
    })
}

impl SvgThemeConfigInput {
    fn into_svg_theme_config(self) -> Result<SvgThemeConfig, RenderError> {
        let mode = match self.mode {
            Some(mode) => parse_svg_theme_mode(&mode)?,
            None => SvgThemeMode::default(),
        };

        Ok(SvgThemeConfig {
            name: self.name,
            mode,
            bg: self.bg,
            fg: self.fg,
            line: self.line,
            accent: self.accent,
            muted: self.muted,
            surface: self.surface,
            border: self.border,
        })
    }
}

fn parse_ranker(value: &str) -> Result<Ranker, RenderError> {
    match normalize_enum_token(value).as_str() {
        "network-simplex" | "networksimplex" => Ok(Ranker::NetworkSimplex),
        "longest-path" | "longestpath" => Ok(Ranker::LongestPath),
        _ => Err(RenderError {
            message: format!("unknown ranker: {value}"),
        }),
    }
}

fn parse_svg_theme_mode(value: &str) -> Result<SvgThemeMode, RenderError> {
    match normalize_enum_token(value).as_str() {
        "static" => Ok(SvgThemeMode::Static),
        "dynamic" => Ok(SvgThemeMode::Dynamic),
        _ => Err(RenderError {
            message: format!("unknown svg theme mode: {value} (expected static or dynamic)"),
        }),
    }
}

/// The default SVG layout engine (flux-layered).
pub fn default_svg_engine() -> EngineAlgorithmId {
    EngineAlgorithmId::FLUX_LAYERED
}

/// Apply SVG surface defaults for flux-layered engine.
///
/// When the output format is SVG and no edge styling is configured, this
/// applies `SmoothStep` as the default edge preset for the flux-layered engine.
///
/// The `force_engine` parameter controls whether to force the default SVG
/// engine when none is set:
/// - `true`: Wasm behavior — always set flux-layered for SVG.
/// - `false`: CLI behavior — leave engine unset (auto-detect later).
pub fn apply_svg_surface_defaults(
    format: OutputFormat,
    config: &mut RenderConfig,
    force_engine: bool,
) {
    if !matches!(format, OutputFormat::Svg) {
        return;
    }

    if force_engine && config.layout_engine.is_none() {
        config.layout_engine = Some(default_svg_engine());
    }

    if config.edge_preset.is_some() || config.routing_style.is_some() || config.curve.is_some() {
        return;
    }

    if config.layout_engine.unwrap_or(default_svg_engine()) == default_svg_engine() {
        config.edge_preset = Some(EdgePreset::SmoothStep);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn font_family_compare_key_normalizes_quotes_case_and_spacing() {
        let first = font_family_compare_key_for_field(
            "fontFamily",
            r#" "Trebuchet   MS" , Verdana, ARIAL , sans-serif "#,
        )
        .unwrap();
        let second = font_family_compare_key_for_field(
            "fontFamily",
            r#"trebuchet ms,verdana,arial,sans-serif"#,
        )
        .unwrap();

        assert_eq!(first, second);
        assert_eq!(
            first,
            vec!["trebuchet ms", "verdana", "arial", "sans-serif"]
        );
    }

    #[test]
    fn font_family_compare_key_rejects_empty_tokens() {
        let err = font_family_compare_key_for_field("fontFamily", "Inter, , Arial").unwrap_err();

        assert!(err.message.contains("fontFamily"), "{err}");
    }

    #[test]
    fn validate_font_size_rejects_non_finite_values() {
        for value in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
            let err = validate_font_size_px("fontSize", value).unwrap_err();
            assert!(err.message.contains("fontSize"), "{err}");
        }
    }
}