Skip to main content

dioxus_mdx/parser/
mod.rs

1//! MDX documentation parser for extracting frontmatter and components.
2//!
3//! This module provides functionality to parse MDX (Markdown with JSX) content,
4//! extracting YAML frontmatter and converting custom components like Cards, Tabs,
5//! Steps, and Callouts into an intermediate representation for rendering.
6
7mod accordion;
8mod callout;
9mod card;
10mod code_group;
11mod content;
12mod fields;
13mod frontmatter;
14mod heading;
15mod openapi_parser;
16mod openapi_tag;
17mod openapi_types;
18mod steps;
19mod tabs;
20mod types;
21mod update;
22mod utils;
23
24pub use content::{get_raw_markdown, parse_mdx};
25pub use frontmatter::extract_frontmatter;
26pub use heading::strip_leading_h1;
27pub use openapi_parser::{OpenApiError, parse_openapi};
28pub use openapi_types::*;
29pub use types::*;
30
31/// Parse a complete MDX document, extracting frontmatter and content.
32///
33/// This is the main entry point for parsing MDX content. It extracts
34/// YAML frontmatter from the beginning of the document and parses the
35/// remaining content into a tree of `DocNode` elements.
36pub fn parse_document(content: &str) -> ParsedDoc {
37    let (frontmatter, remaining) = extract_frontmatter(content);
38    // Consumer layouts render the frontmatter title in their own <h1>; drop a
39    // duplicate body H1 so the page emits exactly one.
40    let body = strip_leading_h1(remaining);
41    let nodes = parse_mdx(body);
42    let raw_markdown = get_raw_markdown(&nodes);
43
44    ParsedDoc {
45        frontmatter,
46        content: nodes,
47        raw_markdown,
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn parse_document_strips_duplicate_atx_h1() {
57        let content = "---\ntitle: Hello\n---\n\n# Hello\n\nbody text\n";
58        let doc = parse_document(content);
59        assert_eq!(doc.frontmatter.title, "Hello");
60        assert!(
61            !doc.raw_markdown.contains("# Hello"),
62            "expected leading H1 to be stripped, got: {:?}",
63            doc.raw_markdown
64        );
65        assert!(doc.raw_markdown.contains("body text"));
66    }
67
68    #[test]
69    fn parse_document_leaves_body_without_leading_h1_untouched() {
70        let content = "---\ntitle: Hello\n---\n\nintro paragraph\n\n## Subheading\n";
71        let doc = parse_document(content);
72        assert!(doc.raw_markdown.contains("intro paragraph"));
73        assert!(doc.raw_markdown.contains("## Subheading"));
74    }
75
76    #[test]
77    fn parse_document_preserves_h1_appearing_mid_document() {
78        let content = "---\ntitle: Hello\n---\n\nintro\n\n# Later heading\n\nmore body\n";
79        let doc = parse_document(content);
80        assert!(doc.raw_markdown.contains("intro"));
81        assert!(
82            doc.raw_markdown.contains("# Later heading"),
83            "mid-document H1 should survive, got: {:?}",
84            doc.raw_markdown
85        );
86    }
87}