Skip to main content

dioxus_mdx/
lib.rs

1//! # dioxus-mdx
2//!
3//! MDX parsing and rendering components for Dioxus applications.
4//!
5//! This crate provides a complete solution for rendering Mintlify-style MDX documentation
6//! in Dioxus applications, including:
7//!
8//! - **Parser**: Extracts frontmatter, code blocks, and custom components from MDX
9//! - **Components**: Pre-built Dioxus components for callouts, cards, tabs, steps, etc.
10//! - **Syntax Highlighting**: Code blocks with language-aware highlighting
11//!
12//! ## Quick Start
13//!
14//! ```rust,ignore
15//! use dioxus::prelude::*;
16//! use dioxus_mdx::{parse_document, MdxContent};
17//!
18//! #[component]
19//! fn DocsPage(content: String) -> Element {
20//!     rsx! {
21//!         MdxContent { content }
22//!     }
23//! }
24//! ```
25//!
26//! ## Parsing Only
27//!
28//! If you want to parse MDX without using the components:
29//!
30//! ```rust
31//! use dioxus_mdx::{parse_document, parse_mdx, DocNode};
32//!
33//! let mdx_content = r#"---
34//! title: Getting Started
35//! ---
36//!
37//! <Tip>This is a helpful tip!</Tip>
38//!
39//! ## Introduction
40//!
41//! Welcome to the documentation.
42//! "#;
43//!
44//! // Parse with frontmatter
45//! let doc = parse_document(mdx_content);
46//! assert_eq!(doc.frontmatter.title, "Getting Started");
47//!
48//! // Parse content only
49//! let nodes = parse_mdx("## Hello\n\n<Note>A note</Note>");
50//! ```
51//!
52//! ## Supported Components
53//!
54//! - **Callouts**: `<Tip>`, `<Note>`, `<Warning>`, `<Info>`
55//! - **Cards**: `<Card>`, `<CardGroup>`
56//! - **Tabs**: `<Tabs>`, `<Tab>`
57//! - **Steps**: `<Steps>`, `<Step>`
58//! - **Accordion**: `<AccordionGroup>`, `<Accordion>`
59//! - **Code**: `<CodeGroup>`, fenced code blocks with syntax highlighting
60//! - **API Docs**: `<ParamField>`, `<ResponseField>`, `<Expandable>`
61//! - **Examples**: `<RequestExample>`, `<ResponseExample>`
62//! - **Changelog**: `<Update>`
63//!
64//! ## Styling
65//!
66//! Components use Tailwind CSS with DaisyUI classes. Ensure your project has
67//! Tailwind and DaisyUI configured. The components use:
68//!
69//! - Base/neutral classes: `bg-base-200`, `text-base-content`, etc.
70//! - Color classes: `text-primary`, `bg-success/10`, etc.
71//! - Typography: `prose`, `prose-sm`
72//!
73//! ## Features
74//!
75//! - `web` (default): Enables web-specific features like clipboard copy
76//! - `mermaid` (default): Renders ` ```mermaid ` fences as diagrams
77//! - `highlight` (default): Syntax highlighting via `dioxus-code`
78//! - `lang-*`: One tree-sitter grammar each. `highlight` alone highlights only
79//!   Rust; the default set adds `lang-bash`, `lang-css`, `lang-dockerfile`,
80//!   `lang-html`, `lang-javascript`, `lang-json`, `lang-markdown`,
81//!   `lang-python`, `lang-toml`, `lang-typescript` and `lang-yaml`.
82//!   `lang-c-sharp`, `lang-cpp` and `lang-tsx` exist but are off by default. For any other
83//!   language, depend on `dioxus-code` directly with its `lang-*` flag
84//!   (`features = ["runtime", "lang-go"]`); cargo unifies it into the copy this
85//!   crate uses, so `Language::from_slug` resolves the grammar.
86//!   A fence whose grammar is not compiled in renders as plain text.
87//! - `openapi` (default): Parses OpenAPI specs — `parse_openapi` and inline
88//!   `<OpenAPI>…</OpenAPI>` blocks. Turning it off drops `openapiv3` and
89//!   `serde_yaml` from the build; the `OpenApiSpec` types and the viewer
90//!   components stay, and an `<OpenAPI>` block renders as plain markdown.
91//!
92//! ## Custom Link Handling
93//!
94//! For internal navigation, components accept an `on_link` callback:
95//!
96//! ```rust,ignore
97//! use dioxus::prelude::*;
98//! use dioxus_mdx::DocCardGroup;
99//!
100//! #[component]
101//! fn DocsPage(group: CardGroupNode) -> Element {
102//!     let nav = use_navigator();
103//!
104//!     rsx! {
105//!         DocCardGroup {
106//!             group,
107//!             on_link: move |href: String| nav.push(&href),
108//!         }
109//!     }
110//! }
111//! ```
112
113pub mod components;
114pub mod parser;
115mod re;
116
117// Re-export parser types and functions
118pub use parser::{
119    AccordionGroupNode, AccordionNode, ApiInfo, ApiOperation, ApiParameter, ApiRequestBody,
120    ApiResponse, ApiServer, ApiTag, CalloutNode, CalloutType, CardGroupNode, CardNode,
121    CodeBlockNode, CodeGroupNode, DocFrontmatter, DocNode, ExpandableNode, HttpMethod,
122    MediaTypeContent, OpenApiNode, OpenApiSpec, ParamFieldNode, ParamLocation, ParameterLocation,
123    ParsedDoc, RequestExampleNode, ResponseExampleNode, ResponseFieldNode, SchemaDefinition,
124    SchemaType, StepNode, StepsNode, TabNode, TabsNode, UpdateNode, YamlLiteError, YamlMap,
125    YamlValue, extract_frontmatter, get_raw_markdown, parse_document, parse_mdx, parse_yaml_lite,
126    strip_leading_h1,
127};
128
129// Spec parsing lives behind the `openapi` feature (default); the `OpenApiSpec`
130// types and the viewer components above are always available.
131#[cfg(feature = "openapi")]
132pub use parser::{OpenApiError, parse_openapi};
133
134// Re-export the syntax-highlighting theme types so consumers can build a
135// `CodeThemeOverride` without depending on `dioxus-code` directly. Only available
136// with the `highlight` feature (default), which pulls in `dioxus-code`.
137#[cfg(feature = "highlight")]
138pub use dioxus_code::{CodeTheme, Theme};
139
140// Re-export components
141pub use components::{
142    ApiInfoHeader, DocAccordionGroup, DocAccordionItem, DocCallout, DocCard, DocCardGroup,
143    DocCodeBlock, DocCodeGroup, DocContent, DocExpandable, DocNodeRenderer, DocParamField,
144    DocRequestExample, DocResponseExample, DocResponseField, DocSteps, DocTableOfContents, DocTabs,
145    DocUpdate, EndpointCard, EndpointPage, MdxContent, MdxIcon, MdxRenderer, MethodBadge,
146    OpenApiViewer, ParameterItem, ParametersList, RequestBodySection, ResponseItem, ResponsesList,
147    SchemaDefinitions, SchemaTypeLabel, SchemaViewer, TagGroup, UngroupedEndpoints,
148    extract_headers, slugify,
149};
150
151// `CodeThemeOverride` wraps a `dioxus-code` type, so it's only available with the
152// `highlight` feature (default).
153#[cfg(feature = "highlight")]
154pub use components::CodeThemeOverride;
155
156#[cfg(feature = "mermaid")]
157pub use components::MermaidDiagram;