marq 2.0.0

Markdown rendering with pluggable code block handlers
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
//! Built-in code block handlers.
//!
//! Some handlers are available when their respective feature flags are enabled:
//! - `highlight` - Syntax highlighting via arborium
//! - `aasvg` - ASCII art to SVG conversion
//! - `pikru` - Pikchr diagram rendering
//!
//! The following handlers are always available:
//! - `TermHandler` - Terminal output passthrough
//! - `MermaidHandler` - Client-side Mermaid.js diagrams

use std::future::Future;
use std::pin::Pin;

use crate::Result;
use crate::handler::{CodeBlockHandler, CodeBlockOutput};

/// Syntax highlighting handler using arborium.
///
/// Requires the `highlight` feature.
#[cfg(feature = "highlight")]
pub struct ArboriumHandler {
    highlighter: std::sync::Mutex<arborium::Highlighter>,
    /// Whether to show a language header above code blocks
    show_language_header: bool,
}

#[cfg(feature = "highlight")]
impl ArboriumHandler {
    /// Create a new ArboriumHandler with default config.
    pub fn new() -> Self {
        Self {
            highlighter: std::sync::Mutex::new(arborium::Highlighter::new()),
            show_language_header: true,
        }
    }

    /// Create a new ArboriumHandler with custom config.
    pub fn with_config(config: arborium::Config) -> Self {
        Self {
            highlighter: std::sync::Mutex::new(arborium::Highlighter::with_config(config)),
            show_language_header: true,
        }
    }

    /// Enable or disable the language header above code blocks.
    ///
    /// When enabled, code blocks will be wrapped in a container with a header
    /// showing the language name, similar to the compare block style.
    pub fn with_language_header(mut self, show: bool) -> Self {
        self.show_language_header = show;
        self
    }
}

#[cfg(feature = "highlight")]
impl Default for ArboriumHandler {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "highlight")]
impl CodeBlockHandler for ArboriumHandler {
    fn render<'a>(
        &'a self,
        language: &'a str,
        code: &'a str,
    ) -> Pin<Box<dyn Future<Output = Result<CodeBlockOutput>> + Send + 'a>> {
        Box::pin(async move {
            use crate::handler::html_escape;

            // Empty language means no syntax highlighting requested - render as plain
            if language.is_empty() {
                let escaped = html_escape(code);
                return Ok(format!(
                    "<div class=\"code-block\"><pre><code>{escaped}</code></pre></div>"
                )
                .into());
            }

            // Map common language aliases to arborium language names
            let arborium_lang = match language {
                "jinja" => "jinja2",
                _ => language,
            };

            let escaped_lang = html_escape(language);

            // Try to highlight with arborium
            let mut hl = self.highlighter.lock().unwrap();
            let highlighted_code = match hl.highlight(arborium_lang, code) {
                Ok(html) => {
                    // Trim trailing newline from arborium output
                    // See: https://github.com/bearcove/arborium/issues/128
                    html.trim_end_matches('\n').to_string()
                }
                Err(_e) => {
                    // Fall back to plain text rendering for unsupported languages
                    html_escape(code)
                }
            };

            // Build the output with data-lang for CSS targeting
            if self.show_language_header {
                Ok(format!(
                    "<div class=\"code-block\" data-lang=\"{escaped_lang}\"><div class=\"code-header\">{escaped_lang}</div><pre><code class=\"language-{escaped_lang}\">{highlighted_code}</code></pre></div>"
                )
                .into())
            } else {
                Ok(format!(
                    "<div class=\"code-block\" data-lang=\"{escaped_lang}\"><pre><code class=\"language-{escaped_lang}\">{highlighted_code}</code></pre></div>"
                )
                .into())
            }
        })
    }
}

/// Terminal output handler that passes through HTML without escaping.
///
/// This handler is designed for pre-rendered terminal output from tools like
/// `ddc term` which produce HTML with `<t-*>` custom elements for styled text.
/// The content is wrapped in a code block container but not HTML-escaped.
pub struct TermHandler;

impl TermHandler {
    /// Create a new TermHandler.
    pub fn new() -> Self {
        Self
    }
}

impl Default for TermHandler {
    fn default() -> Self {
        Self::new()
    }
}

impl CodeBlockHandler for TermHandler {
    fn render<'a>(
        &'a self,
        _language: &'a str,
        code: &'a str,
    ) -> Pin<Box<dyn Future<Output = Result<CodeBlockOutput>> + Send + 'a>> {
        Box::pin(async move {
            // Pass through the HTML without escaping - it's already valid HTML
            // from the terminal renderer (contains <t-b>, <t-f>, etc. elements)
            Ok(format!(
                "<div class=\"code-block term-output\"><pre><code>{}</code></pre></div>",
                code
            )
            .into())
        })
    }
}

/// ASCII art to SVG handler using aasvg.
///
/// Requires the `aasvg` feature.
#[cfg(feature = "aasvg")]
pub struct AasvgHandler;

#[cfg(feature = "aasvg")]
impl AasvgHandler {
    /// Create a new AasvgHandler.
    pub fn new() -> Self {
        Self
    }
}

#[cfg(feature = "aasvg")]
impl Default for AasvgHandler {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "aasvg")]
impl CodeBlockHandler for AasvgHandler {
    fn render<'a>(
        &'a self,
        _language: &'a str,
        code: &'a str,
    ) -> Pin<Box<dyn Future<Output = Result<CodeBlockOutput>> + Send + 'a>> {
        Box::pin(async move {
            let svg = aasvg::render(code);
            Ok(svg.into())
        })
    }
}

/// Pikchr diagram handler using pikru.
///
/// Requires the `pikru` feature.
#[cfg(feature = "pikru")]
pub struct PikruHandler {
    /// Whether to use CSS variables for colors (for dark mode support)
    pub css_variables: bool,
}

#[cfg(feature = "pikru")]
impl PikruHandler {
    /// Create a new PikruHandler.
    pub fn new() -> Self {
        Self {
            css_variables: false,
        }
    }

    /// Create a new PikruHandler with CSS variable support.
    pub fn with_css_variables(css_variables: bool) -> Self {
        Self { css_variables }
    }
}

#[cfg(feature = "pikru")]
impl Default for PikruHandler {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "pikru")]
impl CodeBlockHandler for PikruHandler {
    fn render<'a>(
        &'a self,
        _language: &'a str,
        code: &'a str,
    ) -> Pin<Box<dyn Future<Output = Result<CodeBlockOutput>> + Send + 'a>> {
        Box::pin(async move {
            // Parse the pikchr source
            let program = match pikru::parse::parse(code) {
                Ok(p) => p,
                Err(e) => {
                    return Err(crate::Error::CodeBlockHandler {
                        language: "pik".to_string(),
                        message: format!("parse error: {}", e),
                    });
                }
            };

            // Expand macros
            let program = match pikru::macros::expand_macros(program) {
                Ok(p) => p,
                Err(e) => {
                    return Err(crate::Error::CodeBlockHandler {
                        language: "pik".to_string(),
                        message: format!("macro error: {}", e),
                    });
                }
            };

            // Render to SVG
            let options = pikru::render::RenderOptions {
                css_variables: self.css_variables,
            };
            match pikru::render::render_with_options(&program, &options) {
                Ok(svg) => Ok(svg.into()),
                Err(e) => Err(crate::Error::CodeBlockHandler {
                    language: "pik".to_string(),
                    message: format!("render error: {}", e),
                }),
            }
        })
    }
}

/// Mermaid diagram handler.
///
/// Emits a `<pre class="mermaid">` block for client-side rendering by
/// Mermaid.js, wrapped in `data-hotmeal-opaque` for live-reload compatibility.
/// Includes a head injection that loads Mermaid.js from CDN and listens for
/// `hotmeal:opaque-changed` events to re-render after live-reload patches.
pub struct MermaidHandler;

impl MermaidHandler {
    /// Create a new MermaidHandler.
    pub fn new() -> Self {
        Self
    }
}

impl Default for MermaidHandler {
    fn default() -> Self {
        Self::new()
    }
}

impl CodeBlockHandler for MermaidHandler {
    fn render<'a>(
        &'a self,
        _language: &'a str,
        code: &'a str,
    ) -> Pin<Box<dyn Future<Output = Result<CodeBlockOutput>> + Send + 'a>> {
        Box::pin(async move {
            use crate::handler::{HeadInjection, html_escape};

            let escaped = html_escape(code);
            let html = format!(
                "<div data-hotmeal-opaque=\"mermaid\"><pre class=\"mermaid\">{escaped}</pre></div>"
            );

            let script = r#"<script type="module">
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs';

function mermaidTheme() {
  return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'default';
}

async function renderMermaidNode(pre) {
  pre.removeAttribute('data-processed');
  pre.innerHTML = pre.dataset.mermaidSource;
  mermaid.initialize({ startOnLoad: false, theme: mermaidTheme() });
  await mermaid.run({ nodes: [pre] });
}

async function reinitAllMermaid() {
  const nodes = document.querySelectorAll('pre.mermaid');
  for (const pre of nodes) {
    await renderMermaidNode(pre);
  }
}

// stash original source before first render so we can re-render on theme change
for (const pre of document.querySelectorAll('pre.mermaid')) {
  pre.dataset.mermaidSource = pre.innerHTML;
}

// startOnLoad relies on DOMContentLoaded which may have already fired when
// this script is injected dynamically, so we initialize and run explicitly.
mermaid.initialize({ startOnLoad: false, theme: mermaidTheme() });
await mermaid.run();

window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', reinitAllMermaid);

document.addEventListener('hotmeal:opaque-changed', async (e) => {
  if (e.detail?.key !== 'mermaid') return;
  const el = e.detail.element;
  if (!el) return;

  // The opaque patch gives us new HTML content — apply it to the DOM
  if (e.detail.content) {
    el.innerHTML = e.detail.content;
  }

  const pre = el.querySelector('pre.mermaid');
  if (pre) {
    pre.dataset.mermaidSource = pre.textContent;
    await renderMermaidNode(pre);
  }
});
</script>"#;

            Ok(CodeBlockOutput {
                html,
                head_injections: vec![HeadInjection {
                    key: "mermaid".to_string(),
                    html: script.to_string(),
                }],
            })
        })
    }
}

/// A parsed section from a compare block.
#[derive(Debug, Clone)]
pub struct CompareSection {
    /// Language identifier for syntax highlighting
    pub language: String,
    /// The code content
    pub code: String,
}

/// Side-by-side code comparison handler.
///
/// Parses code blocks with `/// language` separators and renders them
/// side-by-side with syntax highlighting.
///
/// # Syntax
///
/// ````text
/// ```compare
/// /// json
/// {"server": {"host": "localhost", "port": 8080}}
/// /// styx
/// server host=localhost port=8080
/// ```
/// ````
///
/// The `/// language` lines act as separators, where `language` is the
/// syntax highlighting language for the following code section.
///
/// # Output
///
/// Renders as a flex container with each section displayed side-by-side.
/// Each section has its language as a header and syntax-highlighted code.
#[cfg(feature = "highlight")]
pub struct CompareHandler {
    highlighter: std::sync::Mutex<arborium::Highlighter>,
}

#[cfg(feature = "highlight")]
impl CompareHandler {
    /// Create a new CompareHandler with default config.
    pub fn new() -> Self {
        Self {
            highlighter: std::sync::Mutex::new(arborium::Highlighter::new()),
        }
    }

    /// Create a new CompareHandler with custom config.
    pub fn with_config(config: arborium::Config) -> Self {
        Self {
            highlighter: std::sync::Mutex::new(arborium::Highlighter::with_config(config)),
        }
    }

    /// Parse the compare block content into sections.
    ///
    /// Each section starts with `/// language` and contains the code until
    /// the next separator or end of content.
    pub fn parse_sections(code: &str) -> Vec<CompareSection> {
        let mut sections = Vec::new();
        let mut current_language: Option<String> = None;
        let mut current_code = String::new();

        for line in code.lines() {
            if let Some(lang) = line.strip_prefix("/// ") {
                // Start a new section - save previous if exists
                if let Some(lang) = current_language.take() {
                    sections.push(CompareSection {
                        language: lang,
                        code: current_code.trim_end().to_string(),
                    });
                    current_code.clear();
                }
                current_language = Some(lang.trim().to_string());
            } else if current_language.is_some() {
                // Accumulate code in current section
                if !current_code.is_empty() {
                    current_code.push('\n');
                }
                current_code.push_str(line);
            }
            // Lines before any `/// language` are ignored
        }

        // Don't forget the last section
        if let Some(lang) = current_language {
            sections.push(CompareSection {
                language: lang,
                code: current_code.trim_end().to_string(),
            });
        }

        sections
    }

    /// Highlight code using arborium, with fallback for unsupported languages.
    fn highlight_code(&self, language: &str, code: &str) -> String {
        use crate::handler::html_escape;

        if language.is_empty() {
            return html_escape(code);
        }

        // Map common language aliases
        let arborium_lang = match language {
            "jinja" => "jinja2",
            _ => language,
        };

        let mut hl = self.highlighter.lock().unwrap();
        match hl.highlight(arborium_lang, code) {
            Ok(html) => html,
            Err(_) => html_escape(code),
        }
    }
}

#[cfg(feature = "highlight")]
impl Default for CompareHandler {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "highlight")]
impl CodeBlockHandler for CompareHandler {
    fn render<'a>(
        &'a self,
        _language: &'a str,
        code: &'a str,
    ) -> Pin<Box<dyn Future<Output = Result<CodeBlockOutput>> + Send + 'a>> {
        Box::pin(async move {
            use crate::handler::html_escape;

            let sections = Self::parse_sections(code);

            if sections.is_empty() {
                // No valid sections found - render as plain text
                let escaped = html_escape(code);
                return Ok(format!(
                    "<div class=\"code-block\"><pre><code>{escaped}</code></pre></div>"
                )
                .into());
            }

            let mut html = String::new();
            html.push_str("<div class=\"compare-container\">");

            for section in &sections {
                let highlighted = self.highlight_code(&section.language, &section.code);
                let escaped_lang = html_escape(&section.language);

                html.push_str("<div class=\"compare-section\">");
                html.push_str(&format!(
                    "<div class=\"compare-header\">{}</div>",
                    escaped_lang
                ));
                html.push_str(&format!(
                    "<div class=\"code-block\"><pre><code class=\"language-{}\">{}</code></pre></div>",
                    escaped_lang, highlighted
                ));
                html.push_str("</div>");
            }

            html.push_str("</div>");

            Ok(html.into())
        })
    }
}

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

    #[cfg(feature = "highlight")]
    mod compare_handler_tests {
        use super::*;

        #[test]
        fn test_parse_sections_basic() {
            let code = r#"/// json
{"key": "value"}
/// yaml
key: value"#;

            let sections = CompareHandler::parse_sections(code);
            assert_eq!(sections.len(), 2);

            assert_eq!(sections[0].language, "json");
            assert_eq!(sections[0].code, r#"{"key": "value"}"#);

            assert_eq!(sections[1].language, "yaml");
            assert_eq!(sections[1].code, "key: value");
        }

        #[test]
        fn test_parse_sections_multiline_code() {
            let code = r#"/// rust
fn main() {
    println!("Hello");
}
/// python
def main():
    print("Hello")"#;

            let sections = CompareHandler::parse_sections(code);
            assert_eq!(sections.len(), 2);

            assert_eq!(sections[0].language, "rust");
            assert!(sections[0].code.contains("fn main()"));
            assert!(sections[0].code.contains("println!"));

            assert_eq!(sections[1].language, "python");
            assert!(sections[1].code.contains("def main():"));
        }

        #[test]
        fn test_parse_sections_ignores_leading_content() {
            let code = r#"This is ignored
Also ignored
/// json
{"valid": true}"#;

            let sections = CompareHandler::parse_sections(code);
            assert_eq!(sections.len(), 1);
            assert_eq!(sections[0].language, "json");
            assert_eq!(sections[0].code, r#"{"valid": true}"#);
        }

        #[test]
        fn test_parse_sections_empty() {
            let code = "no sections here";
            let sections = CompareHandler::parse_sections(code);
            assert!(sections.is_empty());
        }

        #[test]
        fn test_parse_sections_three_way() {
            let code = r#"/// json
{"format": "json"}
/// yaml
format: yaml
/// toml
format = "toml""#;

            let sections = CompareHandler::parse_sections(code);
            assert_eq!(sections.len(), 3);
            assert_eq!(sections[0].language, "json");
            assert_eq!(sections[1].language, "yaml");
            assert_eq!(sections[2].language, "toml");
        }

        #[tokio::test]
        async fn test_render_compare_block() {
            let handler = CompareHandler::new();
            let code = r#"/// json
{"key": "value"}
/// yaml
key: value"#;

            let output = handler.render("compare", code).await.unwrap();

            assert!(output.html.contains(r#"class="compare-container""#));
            assert!(output.html.contains(r#"class="compare-section""#));
            assert!(output.html.contains(r#"class="compare-header""#));
            assert!(output.html.contains("json"));
            assert!(output.html.contains("yaml"));
            assert!(output.head_injections.is_empty());
        }

        #[tokio::test]
        async fn test_render_empty_compare_block() {
            let handler = CompareHandler::new();
            let code = "no valid sections";

            let output = handler.render("compare", code).await.unwrap();

            // Should fall back to plain text rendering
            assert!(
                output
                    .html
                    .contains("<div class=\"code-block\"><pre><code>")
            );
            assert!(output.html.contains("no valid sections"));
        }
    }

    mod mermaid_handler_tests {
        use super::*;

        #[tokio::test]
        async fn test_mermaid_handler_output() {
            let handler = MermaidHandler::new();
            let code = "graph TD\n    A-->B";
            let output = handler.render("mermaid", code).await.unwrap();

            // Wrapped in data-hotmeal-opaque
            assert!(
                output.html.contains("data-hotmeal-opaque=\"mermaid\""),
                "Should have hotmeal opaque wrapper: {}",
                output.html
            );
            // Contains pre.mermaid
            assert!(
                output.html.contains("<pre class=\"mermaid\">"),
                "Should have pre.mermaid: {}",
                output.html
            );
            // Code is HTML-escaped
            assert!(
                output.html.contains("A--&gt;B"),
                "Code should be HTML-escaped: {}",
                output.html
            );
            // Head injection present
            assert_eq!(output.head_injections.len(), 1);
            assert_eq!(output.head_injections[0].key, "mermaid");
            assert!(output.head_injections[0].html.contains("mermaid"));
        }
    }
}