From 9cb9e37c33173c16cbafd345f43e43b5a550537d Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Mon, 19 Sep 2022 17:29:59 +0200 Subject: Add structs, enums for `mdast` --- src/lib.rs | 45 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 0b1a571..669660b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { 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 { + let (events, bytes) = parse(value, options)?; + Ok(to_mdast(&events, bytes, options)) } -- cgit