markdown-syntax 0.1.2

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
//! AST validation: [`Document::validate`] walks the tree and reports each
//! invalid or unsupported node shape as a [`Diagnostic`]. Serialization and HTML
//! rendering run this first and refuse an invalid document.

use alloc::vec::Vec;

use crate::{
    ast::{
        Autolink, AutolinkKind, Block, CodeInline, ContainerDirective, DirectiveAttribute,
        Document, Escape, Heading, Inline, LeafDirective, List, MathInlineKind, Table,
        TextDirective,
    },
    diagnostic::Diagnostic,
    span::Span,
};

impl Document {
    /// Validate this document's AST shape, returning a diagnostic for each
    /// invalid or unsupported node (empty when the document is well-formed).
    pub fn validate(&self) -> Vec<Diagnostic> {
        validate_document(self)
    }
}

pub(crate) fn validate_document(document: &Document) -> Vec<Diagnostic> {
    let mut diagnostics = Vec::new();
    for block in &document.children {
        validate_block(block, &mut diagnostics);
    }
    diagnostics
}

fn validate_block(block: &Block, diagnostics: &mut Vec<Diagnostic>) {
    match block {
        Block::Paragraph(paragraph) => validate_inlines(&paragraph.children, diagnostics),
        Block::Heading(heading) => validate_heading(heading, diagnostics),
        Block::BlockQuote(block_quote) => {
            for child in &block_quote.children {
                validate_block(child, diagnostics);
            }
        }
        Block::Alert(alert) => {
            for child in &alert.children {
                validate_block(child, diagnostics);
            }
        }
        Block::List(list) => {
            validate_list_start(list, diagnostics);
            for item in &list.children {
                for child in &item.children {
                    validate_block(child, diagnostics);
                }
            }
        }
        Block::DescriptionList(list) => {
            for item in &list.children {
                validate_inlines(&item.term, diagnostics);
                if item.details.is_empty() {
                    diagnostics.push(Diagnostic::invalid(
                        item.meta.span,
                        "description item must contain at least one details block",
                    ));
                }
                for details in &item.details {
                    for child in &details.children {
                        validate_block(child, diagnostics);
                    }
                }
            }
        }
        Block::Table(table) => validate_table(table, diagnostics),
        Block::FootnoteDefinition(definition) => {
            if definition.identifier.is_empty() {
                diagnostics.push(Diagnostic::invalid(
                    definition.meta.span,
                    "footnote definition identifier cannot be empty",
                ));
            }
            for child in &definition.children {
                validate_block(child, diagnostics);
            }
        }
        Block::Definition(definition) => {
            if definition.identifier.trim().is_empty() {
                diagnostics.push(Diagnostic::invalid(
                    definition.meta.span,
                    "definition identifier cannot be empty",
                ));
            }
        }
        Block::LeafDirective(directive) => validate_leaf_directive(directive, diagnostics),
        Block::ContainerDirective(directive) => {
            validate_container_directive(directive, diagnostics)
        }
        Block::ThematicBreak(_)
        | Block::CodeBlock(_)
        | Block::HtmlBlock(_)
        | Block::MathBlock(_)
        | Block::Frontmatter(_)
        | Block::MdxEsm(_)
        | Block::MdxExpression(_)
        | Block::MdxJsx(_) => {}
    }
}

fn validate_heading(heading: &Heading, diagnostics: &mut Vec<Diagnostic>) {
    if heading.depth == 0 || heading.depth > 6 {
        diagnostics.push(Diagnostic::invalid(
            heading.meta.span,
            "heading depth must be in the range 1..=6",
        ));
    }
    validate_inlines(&heading.children, diagnostics);
}

fn validate_table(table: &Table, diagnostics: &mut Vec<Diagnostic>) {
    if table.rows.is_empty() {
        diagnostics.push(Diagnostic::invalid(
            table.meta.span,
            "table must contain at least a header row",
        ));
        return;
    }

    let width = table.rows[0].cells.len();
    if width == 0 {
        diagnostics.push(Diagnostic::invalid(
            table.meta.span,
            "table header row must contain at least one cell",
        ));
    }

    if table.alignments.len() != width {
        diagnostics.push(Diagnostic::invalid(
            table.meta.span,
            "table alignment count must match header width",
        ));
    }

    for row in &table.rows {
        if row.cells.len() != width {
            diagnostics.push(Diagnostic::invalid(
                row.meta.span,
                "table row width must match header width",
            ));
        }
        for cell in &row.cells {
            validate_inlines(&cell.children, diagnostics);
        }
    }
}

fn validate_leaf_directive(directive: &LeafDirective, diagnostics: &mut Vec<Diagnostic>) {
    validate_directive_name(directive.meta.span, &directive.name, diagnostics);
    validate_directive_attributes(&directive.attributes, diagnostics);
    validate_inlines(&directive.label, diagnostics);
}

fn validate_container_directive(directive: &ContainerDirective, diagnostics: &mut Vec<Diagnostic>) {
    validate_directive_name(directive.meta.span, &directive.name, diagnostics);
    validate_directive_attributes(&directive.attributes, diagnostics);
    validate_inlines(&directive.label, diagnostics);
    for child in &directive.children {
        validate_block(child, diagnostics);
    }
}

fn validate_inlines(inlines: &[Inline], diagnostics: &mut Vec<Diagnostic>) {
    if let Some(Inline::LineBreak(node)) = inlines.last() {
        diagnostics.push(Diagnostic::invalid(
            node.meta.span,
            "hard line break cannot be the final inline of its container",
        ));
    }
    for inline in inlines {
        match inline {
            Inline::Emphasis(node) => {
                validate_emphasis_container(&node.children, node.meta.span, diagnostics)
            }
            Inline::Strong(node) => {
                validate_emphasis_container(&node.children, node.meta.span, diagnostics)
            }
            Inline::Underline(node) => {
                validate_emphasis_container(&node.children, node.meta.span, diagnostics)
            }
            Inline::Delete(node) => {
                validate_emphasis_container(&node.children, node.meta.span, diagnostics)
            }
            Inline::Insert(node) => {
                validate_emphasis_container(&node.children, node.meta.span, diagnostics)
            }
            Inline::Mark(node) => {
                validate_emphasis_container(&node.children, node.meta.span, diagnostics)
            }
            Inline::Subscript(node) => {
                validate_emphasis_container(&node.children, node.meta.span, diagnostics)
            }
            Inline::Superscript(node) => {
                validate_emphasis_container(&node.children, node.meta.span, diagnostics)
            }
            Inline::Spoiler(node) => {
                validate_emphasis_container(&node.children, node.meta.span, diagnostics)
            }
            Inline::Shortcode(node) => {
                if node.name.is_empty() {
                    diagnostics.push(Diagnostic::invalid(
                        node.meta.span,
                        "shortcode name cannot be empty",
                    ));
                }
            }
            Inline::Link(node) => validate_inlines(&node.children, diagnostics),
            Inline::Image(node) => validate_inlines(&node.alt, diagnostics),
            Inline::LinkReference(node) => {
                if node.identifier.is_empty() {
                    diagnostics.push(Diagnostic::invalid(
                        node.meta.span,
                        "link reference identifier cannot be empty",
                    ));
                }
                validate_inlines(&node.children, diagnostics);
            }
            Inline::ImageReference(node) => {
                if node.identifier.is_empty() {
                    diagnostics.push(Diagnostic::invalid(
                        node.meta.span,
                        "image reference identifier cannot be empty",
                    ));
                }
                validate_inlines(&node.alt, diagnostics);
            }
            Inline::Escape(node) => validate_escape(node, diagnostics),
            Inline::CharacterReference(node) => {
                if node.reference.is_empty() {
                    diagnostics.push(Diagnostic::invalid(
                        node.meta.span,
                        "character reference source cannot be empty",
                    ));
                }
                if node.value.is_empty() {
                    diagnostics.push(Diagnostic::invalid(
                        node.meta.span,
                        "character reference value cannot be empty",
                    ));
                }
            }
            Inline::TextDirective(node) => validate_text_directive(node, diagnostics),
            Inline::FootnoteReference(node) => {
                if node.identifier.is_empty() {
                    diagnostics.push(Diagnostic::invalid(
                        node.meta.span,
                        "footnote reference identifier cannot be empty",
                    ));
                }
            }
            Inline::InlineFootnote(node) => validate_inlines(&node.children, diagnostics),
            Inline::WikiLink(node) => {
                if node.target.is_empty() {
                    diagnostics.push(Diagnostic::invalid(
                        node.meta.span,
                        "wikilink target cannot be empty",
                    ));
                }
            }
            Inline::Code(node) => validate_code_inline(node, diagnostics),
            Inline::Autolink(node) => validate_autolink(node, diagnostics),
            Inline::Math(node) => {
                if let MathInlineKind::Dollar { dollars: 0 } = node.kind {
                    diagnostics.push(Diagnostic::invalid(
                        node.meta.span,
                        "dollar-fenced inline math must have a fence length of at least 1",
                    ));
                }
            }
            Inline::Text(_)
            | Inline::Html(_)
            | Inline::SoftBreak(_)
            | Inline::LineBreak(_)
            | Inline::MdxExpression(_)
            | Inline::MdxJsx(_) => {}
        }
    }
}

fn validate_emphasis_container(
    children: &[Inline],
    span: Option<Span>,
    diagnostics: &mut Vec<Diagnostic>,
) {
    if children.is_empty() {
        diagnostics.push(Diagnostic::invalid(
            span,
            "emphasis-like inline container cannot have empty children",
        ));
    }
    validate_inlines(children, diagnostics);
}

fn validate_escape(escape: &Escape, diagnostics: &mut Vec<Diagnostic>) {
    if !escape.value.is_ascii_punctuation() {
        diagnostics.push(Diagnostic::invalid(
            escape.meta.span,
            "escaped value must be an ASCII punctuation character",
        ));
    }
}

fn validate_autolink(autolink: &Autolink, diagnostics: &mut Vec<Diagnostic>) {
    // GFM literal autolinks carry a synthesized destination that MAY contain
    // `>` (the renderer percent-encodes it). Only angle-bracket autolinks
    // forbid whitespace, `<`, and `>` in the destination.
    if matches!(autolink.kind, AutolinkKind::GfmLiteral { .. }) {
        return;
    }
    if autolink
        .destination
        .chars()
        .any(|char| char.is_whitespace() || char == '<' || char == '>')
    {
        diagnostics.push(Diagnostic::invalid(
            autolink.meta.span,
            "autolink destination cannot contain whitespace, `<`, or `>`",
        ));
    }
}

fn validate_code_inline(code: &CodeInline, diagnostics: &mut Vec<Diagnostic>) {
    if code.fence_length == 0 {
        return;
    }
    // A code span fence of length N is closed only by a backtick run of exactly
    // length N. A run shorter or longer than the fence is inert, so only an
    // exactly-matching interior run would close the raw passthrough early.
    if raw_has_backtick_run(&code.raw, code.fence_length) {
        diagnostics.push(Diagnostic::invalid(
            code.meta.span,
            "inline code raw passthrough contains a backtick run equal to its fence length",
        ));
    }
}

fn raw_has_backtick_run(input: &str, length: usize) -> bool {
    let mut current = 0;
    for byte in input.bytes() {
        if byte == b'`' {
            current += 1;
        } else {
            if current == length {
                return true;
            }
            current = 0;
        }
    }
    current == length
}

fn validate_list_start(list: &List, diagnostics: &mut Vec<Diagnostic>) {
    if !list.ordered {
        return;
    }
    let Some(start) = list.start else {
        return;
    };
    if start > 999_999_999 {
        diagnostics.push(Diagnostic::invalid(
            list.meta.span,
            "ordered list start must be representable in at most 9 digits",
        ));
    }
}

fn validate_text_directive(directive: &TextDirective, diagnostics: &mut Vec<Diagnostic>) {
    validate_directive_name(directive.meta.span, &directive.name, diagnostics);
    validate_directive_attributes(&directive.attributes, diagnostics);
    validate_inlines(&directive.label, diagnostics);
}

fn validate_directive_name(span: Option<Span>, name: &str, diagnostics: &mut Vec<Diagnostic>) {
    if !is_directive_name(name) {
        diagnostics.push(Diagnostic::invalid(
            span,
            "directive name must start with a letter and contain letters, digits, `_`, or `-`",
        ));
    }
}

fn validate_directive_attributes(
    attributes: &[DirectiveAttribute],
    diagnostics: &mut Vec<Diagnostic>,
) {
    for attribute in attributes {
        if !is_attribute_name(&attribute.name) {
            diagnostics.push(Diagnostic::invalid(
                None,
                "directive attribute name must start with a letter, `_`, or `-`",
            ));
        }
    }
}

pub(crate) fn is_directive_name(name: &str) -> bool {
    let mut chars = name.chars();
    let Some(first) = chars.next() else {
        return false;
    };
    if !first.is_ascii_alphabetic() {
        return false;
    }
    chars.all(|char| char.is_ascii_alphanumeric() || char == '_' || char == '-')
}

pub(crate) fn is_attribute_name(name: &str) -> bool {
    let mut chars = name.chars();
    let Some(first) = chars.next() else {
        return false;
    };
    if !(first.is_ascii_alphabetic() || first == '_' || first == '-') {
        return false;
    }
    chars.all(|char| char.is_ascii_alphanumeric() || char == '_' || char == '-' || char == ':')
}