mdx_gen/lib.rs
1#![forbid(unsafe_code)]
2#![deny(missing_docs)]
3// src/lib.rs
4#![doc = include_str!("../README.md")]
5#![doc(
6 html_favicon_url = "https://cloudcdn.pro/mdx-gen/v1/favicon.ico",
7 html_logo_url = "https://cloudcdn.pro/mdx-gen/v1/logos/mdx-gen.svg",
8 html_root_url = "https://docs.rs/mdx-gen"
9)]
10#![crate_name = "mdx_gen"]
11#![crate_type = "lib"]
12
13/// The `error` module contains error types for Markdown processing.
14pub mod error;
15
16/// The `extensions` module contains custom block and table extensions.
17pub mod extensions;
18
19/// Mermaid diagram rendering for fenced `mermaid` code blocks.
20pub mod diagrams;
21
22/// Syntax highlighting adapter for comrak's plugin system.
23#[cfg(feature = "syntax_highlighting")]
24pub mod highlight;
25
26/// Validation primitives used by [`MarkdownOptions::validate`].
27pub mod validation;
28
29/// The `markdown` module contains the core processing pipeline.
30pub mod markdown;
31
32// ── Re-exports ──────────────────────────────────────────────────────
33
34pub use error::MarkdownError;
35
36/// Applies syntax highlighting to code (standalone usage).
37///
38/// # Example
39/// ```
40/// use mdx_gen::apply_syntax_highlighting;
41/// let highlighted = apply_syntax_highlighting("fn main() {}", "rust");
42/// ```
43#[cfg(feature = "syntax_highlighting")]
44pub use highlight::apply_syntax_highlighting;
45
46/// Generates a CSS stylesheet for a built-in syntect theme so that
47/// callers can render the class-based output produced by the
48/// highlighter and the comrak adapter.
49#[cfg(feature = "syntax_highlighting")]
50pub use highlight::theme_css;
51
52pub use extensions::collect_headings;
53pub use extensions::ColumnAlignment;
54pub use extensions::CustomBlockConfig;
55pub use extensions::CustomBlockType;
56pub use extensions::Heading;
57
58/// Structured validation error surfaced by
59/// [`MarkdownOptions::validate`]. The pipeline folds failing checks
60/// into [`MarkdownError::InvalidOptionsError`]; callers can inspect
61/// the structured form directly before running the pipeline.
62pub use validation::ValidationError;
63
64/// Builder that composes multiple validation checks into a single
65/// pass — used internally by [`MarkdownOptions::validate`].
66pub use validation::Validator;
67
68/// Script block that hydrates client-side `<pre class="mermaid">`
69/// containers into inline SVG. Embed once per page — typically
70/// before `</body>`.
71pub use diagrams::hydration_script_html;
72
73/// Processes a Markdown string and converts it into HTML.
74///
75/// # Example
76/// ```
77/// use mdx_gen::{process_markdown, MarkdownOptions};
78/// use comrak::Options;
79///
80/// let markdown_input = "# Hello, World!";
81/// let mut comrak_options = Options::default();
82/// comrak_options.extension.table = true;
83///
84/// let options = MarkdownOptions::default()
85/// .with_custom_blocks(false)
86/// .with_comrak_options(comrak_options);
87///
88/// let html_output = process_markdown(markdown_input, &options).expect("Failed to process markdown");
89/// assert!(html_output.contains("<h1>Hello, World!</h1>"));
90/// ```
91///
92/// # Errors
93///
94/// Returns a `MarkdownError` if options are invalid, input exceeds
95/// the size limit, or rendering fails.
96pub use markdown::process_markdown;
97
98/// Streams processed HTML directly to a `Write` sink.
99///
100/// See [`process_markdown`] for the pipeline semantics; this variant
101/// writes the output through a caller-provided writer instead of
102/// allocating a `String`.
103pub use markdown::process_markdown_to_writer;
104
105/// Extracts plain-text content from Markdown (strips all formatting).
106///
107/// # Example
108/// ```
109/// use mdx_gen::{process_markdown_to_plain_text, MarkdownOptions};
110/// let text = process_markdown_to_plain_text("# Hello\nWorld", &MarkdownOptions::default()).unwrap();
111/// assert_eq!(text, "Hello World");
112/// ```
113pub use markdown::process_markdown_to_plain_text;
114
115/// Processes Markdown and returns both the rendered HTML and a
116/// document-order list of [`Heading`]s suitable for building a
117/// table of contents.
118pub use markdown::process_markdown_with_toc;
119
120/// Streaming variant of [`process_markdown_with_toc`].
121pub use markdown::process_markdown_with_toc_to_writer;
122
123pub use markdown::MarkdownOptions;
124pub use markdown::SanitizerConfig;
125
126/// Re-export comrak's options for convenience.
127///
128/// # Usage
129/// ```
130/// use mdx_gen::Options;
131///
132/// let mut options = Options::default();
133/// options.extension.strikethrough = true;
134/// ```
135pub use comrak::Options;