dmc-transform 0.3.6

Transform pipeline and built-in transformers for the dmc compiler
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
//! Pre-rendered syntax highlighter. See `transformers/pretty-code.md`
//! for full docs.

use crate::config::{MultiThemeStrategy, PrettyCodeOptions, PrettyCodeTheme};
use crate::pipeline::Transformer;
use crate::visit::{NodeAction, Visitor, walk_root};
use dmc_diagnostic::Code;
use dmc_diagnostic::metadata::SourceMeta;
use dmc_highlight::{SyntaxBundle, highlight_code_multi, list_bundled_themes};
use dmc_parser::ast::*;
use duck_diagnostic::{DiagnosticEngine, diag};
/// Code-block syntax highlighter. Holds the resolved theme list (single
/// or multi-mode), the mode whose colors fill the unprefixed CSS attrs,
/// and every DOM-shape knob from [`PrettyCodeOptions`].
#[derive(Debug, Clone)]
pub struct PrettyCode {
  /// Ordered `(mode, bundled_theme_name)` pairs. For single-theme mode,
  /// `mode` is the empty string and the vec has one entry.
  themes: Vec<(String, String)>,
  /// Mode whose colors are emitted as plain `color` / `background-color`.
  /// Empty string for single-theme.
  default_mode: String,
  /// Resolved DOM-shape options (defaults applied).
  shape: ShapeOpts,
}

/// Process-wide dedupe set for "theme not bundled" warnings. The
/// pipeline rebuilds a fresh `PrettyCode` per document, so a per-
/// instance gate would still fire once per doc (300+ duplicates on a
/// real docs build). Keyed on `theme_name` so each missing theme warns
/// exactly once for the whole process.
fn warn_once_for_unbundled(theme: &str) -> bool {
  use std::collections::HashSet;
  use std::sync::Mutex;
  use std::sync::OnceLock;
  static SEEN: OnceLock<Mutex<HashSet<String>>> = OnceLock::new();
  let mu = SEEN.get_or_init(|| Mutex::new(HashSet::new()));
  let mut g = mu.lock().expect("pretty-code dedupe mutex poisoned");
  g.insert(theme.to_string())
}

/// Resolved DOM-shape options. All fields are post-default so render
/// code can read them without `Option` chasing on every code block.
#[derive(Debug, Clone)]
struct ShapeOpts {
  keep_raw_string: bool,
  fragment_wrapper: bool,
  line_class: String,
  highlighted_line_attr: String,
  default_language: String,
  fallback_to_plaintext: bool,
  render_title: bool,
  include_data_language: bool,
  include_pre_background: bool,
  skip_languages: Vec<String>,
  tab_size: Option<u32>,
  multi_theme_strategy: MultiThemeStrategy,
}

impl ShapeOpts {
  fn from_options(o: &PrettyCodeOptions) -> Self {
    Self {
      keep_raw_string: o.keep_raw_string.unwrap_or(true),
      fragment_wrapper: o.fragment_wrapper.unwrap_or(true),
      line_class: o.line_class.clone().unwrap_or_else(|| "line".into()),
      highlighted_line_attr: o.highlighted_line_attr.clone().unwrap_or_else(|| "data-dmc-line-highlighted".into()),
      default_language: o.default_language.clone().unwrap_or_else(|| "plaintext".into()),
      fallback_to_plaintext: o.fallback_to_plaintext.unwrap_or(true),
      render_title: o.render_title.unwrap_or(true),
      include_data_language: o.include_data_language.unwrap_or(true),
      include_pre_background: o.include_pre_background.unwrap_or(true),
      skip_languages: o.skip_languages.clone(),
      tab_size: o.tab_size,
      multi_theme_strategy: o.multi_theme_strategy.unwrap_or_default(),
    }
  }
}

impl Default for PrettyCode {
  fn default() -> Self {
    Self::from_options(&PrettyCodeOptions::default())
  }
}

impl PrettyCode {
  /// Single-theme constructor.
  pub fn new(theme: impl Into<String>) -> Self {
    Self::from_options(&PrettyCodeOptions { theme: PrettyCodeTheme::Single(theme.into()), ..Default::default() })
  }

  /// Resolve `PrettyCodeOptions` into a runtime `PrettyCode`. Picks
  /// `default_mode` from explicit config, else `"dark"` if present, else
  /// the first key in the theme map.
  pub fn from_options(opts: &PrettyCodeOptions) -> Self {
    let shape = ShapeOpts::from_options(opts);
    match &opts.theme {
      PrettyCodeTheme::Single(name) => {
        Self { themes: vec![(String::new(), name.clone())], default_mode: String::new(), shape }
      },
      PrettyCodeTheme::Multi(map) => {
        // Order modes the way shiki/rehype-pretty-code does: light first,
        // then dark, then any other custom mode in alphabetical order. The
        // user-facing data is theme-key -> theme-name, so sorting at the
        // boundary lets consumers diff against shiki output without having
        // to maintain insertion-ordered config maps in Rust.
        let mut themes: Vec<(String, String)> = map.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
        themes.sort_by(|a, b| {
          fn rank(k: &str) -> u8 {
            match k {
              "light" => 0,
              "dark" => 1,
              _ => 2,
            }
          }
          rank(&a.0).cmp(&rank(&b.0)).then_with(|| a.0.cmp(&b.0))
        });
        let default_mode = opts
          .default_mode
          .clone()
          .filter(|m| map.contains_key(m))
          .or_else(|| if map.contains_key("dark") { Some("dark".into()) } else { None })
          .or_else(|| themes.first().map(|(k, _)| k.clone()))
          .unwrap_or_default();
        Self { themes, default_mode, shape }
      },
    }
  }
}

impl Transformer for PrettyCode {
  fn name(&self) -> &str {
    "pretty-code"
  }
  fn transform(&self, doc: &mut Document, _meta: &SourceMeta, engine: &mut DiagnosticEngine<Code>) {
    // Warn once per process for every misconfigured theme. The pipeline
    // rebuilds a fresh `PrettyCode` per doc, so a per-instance gate
    // would still fire 300+ times on a typical docs build.
    let bundled = list_bundled_themes();
    for (mode, theme) in &self.themes {
      if bundled.iter().any(|t| t == theme) {
        continue;
      }
      if !warn_once_for_unbundled(theme) {
        continue;
      }
      let mode_label = if mode.is_empty() { "default".to_string() } else { mode.clone() };
      let hint = if bundled.is_empty() {
        "no themes are bundled - run a clean build".to_string()
      } else {
        format!("bundled themes: {}", bundled.join(", "))
      };
      engine.emit(
        diag!(
          Code::ThemeNotBundled,
          format!(
            "pretty-code: theme `{theme}` (mode `{mode_label}`) is not bundled; falling back to the first bundled theme",
          ))
        .with_help(hint),
      );
    }
    let mut v = Apply { themes: &self.themes, default_mode: &self.default_mode, shape: &self.shape };
    walk_root(&mut doc.children, &mut v);
  }
}

struct Apply<'a> {
  themes: &'a [(String, String)],
  default_mode: &'a str,
  shape: &'a ShapeOpts,
}

impl Visitor for Apply<'_> {
  fn visit_node(&mut self, node: &mut Node) -> NodeAction {
    let Node::CodeBlock(cb) = node else { return NodeAction::Keep };
    // Mermaid blocks are owned by the `mermaid` transformer.
    if cb.lang.as_deref() == Some("mermaid") {
      return NodeAction::Keep;
    }
    // Skip user-listed languages (passed through unchanged for downstream
    // consumers). Bare strings or comparisons against `lang` only.
    if let Some(lang) = cb.lang.as_deref()
      && self.shape.skip_languages.iter().any(|l| l == lang)
    {
      return NodeAction::Keep;
    }
    let meta = cb.meta.as_deref().map(parse_meta).unwrap_or_default();
    let rendered = render_code_block(cb, &meta, self.themes, self.default_mode, self.shape);
    match rendered {
      Some(node) => NodeAction::Replace(vec![Node::JsxElement(node)]),
      None => NodeAction::Keep,
    }
  }
}

#[derive(Default, Debug)]
struct CodeMeta {
  title: Option<String>,
  line_marks: Vec<LineMark>,
}

#[derive(Debug, Clone, Copy)]
struct LineMark {
  start: u32,
  end: u32,
}

impl LineMark {
  fn contains(&self, line: u32) -> bool {
    line >= self.start && line <= self.end
  }
}

/// Parse meta strings like `title="x" {1,3-5}` into a typed `CodeMeta`.
/// Tolerant: any malformed segment is silently skipped.
fn parse_meta(raw: &str) -> CodeMeta {
  let mut m = CodeMeta::default();
  if let Some(start) = raw.find("title=\"") {
    let after = &raw[start + 7..];
    if let Some(end) = after.find('"') {
      m.title = Some(after[..end].to_string());
    }
  }
  if let Some(start) = raw.find('{')
    && let Some(end) = raw[start + 1..].find('}')
  {
    let body = &raw[start + 1..start + 1 + end];
    for tok in body.split(',') {
      let tok = tok.trim();
      if tok.is_empty() {
        continue;
      }
      if let Some((a, b)) = tok.split_once('-') {
        if let (Ok(a), Ok(b)) = (a.trim().parse::<u32>(), b.trim().parse::<u32>()) {
          m.line_marks.push(LineMark { start: a.min(b), end: a.max(b) });
        }
      } else if let Ok(n) = tok.parse::<u32>() {
        m.line_marks.push(LineMark { start: n, end: n });
      }
    }
  }
  m
}

/// Render a fenced code block as velite's `rehype-pretty-code` shape:
///
/// ```html
/// <div data-dmc-fragment="">
///   <div data-theme="<mode>"> <!-- one per theme -->
///     <pre __dmcRaw__="..." data-language="..." data-theme="<mode>">
///       <code data-language="..." data-theme="<mode>">
///         <span class="line">
///           <span style="color:#XXX">token</span>
///           ...
///         </span>
///       </code>
///     </pre>
///   </div>
///   ...
/// </div>
/// ```
///
/// Each theme gets its own `<pre>` with solid `color:#XXX` per token (no
/// shared CSS-var fallback). `__dmcRaw__` lets the consumer's `<pre>`
/// override render the Copy button. The outer `<div data-theme>` wrapper
/// is what consumer CSS uses to show/hide per active theme.
fn render_code_block(
  cb: &CodeBlock,
  meta: &CodeMeta,
  themes: &[(String, String)],
  default_mode: &str,
  shape: &ShapeOpts,
) -> Option<JsxElement> {
  let theme_names: Vec<&str> = themes.iter().map(|(_, n)| n.as_str()).collect();
  // Resolve language: explicit fence lang > default. Borrow when possible
  // so the common path (lang on fence, no tab expansion) does zero allocs.
  let resolved_lang: &str = match cb.lang.as_deref() {
    Some(l) => l,
    None => shape.default_language.as_str(),
  };
  if !shape.fallback_to_plaintext
    && cb.lang.is_some()
    && dmc_highlight::SyntaxBundle::get().syntaxes.find_syntax_by_token(resolved_lang).is_none()
  {
    return None;
  }
  // Optional tab->spaces expansion before highlighting. Borrow when off
  // (the typical case) so we don't clone every block's body.
  let expanded;
  let source: &str = match shape.tab_size {
    Some(n) if n > 0 => {
      expanded = cb.value.replace('\t', &" ".repeat(n as usize));
      expanded.as_str()
    },
    _ => cb.value.as_str(),
  };
  let lines = highlight_code_multi(source, Some(resolved_lang), &theme_names);
  let span = cb.span.clone();

  let bundle = SyntaxBundle::get();
  let foregrounds: Vec<Option<dmc_highlight::Color>> =
    themes.iter().map(|(_, name)| bundle.themes.themes.get(name).and_then(|t| t.settings.foreground)).collect();
  let backgrounds: Vec<Option<dmc_highlight::Color>> =
    themes.iter().map(|(_, name)| bundle.themes.themes.get(name).and_then(|t| t.settings.background)).collect();

  // Pick layout: single tree with `--dmc-{mode}` CSS vars (default,
  // ~25% faster), or one `<pre>` per theme (velite parity, ~2x nodes).
  // Single-theme always uses the simple per-theme path - there's only
  // one tree either way.
  let theme_blocks: Vec<Node> = if themes.len() > 1 && shape.multi_theme_strategy == MultiThemeStrategy::CssVars {
    vec![Node::JsxElement(render_css_vars_pre(
      cb,
      meta,
      &lines,
      themes,
      default_mode,
      &foregrounds,
      &backgrounds,
      resolved_lang,
      shape,
      span.clone(),
    ))]
  } else {
    themes
      .iter()
      .enumerate()
      .map(|(theme_idx, (mode, _))| {
        let fg_default = foregrounds[theme_idx];
        Node::JsxElement(render_theme_pre(
          cb,
          meta,
          &lines,
          theme_idx,
          mode,
          fg_default,
          resolved_lang,
          shape,
          span.clone(),
        ))
      })
      .collect()
  };

  let mut fragment_children: Vec<Node> = Vec::new();
  if shape.render_title
    && let Some(title) = &meta.title
  {
    let mut figcaption_attrs =
      vec![JsxAttr { name: "data-dmc-title".into(), value: JsxAttrValue::String(String::new()), span: span.clone() }];
    if shape.include_data_language {
      figcaption_attrs.push(JsxAttr {
        name: "data-language".into(),
        value: JsxAttrValue::String(cb.lang.clone().unwrap_or_default()),
        span: span.clone(),
      });
    }
    fragment_children.push(Node::JsxElement(JsxElement {
      name: "figcaption".into(),
      attrs: figcaption_attrs,
      children: vec![Node::Text(Text { value: title.clone(), span: span.clone() })],
      span: span.clone(),
    }));
  }
  fragment_children.extend(theme_blocks);

  // Without the fragment wrapper there's no single root to return -
  // emit a fragment-style `<>` if title is present, else just one
  // synthetic `<div>` wrapping the panes (keeps the AST single-rooted).
  if !shape.fragment_wrapper {
    return Some(JsxElement { name: "div".into(), attrs: Vec::new(), children: fragment_children, span });
  }

  Some(JsxElement {
    name: "div".into(),
    attrs: vec![JsxAttr {
      name: "data-dmc-fragment".into(),
      value: JsxAttrValue::String(String::new()),
      span: span.clone(),
    }],
    children: fragment_children,
    span,
  })
}

/// Build the `<pre><code>...</code></pre>` for one theme. Honors every
/// shape knob: line class, highlighted-line attribute, data-language
/// inclusion, raw-string preservation.
#[allow(clippy::too_many_arguments)]
fn render_theme_pre(
  cb: &CodeBlock,
  meta: &CodeMeta,
  lines: &[Vec<dmc_highlight::MultiToken<'_>>],
  theme_idx: usize,
  mode: &str,
  fg_default: Option<dmc_highlight::Color>,
  resolved_lang: &str,
  shape: &ShapeOpts,
  span: duck_diagnostic::Span,
) -> JsxElement {
  let mut line_children: Vec<Node> = Vec::with_capacity(lines.len());
  for (line_i, tokens) in lines.iter().enumerate() {
    let line_no = (line_i + 1) as u32;
    // Coalesce adjacent tokens with identical style - keeps DOM tight.
    let mut runs: Vec<(String, String)> = Vec::with_capacity(tokens.len());
    for tok in tokens.iter() {
      let style = single_theme_token_style(tok, theme_idx, fg_default);
      match runs.last_mut() {
        Some(last) if last.0 == style => last.1.push_str(tok.text),
        _ => runs.push((style, tok.text.to_string())),
      }
    }
    // Fold leading-whitespace runs into the first styled token so
    // indented lines don't emit an empty `<span>"   "</span>` sibling.
    coalesce_leading_whitespace(&mut runs);
    let mut tok_children: Vec<Node> = Vec::with_capacity(runs.len());
    for (style, text) in runs {
      let text_node = Node::Text(Text { value: text, span: span.clone() });
      let attrs = if style.is_empty() {
        Vec::new()
      } else {
        vec![JsxAttr { name: "style".into(), value: JsxAttrValue::String(style), span: span.clone() }]
      };
      tok_children.push(Node::JsxElement(JsxElement {
        name: "span".into(),
        attrs,
        children: vec![text_node],
        span: span.clone(),
      }));
    }
    let mut line_attrs =
      vec![JsxAttr { name: "class".into(), value: JsxAttrValue::String(shape.line_class.clone()), span: span.clone() }];
    if meta.line_marks.iter().any(|m| m.contains(line_no)) {
      line_attrs.push(JsxAttr {
        name: shape.highlighted_line_attr.clone(),
        value: JsxAttrValue::String(String::new()),
        span: span.clone(),
      });
    }
    line_children.push(Node::JsxElement(JsxElement {
      name: "span".into(),
      attrs: line_attrs,
      children: tok_children,
      span: span.clone(),
    }));
  }

  let mut code_attrs: Vec<JsxAttr> = Vec::new();
  if shape.include_data_language {
    code_attrs.push(JsxAttr {
      name: "data-language".into(),
      value: JsxAttrValue::String(resolved_lang.to_string()),
      span: span.clone(),
    });
  }
  code_attrs.push(JsxAttr {
    name: "data-theme".into(),
    value: JsxAttrValue::String(mode.to_string()),
    span: span.clone(),
  });
  let code_el = Node::JsxElement(JsxElement {
    name: "code".into(),
    attrs: code_attrs,
    children: line_children,
    span: span.clone(),
  });

  let mut pre_attrs: Vec<JsxAttr> = Vec::new();
  if shape.keep_raw_string {
    // Consumer `<PreBlock>` reads `__dmcRaw__` for the Copy button.
    pre_attrs.push(JsxAttr {
      name: "__dmcRaw__".into(),
      value: JsxAttrValue::String(cb.value.clone()),
      span: span.clone(),
    });
  }
  if shape.include_data_language {
    pre_attrs.push(JsxAttr {
      name: "data-language".into(),
      value: JsxAttrValue::String(resolved_lang.to_string()),
      span: span.clone(),
    });
  }
  pre_attrs.push(JsxAttr {
    name: "data-theme".into(),
    value: JsxAttrValue::String(mode.to_string()),
    span: span.clone(),
  });

  JsxElement { name: "pre".into(), attrs: pre_attrs, children: vec![code_el], span }
}

/// Multi-theme css-vars renderer. ONE `<pre><code>` tree carrying every
/// theme via `--dmc-{mode}` CSS custom properties. Consumer CSS swaps
/// modes by overriding `color` to `var(--dmc-{active-mode})`.
///
/// The `<pre>` carries default `color` / `background-color` from the
/// primary mode, plus `--dmc-{mode}:#hex` for every mode (foreground)
/// and `--dmc-{mode}-bg:#hex` (background). Each styled token carries
/// `--dmc-{mode}:#hex` for its color, plus per-mode font-style if any.
/// Tokens whose color matches the default foreground emit no style.
#[allow(clippy::too_many_arguments)]
fn render_css_vars_pre(
  cb: &CodeBlock,
  meta: &CodeMeta,
  lines: &[Vec<dmc_highlight::MultiToken<'_>>],
  themes: &[(String, String)],
  default_mode: &str,
  foregrounds: &[Option<dmc_highlight::Color>],
  backgrounds: &[Option<dmc_highlight::Color>],
  resolved_lang: &str,
  shape: &ShapeOpts,
  span: duck_diagnostic::Span,
) -> JsxElement {
  let default_idx = themes.iter().position(|(m, _)| m == default_mode).unwrap_or(0);

  let mut line_children: Vec<Node> = Vec::with_capacity(lines.len());
  for (line_i, tokens) in lines.iter().enumerate() {
    let line_no = (line_i + 1) as u32;
    // Coalesce adjacent tokens with identical multi-theme style runs.
    let mut runs: Vec<(String, String)> = Vec::with_capacity(tokens.len());
    for tok in tokens.iter() {
      let style = css_vars_token_style(tok, themes, foregrounds);
      match runs.last_mut() {
        Some(last) if last.0 == style => last.1.push_str(tok.text),
        _ => runs.push((style, tok.text.to_string())),
      }
    }
    // Fold leading-whitespace runs into the first styled token.
    coalesce_leading_whitespace(&mut runs);
    let mut tok_children: Vec<Node> = Vec::with_capacity(runs.len());
    for (style, text) in runs {
      let text_node = Node::Text(Text { value: text, span: span.clone() });
      let attrs = if style.is_empty() {
        Vec::new()
      } else {
        vec![JsxAttr { name: "style".into(), value: JsxAttrValue::String(style), span: span.clone() }]
      };
      tok_children.push(Node::JsxElement(JsxElement {
        name: "span".into(),
        attrs,
        children: vec![text_node],
        span: span.clone(),
      }));
    }
    let mut line_attrs =
      vec![JsxAttr { name: "class".into(), value: JsxAttrValue::String(shape.line_class.clone()), span: span.clone() }];
    if meta.line_marks.iter().any(|m| m.contains(line_no)) {
      line_attrs.push(JsxAttr {
        name: shape.highlighted_line_attr.clone(),
        value: JsxAttrValue::String(String::new()),
        span: span.clone(),
      });
    }
    line_children.push(Node::JsxElement(JsxElement {
      name: "span".into(),
      attrs: line_attrs,
      children: tok_children,
      span: span.clone(),
    }));
  }

  let mut code_attrs: Vec<JsxAttr> = Vec::new();
  if shape.include_data_language {
    code_attrs.push(JsxAttr {
      name: "data-language".into(),
      value: JsxAttrValue::String(resolved_lang.to_string()),
      span: span.clone(),
    });
  }
  let code_el = Node::JsxElement(JsxElement {
    name: "code".into(),
    attrs: code_attrs,
    children: line_children,
    span: span.clone(),
  });

  // `<pre>` style: primary `color` + `background-color`, plus
  // `--dmc-{mode}` and `--dmc-{mode}-bg` per mode for theme switching.
  let pre_style = build_pre_style(themes, foregrounds, backgrounds, default_idx, shape.include_pre_background);
  let mut pre_attrs: Vec<JsxAttr> = Vec::new();
  if shape.keep_raw_string {
    pre_attrs.push(JsxAttr {
      name: "__dmcRaw__".into(),
      value: JsxAttrValue::String(cb.value.clone()),
      span: span.clone(),
    });
  }
  if shape.include_data_language {
    pre_attrs.push(JsxAttr {
      name: "data-language".into(),
      value: JsxAttrValue::String(resolved_lang.to_string()),
      span: span.clone(),
    });
  }
  if !pre_style.is_empty() {
    pre_attrs.push(JsxAttr { name: "style".into(), value: JsxAttrValue::String(pre_style), span: span.clone() });
  }
  JsxElement { name: "pre".into(), attrs: pre_attrs, children: vec![code_el], span }
}

/// Fold leading whitespace-only runs into the first styled run so
/// indented lines don't emit `<span>   </span>` siblings. All-whitespace
/// lines are left as-is.
fn coalesce_leading_whitespace(runs: &mut Vec<(String, String)>) {
  let split = runs.iter().position(|(style, text)| !(style.is_empty() && text.chars().all(|c| c.is_whitespace())));
  let Some(idx) = split else { return };
  if idx == 0 {
    return;
  }
  let mut prefix = String::new();
  for (_, text) in runs.drain(..idx) {
    prefix.push_str(&text);
  }
  if let Some(first) = runs.first_mut() {
    first.1.insert_str(0, &prefix);
  }
}

fn css_vars_token_style(
  tok: &dmc_highlight::MultiToken<'_>,
  themes: &[(String, String)],
  foregrounds: &[Option<dmc_highlight::Color>],
) -> String {
  use dmc_highlight::HlFontStyle as FontStyle;
  // Whitespace inherits unconditionally - saves a span attribute on
  // most lines (significant share of token count).
  if tok.text.chars().all(|c| c.is_whitespace()) {
    return String::new();
  }
  let mut parts: Vec<String> = Vec::with_capacity(themes.len() * 2);
  for (j, (mode, _)) in themes.iter().enumerate() {
    let Some(style) = tok.styles.get(j) else { continue };
    let fg = style.foreground;
    let same_as_default =
      foregrounds.get(j).and_then(|c| *c).map(|d| d.r == fg.r && d.g == fg.g && d.b == fg.b).unwrap_or(false);
    if !same_as_default {
      parts.push(format!("--dmc-{mode}:#{:02X}{:02X}{:02X}", fg.r, fg.g, fg.b));
    }
    if style.font_style.contains(FontStyle::ITALIC) {
      parts.push(format!("--dmc-{mode}-fs:italic"));
    }
    if style.font_style.contains(FontStyle::BOLD) {
      parts.push(format!("--dmc-{mode}-fw:bold"));
    }
    if style.font_style.contains(FontStyle::UNDERLINE) {
      parts.push(format!("--dmc-{mode}-td:underline"));
    }
  }
  parts.join(";")
}

/// Build the `<pre>` `style` attribute: primary mode's solid color +
/// background, plus every mode's `--dmc-{mode}` and `--dmc-{mode}-bg`.
fn build_pre_style(
  themes: &[(String, String)],
  foregrounds: &[Option<dmc_highlight::Color>],
  backgrounds: &[Option<dmc_highlight::Color>],
  default_idx: usize,
  include_bg: bool,
) -> String {
  let mut parts: Vec<String> = Vec::with_capacity(themes.len() * 2 + 2);
  if let Some(fg) = foregrounds.get(default_idx).and_then(|c| *c) {
    parts.push(format!("color:#{:02x}{:02x}{:02x}", fg.r, fg.g, fg.b));
  }
  // Solid `background-color` from the primary theme. Consumers that
  // want their own chrome around the `<pre>` (and don't want the
  // theme palette bleeding through) opt out via
  // `prettyCode.includePreBackground: false` - the per-mode
  // `--dmc-{mode}-bg` custom properties below stay either way.
  if include_bg && let Some(bg) = backgrounds.get(default_idx).and_then(|c| *c) {
    parts.push(format!("background-color:#{:02x}{:02x}{:02x}", bg.r, bg.g, bg.b));
  }
  for (j, (mode, _)) in themes.iter().enumerate() {
    if let Some(fg) = foregrounds.get(j).and_then(|c| *c) {
      parts.push(format!("--dmc-{mode}:#{:02x}{:02x}{:02x}", fg.r, fg.g, fg.b));
    }
    if let Some(bg) = backgrounds.get(j).and_then(|c| *c) {
      parts.push(format!("--dmc-{mode}-bg:#{:02x}{:02x}{:02x}", bg.r, bg.g, bg.b));
    }
  }
  parts.join(";")
}

/// Per-theme token style: solid `color:#XXX` from that theme alone, plus
/// font-style flags. Tokens whose color matches the theme's default
/// foreground get an empty style so they inherit through `<pre>`.
fn single_theme_token_style(
  tok: &dmc_highlight::MultiToken<'_>,
  theme_idx: usize,
  fg_default: Option<dmc_highlight::Color>,
) -> String {
  use dmc_highlight::HlFontStyle as FontStyle;
  let Some(style) = tok.styles.get(theme_idx) else { return String::new() };
  let fg = style.foreground;
  let plain = tok.text.chars().all(|c| c.is_whitespace());
  if plain {
    return String::new();
  }
  let same_as_default = fg_default.map(|d| d.r == fg.r && d.g == fg.g && d.b == fg.b).unwrap_or(false);
  let mut s = if same_as_default { String::new() } else { format!("color:#{:02X}{:02X}{:02X}", fg.r, fg.g, fg.b) };
  if style.font_style.contains(FontStyle::ITALIC) {
    if !s.is_empty() {
      s.push(';');
    }
    s.push_str("font-style:italic");
  }
  if style.font_style.contains(FontStyle::BOLD) {
    if !s.is_empty() {
      s.push(';');
    }
    s.push_str("font-weight:bold");
  }
  if style.font_style.contains(FontStyle::UNDERLINE) {
    if !s.is_empty() {
      s.push(';');
    }
    s.push_str("text-decoration:underline");
  }
  s
}

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

  #[test]
  fn parse_meta_title_and_marks() {
    let m = parse_meta(r#"title="hello" {1,3-5}"#);
    assert_eq!(m.title.as_deref(), Some("hello"));
    assert_eq!(m.line_marks.len(), 2);
    assert!(m.line_marks[0].contains(1));
    assert!(!m.line_marks[0].contains(2));
    assert!(m.line_marks[1].contains(3));
    assert!(m.line_marks[1].contains(5));
    assert!(!m.line_marks[1].contains(6));
  }

  #[test]
  fn parse_meta_empty() {
    let m = parse_meta("");
    assert!(m.title.is_none());
    assert!(m.line_marks.is_empty());
  }

  #[test]
  fn parse_meta_malformed_marks_skipped() {
    let m = parse_meta("{1,abc,3-x}");
    assert_eq!(m.line_marks.len(), 1);
    assert!(m.line_marks[0].contains(1));
  }

  #[test]
  fn options_default_resolves_to_catppuccin_pair_with_dark_primary() {
    let pc = PrettyCode::default();
    assert_eq!(pc.themes.len(), 2);
    assert_eq!(pc.default_mode, "dark");
    let modes: BTreeMap<_, _> = pc.themes.iter().cloned().collect();
    assert_eq!(modes.get("light").map(String::as_str), Some("Catppuccin Latte"));
    assert_eq!(modes.get("dark").map(String::as_str), Some("Catppuccin Mocha"));
  }

  #[test]
  fn from_options_picks_explicit_default_mode_when_present() {
    let map: BTreeMap<String, String> =
      [("dim".to_string(), "Nord".to_string()), ("bright".to_string(), "Catppuccin Latte".to_string())]
        .into_iter()
        .collect();
    let opts = PrettyCodeOptions {
      theme: PrettyCodeTheme::Multi(map),
      default_mode: Some("bright".into()),
      ..Default::default()
    };
    let pc = PrettyCode::from_options(&opts);
    assert_eq!(pc.default_mode, "bright");
  }

  #[test]
  fn from_options_falls_back_to_first_when_no_dark_or_explicit() {
    let map: BTreeMap<String, String> =
      [("alpha".into(), "Nord".into()), ("beta".into(), "TwoDark".into())].into_iter().collect();
    let opts = PrettyCodeOptions { theme: PrettyCodeTheme::Multi(map), default_mode: None, ..Default::default() };
    let pc = PrettyCode::from_options(&opts);
    // BTreeMap orders alphabetically; first key is "alpha".
    assert_eq!(pc.default_mode, "alpha");
  }

  #[test]
  fn theme_serde_accepts_string_and_object() {
    let s: PrettyCodeTheme = serde_json::from_str(r#""Catppuccin Mocha""#).unwrap();
    assert!(matches!(s, PrettyCodeTheme::Single(ref n) if n == "Catppuccin Mocha"));
    let m: PrettyCodeTheme = serde_json::from_str(r#"{"light":"Catppuccin Latte","dark":"Catppuccin Mocha"}"#).unwrap();
    if let PrettyCodeTheme::Multi(map) = m {
      assert_eq!(map.len(), 2);
    } else {
      panic!("expected Multi");
    }
  }
}