diff options
author | Titus Wormer <tituswormer@gmail.com> | 2022-09-19 17:29:59 +0200 |
---|---|---|
committer | Titus Wormer <tituswormer@gmail.com> | 2022-09-19 17:29:59 +0200 |
commit | 9cb9e37c33173c16cbafd345f43e43b5a550537d (patch) | |
tree | 6b02d2ea2c3ba904887b01f88aee369bd1a026d8 /src/lib.rs | |
parent | dfe1f2da522afa3e86680fa763b2f3c75e86b3ca (diff) | |
download | markdown-rs-9cb9e37c33173c16cbafd345f43e43b5a550537d.tar.gz markdown-rs-9cb9e37c33173c16cbafd345f43e43b5a550537d.tar.bz2 markdown-rs-9cb9e37c33173c16cbafd345f43e43b5a550537d.zip |
Add structs, enums for `mdast`
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 45 |
1 files changed, 37 insertions, 8 deletions
@@ -1,26 +1,37 @@ //! Public API of micromark. //! -//! This module exposes [`micromark`][] (and [`micromark_with_options`][]). -//! `micromark` is a safe way to transform (untrusted?) markdown into HTML. -//! `micromark_with_options` allows you to configure how markdown is turned into -//! HTML, such as by allowing dangerous HTML when you trust it. +//! This module exposes primarily [`micromark`][]. +//! It also exposes [`micromark_with_options`][] and [`micromark_to_mdast`][]. +//! +//! * [`micromark`][] +//! — safe way to transform (untrusted?) markdown into HTML +//! * [`micromark_with_options`][] +//! — like `micromark` but lets you configure how markdown is turned into +//! HTML, such as allowing dangerous HTML or turning on/off +//! different constructs (GFM, MDX, and the like) +//! * [`micromark_to_mdast`][] +//! — like `micromark_with_options` but compiles to a syntax tree #![no_std] extern crate alloc; -mod compiler; mod construct; mod event; +pub mod mdast; mod parser; mod resolve; mod state; mod subtokenize; +mod to_html; +mod to_mdast; mod tokenizer; mod util; -use crate::compiler::compile; -use crate::parser::parse; use alloc::{boxed::Box, fmt, string::String}; +use mdast::Root; +use parser::parse; +use to_html::compile as to_html; +use to_mdast::compile as to_mdast; /// Type of line endings in markdown. #[derive(Clone, Debug, Default, Eq, PartialEq)] @@ -1154,5 +1165,23 @@ pub fn micromark(value: &str) -> String { /// ``` pub fn micromark_with_options(value: &str, options: &Options) -> Result<String, String> { let (events, bytes) = parse(value, options)?; - Ok(compile(&events, bytes, options)) + Ok(to_html(&events, bytes, options)) +} + +/// Turn markdown into a syntax tree. +/// +/// ## Errors +/// +/// `to_mdast` never errors with normal markdown because markdown does not have +/// syntax errors, so feel free to `unwrap()`. +/// However, MDX does have syntax errors. +/// When MDX is turned on, there are several errors that can occur with how +/// JSX, expressions, or ESM are written. +/// +/// ## Examples +/// +/// To do. +pub fn micromark_to_mdast(value: &str, options: &Options) -> Result<Root, String> { + let (events, bytes) = parse(value, options)?; + Ok(to_mdast(&events, bytes, options)) } |