Skip to main content

Crate base_markdown

Crate base_markdown 

Source
Expand description

A pull parser, source-backed document AST, and shared HTML writer.

Use to_html for direct rendering, EventParser for custom pull consumers, and Document::parse when inspecting or editing a syntax tree. Documents traverse the same semantic event contract as the parser. to_markdown serializes documents; render_events accepts parser or document events.

§Pull events and build a source-backed AST

EventParser yields resolved semantic events on demand. Direct HTML rendering and the source-backed Document AST use this same contract. A document can own the input buffer, while ordinary text values retain byte ranges into it:

use base_markdown::{render_events, CompileOptions, Document, ParseOptions};

let source = String::from("# Introduction\n\nHello, *world*!");
let document = Document::parse(source, &ParseOptions::default()).unwrap();
let mut html = String::new();
render_events(document.source(), document.events(), &CompileOptions::default(), &mut html).unwrap();
assert_eq!(html, "<h1>Introduction</h1>\n<p>Hello, <em>world</em>!</p>");

AstBuilder::consume accepts an event budget so callers can build the AST over several steps without collecting the complete stream. Parsing accepts a complete UTF-8 buffer and prepares block structure before yielding events; inline resolution is deferred until consumption.

§Edit and serialize a document

Nodes use stable NodeId indices. Editing text with Document::text_mut copies only that value; detaching or moving a subtree retains its IDs and source metadata, and detached arena slots remain available until the document is dropped:

use base_markdown::{to_markdown, Document, HeadingOptions, ParseOptions};

let mut document = Document::parse(String::from("# Introduction\n\nHello"), &ParseOptions::default()).unwrap();
let heading = document.headings(&HeadingOptions::default())[0].node;
let text = document.children(heading).next().unwrap();
document.text_mut(text).unwrap().push_str(" updated");
assert!(to_markdown(&document).unwrap().starts_with("# Introduction updated"));

Document::append_child, Document::detach, and Document::attach edit structure without rebuilding the tree, and Document::replace_heading_section copies replacement content from another document. Document::render_node and to_markdown_node render individual subtrees. NodeSyntax carries optional authored formatting, such as reference style, code info strings, and frontmatter fences, so Markdown serialization can preserve it.

§Parsing options

The parser supports CommonMark and the GFM, frontmatter, and math constructs inherited from markdown-rs; see Constructs and Options::gfm. HTML output escapes raw HTML by default. MDX, JSX, and JavaScript expression parsing are not supported. Three additional ParseOptions are disabled by default, so ordinary parsing retains CommonMark behavior: ParseOptions::inline_code_info recognizes a language prefix before inline code, ParseOptions::html_block_blank_lines continues indented HTML blocks across blank lines, and ParseOptions::html_tag_namespaces accepts namespace-prefixed HTML tags. Constructs::frontmatter accepts language-agnostic frontmatter, and CodeFence describes authored code fences.

Re-exports§

pub use markdown::to_markdown_node;
pub use markdown::to_markdown;
pub use markdown::to_markdown_with_options;
pub use markdown::IndentOptions;
pub use markdown::LineWrapping;
pub use markdown::Options as MarkdownOptions;

Modules§

markdown
API.
message
Diagnostics use the same byte offsets as documents and events.
stream
The shared pull-based semantic event contract.

Structs§

AstBuilder
An incremental consumer that pulls events directly into an arena.
Children
A stack-free iterator over a node’s direct children.
CodeFence
Opening fence metadata, matching Markly’s Node::Fence structure.
CompileOptions
Configuration that describes how to compile to HTML.
Constructs
Control which constructs are enabled.
Descendants
A stack-free preorder traversal, including its starting node.
Document
An owned source buffer and a direct semantic AST.
DocumentEvents
A zero-allocation pull traversal over the arena’s parent/child/sibling links.
DocumentNode
A semantic AST node. Child and sibling lists use arena indices, not heap objects.
EventHtmlRenderer
Render semantic events directly into a caller-owned writer.
EventParser
A pull producer of resolved semantic events. Preparation records block structure; inline syntax is scanned only when its containing block is consumed.
HeadingEntry
A heading’s stable node ID, plain text and unique HTML anchor.
HeadingOptions
Heading levels to include when building a table of contents.
NodeId
A stable index into a document’s contiguous node arena.
Options
Configuration that describes how to parse from markdown and compile to HTML.
ParseOptions
Configuration that describes how to parse from markdown.

Enums§

AlignKind
Alignment of phrasing content.
BuildProgress
The result of one bounded AST-building operation.
LineEnding
Type of line endings in markdown.
NodeSyntax
Source formatting that is useful for Markdown serialization but unnecessary for HTML.
ReferenceKind
Explicitness of a reference.

Functions§

render_events
Render events with the shared HTML writer.
to_html
Turn markdown into HTML.
to_html_with_options
Turn markdown into HTML, with configuration.