umd 0.2.0

Universal Markdown - A post-Markdown superset with semantic HTML and extensible syntax
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
//! Universal Markdown Parser
//!
//! A post-Markdown superset parser with its own `umd-*` reference-CSS
//! classes and extensible syntax. This parser aims for reasonable
//! CommonMark compliance (75%+) while providing powerful extensions
//! including its own reference-CSS styling, semantic HTML, and plugin
//! support.
//!
//! # Features
//!
//! - CommonMark-compliant Markdown parsing
//! - Own `umd-*` reference-CSS classes (no CSS framework dependency)
//! - Extended syntax (definition lists, decorations, semantic HTML)
//! - LukiWiki legacy syntax support for backward compatibility
//! - HTML sanitization (direct HTML input is forbidden)
//! - Safe HTML output generation
//! - Plugin system support (output only, execution handled externally)
//!
//! # Security
//!
//! All user input is sanitized to prevent XSS attacks. HTML entities are
//! preserved, but raw HTML tags are escaped. Plugin output is the only
//! exception, as plugins are considered trusted code.
//!
//! # Example
//!
//! ```
//! use umd::parse;
//!
//! let input = "# Hello World\n\nThis is **bold** text.";
//! let html = parse(input);
//! ```
//!
//! # WASM Usage
//!
//! This library can be compiled to WebAssembly for use in browsers:
//!
//! ```javascript
//! import init, { parse } from './umd.js';
//!
//! await init();
//! const html = parse('# Hello World');
//! ```

use serde::Deserialize;
use wasm_bindgen::prelude::*;

pub mod extensions;
pub mod frontmatter;
pub mod parser;
pub mod sanitizer;

/// Parse result with optional frontmatter and footnotes
#[derive(Debug, Clone)]
pub struct ParseResult {
    /// The rendered HTML content (body only, footnotes are separate)
    pub html: String,
    /// Optional frontmatter data
    pub frontmatter: Option<frontmatter::Frontmatter>,
    /// Footnotes HTML (if any footnotes are present)
    pub footnotes: Option<String>,
}

/// Parse Universal Markdown and convert to HTML
///
/// This function extracts frontmatter (if present) and parses the content.
/// Returns merged HTML of body and footnotes (if any).
///
/// # Arguments
///
/// * `input` - The Universal Markdown source text
///
/// # Returns
///
/// HTML string (frontmatter is removed from output, footnotes are appended)
///
/// # Examples
///
/// ```
/// use umd::parse;
///
/// let input = "# Heading\n\n**Bold** and *italic*[^1]\n\n[^1]: Footnote";
/// let html = parse(input);
/// assert!(html.contains("<h1>"));
/// assert!(html.contains("<strong>"));
/// assert!(html.contains("Footnote"));
/// ```
pub fn parse(input: &str) -> String {
    let result = parse_with_frontmatter(input);
    if let Some(footnotes) = result.footnotes {
        format!("{}\n{}", result.html, footnotes)
    } else {
        result.html
    }
}

#[derive(Debug, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
struct WasmIconsOptions {
    video: Option<String>,
    audio: Option<String>,
    download: Option<String>,
    color_swatch: Option<String>,
    note: Option<String>,
    tip: Option<String>,
    important: Option<String>,
    warning: Option<String>,
    caution: Option<String>,
    must: Option<String>,
    recommend: Option<String>,
    dont: Option<String>,
    never: Option<String>,
    example: Option<String>,
}

#[derive(Debug, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
struct WasmParseOptions {
    gfm_extensions: Option<bool>,
    umd_extensions: Option<bool>,
    max_heading_level: Option<u8>,
    max_inline_nesting: Option<u8>,
    base_url: Option<String>,
    allow_fragment_extension_hint: Option<bool>,
    allow_hex_colors: Option<bool>,
    allow_custom_font_size: Option<bool>,
    icons: Option<WasmIconsOptions>,
}

fn parse_with_options_json(input: &str, options_json: Option<&str>) -> String {
    let mut options = parser::ParserOptions::default();

    if let Some(raw_json) = options_json {
        let trimmed = raw_json.trim();
        if !trimmed.is_empty() {
            if let Ok(raw) = serde_json::from_str::<WasmParseOptions>(trimmed) {
                if let Some(value) = raw.gfm_extensions {
                    options.gfm_extensions = value;
                }
                if let Some(value) = raw.umd_extensions {
                    options.umd_extensions = value;
                }
                if let Some(value) = raw.max_heading_level {
                    options.max_heading_level = value;
                }
                if let Some(value) = raw.max_inline_nesting {
                    options.max_inline_nesting = Some(value);
                }
                if let Some(value) = raw.base_url {
                    options.base_url = Some(value);
                }
                if let Some(value) = raw.allow_fragment_extension_hint {
                    options.allow_fragment_extension_hint = value;
                }
                if let Some(value) = raw.allow_hex_colors {
                    options.allow_hex_colors = value;
                }
                if let Some(value) = raw.allow_custom_font_size {
                    options.allow_custom_font_size = value;
                }
                if let Some(icons) = raw.icons {
                    if let Some(value) = icons.video {
                        options.icons.video = value;
                    }
                    if let Some(value) = icons.audio {
                        options.icons.audio = value;
                    }
                    if let Some(value) = icons.download {
                        options.icons.download = value;
                    }
                    if let Some(value) = icons.color_swatch {
                        options.icons.color_swatch = value;
                    }
                    if let Some(value) = icons.note {
                        options.icons.note = value;
                    }
                    if let Some(value) = icons.tip {
                        options.icons.tip = value;
                    }
                    if let Some(value) = icons.important {
                        options.icons.important = value;
                    }
                    if let Some(value) = icons.warning {
                        options.icons.warning = value;
                    }
                    if let Some(value) = icons.caution {
                        options.icons.caution = value;
                    }
                    if let Some(value) = icons.must {
                        options.icons.must = value;
                    }
                    if let Some(value) = icons.recommend {
                        options.icons.recommend = value;
                    }
                    if let Some(value) = icons.dont {
                        options.icons.dont = value;
                    }
                    if let Some(value) = icons.never {
                        options.icons.never = value;
                    }
                    if let Some(value) = icons.example {
                        options.icons.example = value;
                    }
                }
            }
        }
    }

    let result = parse_with_frontmatter_opts(input, &options);
    if let Some(footnotes) = result.footnotes {
        format!("{}\n{}", result.html, footnotes)
    } else {
        result.html
    }
}

/// Parse Universal Markdown and return HTML with frontmatter
///
/// This function extracts frontmatter and returns it separately from the HTML content.
///
/// # Arguments
///
/// * `input` - The Universal Markdown source text
///
/// # Returns
///
/// ParseResult containing HTML and optional frontmatter
///
/// # Examples
///
/// ```
/// use umd::parse_with_frontmatter;
///
/// let input = "---\ntitle: Test\n---\n\n# Content";
/// let result = parse_with_frontmatter(input);
/// assert!(result.frontmatter.is_some());
/// assert!(result.html.contains("<h1>"));
/// ```
pub fn parse_with_frontmatter(input: &str) -> ParseResult {
    let options = parser::ParserOptions::default();
    parse_with_frontmatter_opts(input, &options)
}

/// Parse Universal Markdown and return HTML with frontmatter and custom options
///
/// This function extracts frontmatter and returns it separately from the HTML content,
/// with support for custom parser options (e.g., base_url for URL resolution).
///
/// # Arguments
///
/// * `input` - The Universal Markdown source text
/// * `options` - Parser configuration options
///
/// # Returns
///
/// ParseResult containing HTML and optional frontmatter
///
/// # Examples
///
/// ```
/// use umd::{parse_with_frontmatter_opts, parser::ParserOptions};
///
/// let input = "[link](/docs)";
/// let mut opts = ParserOptions::default();
/// opts.base_url = Some("/app".to_string());
/// let result = parse_with_frontmatter_opts(input, &opts);
/// assert!(result.html.contains(r#"href="/app/docs""#));
/// ```
pub fn parse_with_frontmatter_opts(input: &str, options: &parser::ParserOptions) -> ParseResult {
    // Step 0: Extract frontmatter
    let (frontmatter_data, content) = frontmatter::extract_frontmatter(input);

    // Step 1: Pre-process list items to allow nested block elements
    let content = extensions::nested_blocks::preprocess_nested_blocks(&content);

    // Step 2: Pre-process indeterminate task list markers
    let content = extensions::preprocessor::preprocess_tasklist_indeterminate(&content);

    // Step 3: Pre-process Discord-style underline (__text__) to prevent CommonMark conversion
    let content = extensions::inline::notation::preprocess_discord_underline(&content);

    // Step 3.5: Normalize fenced code block filename syntax (```lang:file)
    let content = extensions::fence::normalize::preprocess_code_block_filenames(&content);

    // Step 4: Pre-process to resolve syntax conflicts and extract custom header IDs
    let (preprocessed, header_map) = extensions::conflict_resolver::preprocess_conflicts(
        &content,
        options.allow_hex_colors,
        options.allow_custom_font_size,
    );

    // Step 4.5: Remove ASCII control characters (U+0000-U+001F except TAB/LF/CR, and U+007F)
    // from non-code-block regions. Plugin content is already base64-encoded by Step 4,
    // so plugin markers are inherently safe. Plugin authors are responsible for any
    // further sanitization of their plugin's content.
    let preprocessed = sanitizer::remove_ascii_control_chars_from_markup(&preprocessed);

    // Step 5: Sanitize input
    let sanitized = sanitizer::sanitize_opts(&preprocessed, options.allow_bidi_in_code_blocks);

    // Step 6: Parse with comrak-based parser
    let html = parser::parse_to_html(&sanitized, options);

    // Step 7: Restore Discord-style underline placeholders to <u> tags
    let html = extensions::inline::notation::postprocess_discord_underline(&html);

    // Step 8: Apply extended syntax and custom header IDs (includes post-processing)
    let final_html = extensions::apply_extensions_with_headers(&html, &header_map, options);

    // Step 9: Extract footnotes from HTML
    let (body_html, footnotes_html) = extract_footnotes(&final_html);

    ParseResult {
        html: body_html,
        frontmatter: frontmatter_data,
        footnotes: footnotes_html,
    }
}

/// Extract footnotes section from HTML
///
/// Comrak generates footnotes as a `<section class="footnotes">` element.
/// This function separates the footnotes from the main content.
///
/// # Arguments
///
/// * `html` - The complete HTML with potential footnotes
///
/// # Returns
///
/// A tuple of (body HTML, optional footnotes HTML)
fn extract_footnotes(html: &str) -> (String, Option<String>) {
    use regex::Regex;

    // Match the footnotes section generated by comrak
    let footnote_pattern =
        Regex::new(r#"(?s)<section class="footnotes"[^>]*>.*?</section>"#).unwrap();

    if let Some(matched) = footnote_pattern.find(html) {
        let footnotes = matched.as_str().to_string();
        let body = footnote_pattern.replace(html, "").to_string();
        (body, Some(footnotes))
    } else {
        (html.to_string(), None)
    }
}

/// WASM-exposed API for parsing Universal Markdown
///
/// This is the main entry point when using the library from JavaScript/WebAssembly.
///
/// The second argument is optional JSON options in camelCase.
///
/// Supported options:
/// - `gfmExtensions`: boolean
/// - `umdExtensions`: boolean
/// - `maxHeadingLevel`: number
/// - `maxInlineNesting`: number (recommended: 3-5)
/// - `baseUrl`: string
/// - `allowFragmentExtensionHint`: boolean
/// - `allowHexColors`: boolean
/// - `allowCustomFontSize`: boolean — when false (default), `SIZE()`/`&size()`
///   only accept the keyword sizes `xs`/`sm`/`lg`/`xl` (mapped to the
///   `umd-text-size-*` classes); when true, arbitrary rem/px/unitless values
///   are also accepted and rendered as an inline `font-size` style.
/// - `icons`: object with `video`, `audio`, `download`, `colorSwatch`, `note`,
///   `tip`, `important`, `warning`, `caution`, `must`, `recommend`, `dont`,
///   `never`, `example`. Defaults render via the Iconify
///   [`<iconify-icon>`](https://iconify.design/docs/iconify-icon/) web
///   component using Google's Material Icons (`ic:` prefix); loading the
///   Iconify Icon script is the host app's responsibility.
///
/// # Arguments
///
/// * `input` - The Universal Markdown source text
///
/// # Returns
///
/// HTML string
///
/// # JavaScript Example
///
/// ```javascript
/// import init, { parse } from './umd.js';
///
/// await init();
/// const html = parse('# Hello World\n\nThis is **bold** text.');
/// const html2 = parse('# Link\n\n[docs](/docs)', JSON.stringify({
///   baseUrl: '/app',
///   maxInlineNesting: 4,
///   icons: {
///     colorSwatch: '<iconify-icon icon="ic:baseline-colorize" aria-hidden="true"></iconify-icon>'
///   }
/// }));
/// console.log(html);
/// ```
#[wasm_bindgen(js_name = parse)]
pub fn parse_wasm(input: &str, options_json: Option<String>) -> String {
    parse_with_options_json(input, options_json.as_deref())
}

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

    #[test]
    fn test_basic_parse() {
        let input = "Hello World";
        let output = parse(input);
        assert!(output.contains("Hello World"));
    }

    #[test]
    fn test_html_injection_neutralized() {
        // Raw-HTML safety is comrak's job (render.unsafe = false), not a
        // pre-escape pass — see sanitizer.rs module docs. Comrak recognizes
        // the <script> tag as an HTML construct and drops it during render.
        let input = "<script>alert('xss')</script>";
        let output = parse(input);
        assert!(!output.contains("<script>"));
        assert!(output.contains("<!-- raw HTML omitted -->"));
    }

    #[test]
    fn test_blockquote_survives_sanitization() {
        // Regression test: an earlier pre-escape pass turned every
        // line-leading `>` into `&gt;` before comrak could parse it as a
        // blockquote marker, silently breaking both plain blockquotes and
        // GFM-alert-style ([!NOTE] etc.) blocks end-to-end.
        let input = "> A plain blockquote";
        let output = parse(input);
        assert!(output.contains("<blockquote"));
        assert!(output.contains("A plain blockquote"));
    }

    #[test]
    fn test_parse_with_options_json_base_url() {
        let input = "[docs](/guide)";
        let output = parse_with_options_json(input, Some(r#"{"baseUrl":"/app"}"#));
        assert!(output.contains(r#"href="/app/guide""#));
    }

    #[test]
    fn test_parse_with_options_json_icon_override() {
        let input = "`#ff0000`";
        let output = parse_with_options_json(
            input,
            Some(
                r#"{"icons":{"colorSwatch":"<span class=\"my-icon\" aria-hidden=\"true\"></span>"}}"#,
            ),
        );
        assert!(output.contains(r#"<span class="my-icon" aria-hidden="true"></span>"#));
    }

    #[test]
    fn test_parse_with_options_json_inline_nesting_limit() {
        let input = "&color(blue){&abbr(t){x};};";
        let output_from_json = parse_with_options_json(input, Some(r#"{"maxInlineNesting":1}"#));

        let mut options = parser::ParserOptions::default();
        options.max_inline_nesting = Some(1);
        let expected = parse_with_frontmatter_opts(input, &options);
        let expected_html = if let Some(footnotes) = expected.footnotes {
            format!("{}\n{}", expected.html, footnotes)
        } else {
            expected.html
        };

        assert_eq!(output_from_json, expected_html);
    }
}