markdown-syntax 0.1.1

no_std + alloc Markdown syntax parser and serializer
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
//! Parser configuration: which Markdown constructs are recognized and how.
//!
//! [`SyntaxOptions`] is the entry point — pick a preset, optionally tune it with
//! the [`Construct`] builder, then call [`SyntaxOptions::parse`]. [`Constructs`]
//! is the exhaustive per-feature flag set behind it, and [`ParseOptions`] holds
//! the lexing knobs.

use alloc::string::String;

/// The full set of syntactic constructs the parser may recognize, one boolean
/// per feature. This is the exhaustive escape hatch; most callers use the
/// [`Constructs::commonmark`]/[`gfm`](Constructs::gfm)/[`mdx`](Constructs::mdx)/
/// [`max`](Constructs::max) presets or the [`Construct`] builder instead of
/// setting fields directly.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Constructs {
    /// Raw HTML blocks, e.g. a `<div>…</div>` block at the top level.
    pub html_block: bool,
    /// Raw inline HTML, e.g. `<span>` within a paragraph.
    pub html_inline: bool,
    /// Indented code blocks (each line indented four spaces or a tab).
    pub indented_code: bool,
    /// GFM pipe tables: a `| a | b |` row over a `|---|---|` delimiter row.
    pub gfm_table: bool,
    /// GFM task list items: `- [ ]` (unchecked) and `- [x]` (checked).
    pub gfm_task_list_item: bool,
    /// GFM strikethrough: `~~text~~`.
    pub gfm_strikethrough: bool,
    /// GFM literal autolinks: a bare `https://…`, `www.…`, or email becomes a
    /// link without angle brackets.
    pub gfm_autolink_literal: bool,
    /// cmark-gfm "relaxed" URL autolinks: bare `scheme://` URLs (and a bare
    /// leading `://`) are auto-linkified without angle brackets, e.g. `smb://`,
    /// `irc://`, `rdar://`. This is a cmark extension beyond the GFM spec (which
    /// defines only `http(s)://`/`www.`/email); on by default in `gfm()` for
    /// GitHub/cmark-gfm parity. The angle form `<scheme:…>` works regardless.
    pub relaxed_autolinks: bool,
    /// GFM alerts: a `> [!NOTE]` (TIP/IMPORTANT/WARNING/CAUTION) blockquote.
    pub gfm_alert: bool,
    /// Underline spans: `__text__`. This overrides CommonMark's `__`-as-strong,
    /// so it is off in the [`max`](Constructs::max) default.
    pub underline: bool,
    /// CriticMarkup-style insertions: `++text++`.
    pub insert: bool,
    /// Highlight / "mark" spans: `==text==`.
    pub highlight: bool,
    /// Subscript: a single-tilde span `~text~` (no spaces).
    pub subscript: bool,
    /// Superscript: `^text^`.
    pub superscript: bool,
    /// Spoiler spans: `||text||`.
    pub spoiler: bool,
    /// Emoji-style shortcodes: `:tada:`.
    pub shortcode: bool,
    /// Description (definition) lists: a term followed by `:`-led details.
    pub description_list: bool,
    /// Footnote definitions: `[^1]: the footnote body`.
    pub footnote_definition: bool,
    /// Footnote references: `[^1]` in running text.
    pub footnote_reference: bool,
    /// Inline footnotes: `^[the note inline]` (also needs `footnote_reference`).
    pub inline_footnote: bool,
    /// Block math: a `$$ … $$` fenced block.
    pub math_block: bool,
    /// Inline math: `$x$` (and the math-code form `` $`x`$ ``).
    pub math_inline: bool,
    /// A leading frontmatter block at the start of the document: `---` YAML or
    /// `+++` TOML.
    pub frontmatter: bool,
    /// Wikilinks with the display title after the pipe: `[[target|title]]`
    /// (the Obsidian convention). Mutually exclusive with the before-pipe order.
    pub wikilink_title_after_pipe: bool,
    /// Wikilinks with the display title before the pipe: `[[title|target]]`.
    /// Mutually exclusive with the after-pipe order.
    pub wikilink_title_before_pipe: bool,
    /// MDX ESM: `import`/`export` statement lines.
    pub mdx_esm: bool,
    /// MDX block-level `{ … }` expressions.
    pub mdx_expression_block: bool,
    /// MDX inline `{ … }` expressions within text.
    pub mdx_expression_inline: bool,
    /// MDX block-level JSX: `<Component/>` as a block. Conflicts with raw HTML.
    pub mdx_jsx_block: bool,
    /// MDX inline JSX: `<Component/>` within text. Conflicts with raw HTML.
    pub mdx_jsx_inline: bool,
    /// Inline directive: `:name[label]{key=val}`. A directive, not MDX.
    pub directive_text: bool,
    /// Leaf directive: `::name[label]{key=val}` on its own line. A directive,
    /// not MDX.
    pub directive_leaf: bool,
    /// Container directive: a `:::name … :::` fenced block. A directive, not MDX.
    pub directive_container: bool,
}

impl Constructs {
    /// The CommonMark baseline: raw HTML and indented code, no extensions.
    pub const fn commonmark() -> Self {
        Self {
            html_block: true,
            html_inline: true,
            indented_code: true,
            gfm_table: false,
            gfm_task_list_item: false,
            gfm_strikethrough: false,
            gfm_autolink_literal: false,
            relaxed_autolinks: false,
            gfm_alert: false,
            underline: false,
            insert: false,
            highlight: false,
            subscript: false,
            superscript: false,
            spoiler: false,
            shortcode: false,
            description_list: false,
            footnote_definition: false,
            footnote_reference: false,
            inline_footnote: false,
            math_block: false,
            math_inline: false,
            frontmatter: false,
            wikilink_title_after_pipe: false,
            wikilink_title_before_pipe: false,
            mdx_esm: false,
            mdx_expression_block: false,
            mdx_expression_inline: false,
            mdx_jsx_block: false,
            mdx_jsx_inline: false,
            directive_text: false,
            directive_leaf: false,
            directive_container: false,
        }
    }

    /// GitHub Flavored Markdown: CommonMark plus tables, task lists,
    /// strikethrough, literal autolinks, and footnotes.
    pub const fn gfm() -> Self {
        let mut constructs = Self::commonmark();
        constructs.gfm_table = true;
        constructs.gfm_task_list_item = true;
        constructs.gfm_strikethrough = true;
        constructs.gfm_autolink_literal = true;
        constructs.relaxed_autolinks = true;
        constructs.footnote_definition = true;
        constructs.footnote_reference = true;
        constructs
    }

    /// MDX: CommonMark with raw HTML and indented code off, and MDX ESM,
    /// expressions, and JSX on.
    pub const fn mdx() -> Self {
        let mut constructs = Self::commonmark();
        constructs.html_block = false;
        constructs.html_inline = false;
        constructs.indented_code = false;
        constructs.mdx_esm = true;
        constructs.mdx_expression_block = true;
        constructs.mdx_expression_inline = true;
        constructs.mdx_jsx_block = true;
        constructs.mdx_jsx_inline = true;
        constructs
    }

    /// The maximal non-MDX construct set, and the default dialect: every
    /// construct that does not reinterpret a core CommonMark delimiter. MDX is
    /// off (it conflicts with raw HTML and reinterprets `{…}`/`<…>`), and
    /// `underline` is off because it would parse `__bold__` as underline,
    /// overriding CommonMark strong. The wikilink title order is after-pipe.
    pub const fn max() -> Self {
        Self {
            html_block: true,
            html_inline: true,
            indented_code: true,
            gfm_table: true,
            gfm_task_list_item: true,
            gfm_strikethrough: true,
            gfm_autolink_literal: true,
            relaxed_autolinks: true,
            gfm_alert: true,
            underline: false,
            insert: true,
            highlight: true,
            subscript: true,
            superscript: true,
            spoiler: true,
            shortcode: true,
            description_list: true,
            footnote_definition: true,
            footnote_reference: true,
            inline_footnote: true,
            math_block: true,
            math_inline: true,
            frontmatter: true,
            wikilink_title_after_pipe: true,
            wikilink_title_before_pipe: false,
            mdx_esm: false,
            mdx_expression_block: false,
            mdx_expression_inline: false,
            mdx_jsx_block: false,
            mdx_jsx_inline: false,
            directive_text: true,
            directive_leaf: true,
            directive_container: true,
        }
    }
}

impl Default for Constructs {
    fn default() -> Self {
        Self::max()
    }
}

/// Lexing knobs that tune how existing constructs are read or how source text is
/// preserved, separate from which constructs are recognized ([`Constructs`]).
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct ParseOptions {
    /// Treat a single `~text~` as strikethrough (in addition to `~~text~~`).
    /// Inert unless `gfm_strikethrough` is also enabled.
    pub single_tilde_strikethrough: bool,
    /// Keep backslash character escapes (e.g. `\*`) as `Escape` nodes instead of
    /// folding them into text, so the original source can be reproduced.
    pub preserve_character_escapes: bool,
    /// Keep character references (e.g. `&amp;`) as `CharacterReference` nodes
    /// instead of resolving them to their value.
    pub preserve_character_references: bool,
}

/// A full syntax configuration: which [`Constructs`] are recognized plus the
/// [`ParseOptions`] lexing knobs. Build one with a preset
/// ([`commonmark`](SyntaxOptions::commonmark)/[`gfm`](SyntaxOptions::gfm)/
/// [`mdx`](SyntaxOptions::mdx)/[`default`](SyntaxOptions::default)), optionally
/// tune it with [`enable`](SyntaxOptions::enable)/[`disable`](SyntaxOptions::disable),
/// then call [`parse`](SyntaxOptions::parse).
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SyntaxOptions {
    /// Which syntactic constructs are recognized.
    pub constructs: Constructs,
    /// Lexing / source-preservation knobs.
    pub parse: ParseOptions,
}

impl SyntaxOptions {
    /// The strict CommonMark dialect.
    pub fn commonmark() -> Self {
        Self {
            constructs: Constructs::commonmark(),
            parse: ParseOptions::default(),
        }
    }

    /// GitHub Flavored Markdown (also enables single-tilde strikethrough).
    pub fn gfm() -> Self {
        Self {
            constructs: Constructs::gfm(),
            parse: ParseOptions {
                single_tilde_strikethrough: true,
                preserve_character_escapes: false,
                preserve_character_references: false,
            },
        }
    }

    /// The MDX dialect (JSX, expressions, ESM; no raw HTML).
    pub fn mdx() -> Self {
        Self {
            constructs: Constructs::mdx(),
            parse: ParseOptions::default(),
        }
    }

    /// Enable a [`Construct`] on top of these options, returning the modified
    /// options for chaining. Grouped constructs (footnotes, math, directives, …)
    /// flip every flag in the group so no member is left silently inert.
    pub fn enable(mut self, construct: Construct) -> Self {
        construct.apply(&mut self.constructs, true);
        self
    }

    /// Disable a [`Construct`], the inverse of [`SyntaxOptions::enable`].
    pub fn disable(mut self, construct: Construct) -> Self {
        construct.apply(&mut self.constructs, false);
        self
    }

    /// Check for contradictory construct combinations (MDX JSX with raw HTML;
    /// both wikilink title orders). Returns `Ok(())` for every preset; only a
    /// hand-built config can trip a [`SyntaxConfigError`].
    pub fn validate(&self) -> Result<(), SyntaxConfigError> {
        if (self.constructs.mdx_jsx_block || self.constructs.mdx_jsx_inline)
            && (self.constructs.html_block || self.constructs.html_inline)
        {
            return Err(SyntaxConfigError::MdxHtmlConflict);
        }
        if self.constructs.wikilink_title_after_pipe && self.constructs.wikilink_title_before_pipe {
            return Err(SyntaxConfigError::WikilinkTitleOrderConflict);
        }

        Ok(())
    }
}

impl Default for SyntaxOptions {
    fn default() -> Self {
        Self {
            constructs: Constructs::max(),
            parse: ParseOptions::default(),
        }
    }
}

/// Where a wikilink's display title sits relative to the `|` separator. The two
/// orders are mutually exclusive ([`SyntaxConfigError::WikilinkTitleOrderConflict`]).
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum WikiLinkOrder {
    /// `[[target|title]]` — the Obsidian convention, and the maximal default.
    TitleAfterPipe,
    /// `[[title|target]]`.
    TitleBeforePipe,
}

/// A discoverable, typo-proof front door for toggling a syntax feature via
/// [`SyntaxOptions::enable`] / [`SyntaxOptions::disable`]. Each variant maps to
/// one conceptual feature; grouped features flip every underlying [`Constructs`]
/// flag together. The raw [`Constructs`] struct remains the exhaustive escape
/// hatch for fine-grained control.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum Construct {
    /// GFM pipe tables: `| a | b |` over `|---|---|`.
    Table,
    /// GFM task list items: `- [ ]` / `- [x]`.
    TaskList,
    /// Strikethrough: `~~text~~`.
    Strikethrough,
    /// GFM literal autolinks plus the cmark relaxed `scheme://` extension.
    Autolink,
    /// GFM alerts: `> [!NOTE]` callouts.
    Alert,
    /// Footnote definitions, references, and inline footnotes.
    Footnotes,
    /// Inline and block math.
    Math,
    /// A leading `---`/`+++` frontmatter block.
    Frontmatter,
    /// Underline: `__text__` (overrides CommonMark strong).
    Underline,
    /// Insertions: `++text++`.
    Insert,
    /// Highlight / mark: `==text==`.
    Highlight,
    /// Subscript: `~text~`.
    Subscript,
    /// Superscript: `^text^`.
    Superscript,
    /// Spoilers: `||text||`.
    Spoiler,
    /// Emoji-style shortcodes: `:tada:`.
    Shortcode,
    /// Description / definition lists.
    DescriptionList,
    /// Wikilinks `[[…]]` with the given title order.
    Wikilinks(WikiLinkOrder),
    /// MDX JSX (block and inline). Conflicts with raw HTML; pair with
    /// `disable`-ing HTML or start from [`SyntaxOptions::mdx`].
    MdxJsx,
    /// MDX `{…}` expressions (block and inline).
    MdxExpressions,
    /// MDX ESM `import`/`export` lines.
    MdxEsm,
    /// The `:name` / `::name` / `:::name` directive family.
    Directives,
}

impl Construct {
    fn apply(self, c: &mut Constructs, on: bool) {
        match self {
            Construct::Table => c.gfm_table = on,
            Construct::TaskList => c.gfm_task_list_item = on,
            Construct::Strikethrough => c.gfm_strikethrough = on,
            Construct::Autolink => {
                c.gfm_autolink_literal = on;
                c.relaxed_autolinks = on;
            }
            Construct::Alert => c.gfm_alert = on,
            Construct::Footnotes => {
                c.footnote_definition = on;
                c.footnote_reference = on;
                c.inline_footnote = on;
            }
            Construct::Math => {
                c.math_block = on;
                c.math_inline = on;
            }
            Construct::Frontmatter => c.frontmatter = on,
            Construct::Underline => c.underline = on,
            Construct::Insert => c.insert = on,
            Construct::Highlight => c.highlight = on,
            Construct::Subscript => c.subscript = on,
            Construct::Superscript => c.superscript = on,
            Construct::Spoiler => c.spoiler = on,
            Construct::Shortcode => c.shortcode = on,
            Construct::DescriptionList => c.description_list = on,
            Construct::Wikilinks(order) => {
                c.wikilink_title_after_pipe = on && matches!(order, WikiLinkOrder::TitleAfterPipe);
                c.wikilink_title_before_pipe =
                    on && matches!(order, WikiLinkOrder::TitleBeforePipe);
            }
            Construct::MdxJsx => {
                c.mdx_jsx_block = on;
                c.mdx_jsx_inline = on;
            }
            Construct::MdxExpressions => {
                c.mdx_expression_block = on;
                c.mdx_expression_inline = on;
            }
            Construct::MdxEsm => c.mdx_esm = on,
            Construct::Directives => {
                c.directive_text = on;
                c.directive_leaf = on;
                c.directive_container = on;
            }
        }
    }
}

/// A contradictory [`SyntaxOptions`] configuration, reported by
/// [`SyntaxOptions::validate`].
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum SyntaxConfigError {
    /// MDX JSX and raw HTML were both enabled; they both claim `<`.
    MdxHtmlConflict,
    /// Both wikilink title orders (before- and after-pipe) were enabled.
    WikilinkTitleOrderConflict,
}

impl SyntaxConfigError {
    /// A human-readable description of the conflict.
    pub fn message(&self) -> String {
        match self {
            Self::MdxHtmlConflict => "MDX JSX and raw HTML syntax cannot both be enabled".into(),
            Self::WikilinkTitleOrderConflict => {
                "wikilink title-before-pipe and title-after-pipe cannot both be enabled".into()
            }
        }
    }
}