silk.markdown.document.

A source-spanned Markdown tree with indexed nodes and borrowed source text.

When to use#

Parse strict CommonMark with MarkdownDocument.parse, or select extensions with MarkdownOptions and MarkdownDocument.parseWithOptions. Walk the tree or render it with silk.markdown.html.

Details#

The document borrows its UTF-8 source. Verbatim text uses source ranges; decoded text uses one owned buffer. Node ids belong to one document. Every span counts bytes in the original source, including syntax delimiters.

Examples#

Inspect a heading and its source span#

silk
import silk.markdown.document { MarkdownDocument, MarkdownNodeId }
import silk.core.effect { Effect }
import silk.mem.allocator { Allocator, OutOfMemoryError }

pub effect fn main() -> i32
  ! OutOfMemoryError {
  let mut allocator = Allocator.systemAllocatorProvider()
  let document = run MarkdownDocument.parse("# Hello")
    |> Effect.provideMut<Allocator>(&mut allocator)
  let heading = MarkdownNodeId { index: 1 }
  if MarkdownDocument.headingLevel(&document, heading) != 1
    || MarkdownDocument.span(&document, heading).end != 7 {
    return 1
  }
  return 0
}

Import as MarkdownDocument with import silk.markdown.document { MarkdownDocument }.

Public declarations: 10.

MarkdownSpan#

silk
pub struct MarkdownSpan

A half-open byte range in the original Markdown source.

Field start#

silk
pub start: usize

The offset of the first byte, including any opening syntax delimiter.

Field end#

silk
pub end: usize

The offset after the last byte, including any closing syntax delimiter.

Implementation Copy for MarkdownSpan#

silk
impl Copy for MarkdownSpan

MarkdownNodeId#

silk
pub struct MarkdownNodeId

The index of one node in one Markdown document. An id from another document is not transferable.

Field index#

silk
pub index: usize

The index in the document's preorder node slice.

Implementation Copy for MarkdownNodeId#

silk
impl Copy for MarkdownNodeId

MarkdownNodeKind#

silk
pub enum MarkdownNodeKind

The syntactic role of a Markdown node.

Document#

silk
Document

The root container of all blocks.

Paragraph#

silk
Paragraph

A paragraph with inline children.

Heading#

silk
Heading

An ATX or setext heading with inline children.

ThematicBreak#

silk
ThematicBreak

A horizontal thematic break.

BlockQuote#

silk
BlockQuote

A block quote container.

List#

silk
List

An ordered or bullet list.

ListItem#

silk
ListItem

One item in a list.

CodeBlock#

silk
CodeBlock

A fenced or indented code block.

HtmlBlock#

silk
HtmlBlock

A raw HTML block.

Text#

silk
Text

Literal inline text with escapes and entities decoded.

SoftBreak#

silk
SoftBreak

A line ending within a paragraph.

HardBreak#

silk
HardBreak

An explicit inline line break.

Code#

silk
Code

An inline code span.

Emphasis#

silk
Emphasis

Emphasized inline content.

Strong#

silk
Strong

Strongly emphasized inline content.

silk
Link

A link with inline label children.

Image#

silk
Image

An image with inline alternative-text children.

HtmlInline#

silk
HtmlInline

A raw inline HTML fragment.

Table#

silk
Table

A GFM table with row children.

TableRow#

silk
TableRow

A table header or body row with cell children.

TableCell#

silk
TableCell

A table cell with inline children.

Strikethrough#

silk
Strikethrough

Deleted inline content enclosed by one or two tildes.

FootnoteReference#

silk
FootnoteReference

A reference to a defined footnote.

FootnoteDefinition#

silk
FootnoteDefinition

A footnote container with block children.

Frontmatter#

silk
Frontmatter

A leading YAML or TOML metadata block with an unparsed source body.

Alert#

silk
Alert

A GitHub alert container with block children.

MarkdownAlignment#

silk
pub enum MarkdownAlignment

A table column's horizontal alignment.

None#

silk
None

No explicit alignment.

Left#

silk
Left

Left alignment.

Center#

silk
Center

Center alignment.

silk
Right

Right alignment.

MarkdownTaskState#

silk
pub enum MarkdownTaskState

A list item's task marker state.

None#

silk
None

The item has no task marker, or the node is not a list item.

Unchecked#

silk
Unchecked

The item starts with an unchecked task marker.

Checked#

silk
Checked

The item starts with a checked task marker.

MarkdownFrontmatterKind#

silk
pub enum MarkdownFrontmatterKind

The delimiter of an unparsed frontmatter block.

None#

silk
None

The node is not frontmatter.

Yaml#

silk
Yaml

Three hyphens delimit YAML metadata.

Toml#

silk
Toml

Three plus signs delimit TOML metadata.

MarkdownAlertKind#

silk
pub enum MarkdownAlertKind

The category of a GitHub alert.

None#

silk
None

The node is not an alert.

Note#

silk
Note

Useful information.

Tip#

silk
Tip

Advice for a task.

Important#

silk
Important

Information required for a task.

Warning#

silk
Warning

A potential problem.

Caution#

silk
Caution

A risk that requires attention.

MarkdownOptions#

silk
pub struct MarkdownOptions

Syntax switches and a nesting bound. Every extension is disabled by default. Excess nesting remains literal text or paragraph content.

Field maxNesting#

silk
pub maxNesting: usize

The maximum block-container and inline nesting depth. The default is 256; zero disables nesting.

Field tables#

silk
pub tables: bool

Enables GFM tables. The default is false.

Field strikethrough#

silk
pub strikethrough: bool

Enables matching one- or two-tilde strikethrough. The default is false.

Field taskLists#

silk
pub taskLists: bool

Enables task markers in list items. The default is false.

Field autolinkLiterals#

silk
pub autolinkLiterals: bool

Enables GFM URL and email literals, including ftp, mailto, and xmpp. The default is false.

Field footnotes#

silk
pub footnotes: bool

Enables footnote definitions and references. The default is false.

Field frontmatter#

silk
pub frontmatter: bool

Enables leading YAML or TOML frontmatter capture. The default is false.

Field alerts#

silk
pub alerts: bool

Enables GitHub alert block quotes. The default is false.

Implementation Copy for MarkdownOptions#

silk
impl Copy for MarkdownOptions

Implementation MarkdownOptions#

silk
impl MarkdownOptions

Associated function make#

silk
pub fn make() -> MarkdownOptions

Returns strict CommonMark options with a nesting bound of 256 and every extension disabled.

Associated function gfm#

silk
pub fn gfm() -> MarkdownOptions

Returns GFM table, strikethrough, task-list, and autolink options with a nesting bound of 256. Footnotes, frontmatter, and alerts remain disabled.

MarkdownNode#

silk
pub struct MarkdownNode

One node in a Markdown document. Read payloads and tree links with MarkdownDocument operations. The node slice is in preorder, with each parent before its children.

Field kind#

silk
pub kind: MarkdownNodeKind

The syntactic role of this node.

Implementation Copy for MarkdownNode#

silk
impl Copy for MarkdownNode

MarkdownDocument#

silk
pub struct MarkdownDocument<'source>

A parsed Markdown tree that borrows the source and owns decoded text and a flat node arena.

Details#

Verbatim literals are views into the original source. Decoded literals borrow one owned side buffer. Returned text cannot outlive the document. Invalid ids return absent links, empty text, and zero payloads. Footnote definitions follow main blocks in first-reference order. Unused definitions follow referenced definitions.

Implementation MarkdownDocument<'source>#

silk
impl<'source> MarkdownDocument<'source>

Associated function parse#

silk
pub effect fn parse(source: string<'source>) -> MarkdownDocument<'source>
    ! OutOfMemoryError
    ? &mut Allocator

Parses strict CommonMark 0.31.2 with a nesting bound of 256 and every extension disabled. Every input produces a document. The document borrows source and owns its nodes and decoded text.

Details#

Bounded inline lookahead and reference expansion leave excess candidates as literal text.

Errors#

  • OutOfMemoryError if the allocator cannot supply the tree or decoded text.

Associated function parseWithOptions#

silk
pub effect fn parseWithOptions(
    source: string<'source>,
    options: &MarkdownOptions,
  ) -> MarkdownDocument<'source>
    ! OutOfMemoryError
    ? &mut Allocator

Parses UTF-8 Markdown with the extension switches and nesting bound in options. Disabled extensions retain CommonMark syntax. Excess nesting remains literal text or paragraph content. The document borrows source and owns its nodes and decoded text.

Details#

Bounded inline lookahead and reference expansion leave excess candidates as literal text.

Errors#

  • OutOfMemoryError if the allocator cannot supply the tree or decoded text.

Method root#

silk
pub fn root(self: &Self) -> MarkdownNodeId

Returns the document root, whose index is zero, including for an empty source.

Method nodes#

silk
pub fn nodes(self: &Self) -> &[MarkdownNode]

Borrows all nodes in preorder. A node's id is its index in this slice.

Method kind#

silk
pub fn kind(self: &Self, id: MarkdownNodeId) -> MarkdownNodeKind

Returns the node kind, or Text for an invalid id.

Method span#

silk
pub fn span(self: &Self, id: MarkdownNodeId) -> MarkdownSpan

Returns the source byte range, including delimiters, or 0..0 for an invalid id. Container spans include prefixes and line endings; inline spans include only their own syntax.

Method parent#

silk
pub fn parent(self: &Self, id: MarkdownNodeId) -> Option<MarkdownNodeId>

Returns the parent, or None if the link is absent or the id is invalid.

Method firstChild#

silk
pub fn firstChild(self: &Self, id: MarkdownNodeId) -> Option<MarkdownNodeId>

Returns the first child, or None if the link is absent or the id is invalid.

Method lastChild#

silk
pub fn lastChild(self: &Self, id: MarkdownNodeId) -> Option<MarkdownNodeId>

Returns the last child, or None if the link is absent or the id is invalid.

Method nextSibling#

silk
pub fn nextSibling(self: &Self, id: MarkdownNodeId) -> Option<MarkdownNodeId>

Returns the next sibling, or None if the link is absent or the id is invalid.

Method previousSibling#

silk
pub fn previousSibling(self: &Self, id: MarkdownNodeId) -> Option<MarkdownNodeId>

Returns the previous sibling, or None if the link is absent or the id is invalid.

Method headingLevel#

silk
pub fn headingLevel(self: &Self, id: MarkdownNodeId) -> u8

Returns the heading level from one through six, or zero for other kinds.

Method ordered#

silk
pub fn ordered(self: &Self, id: MarkdownNodeId) -> bool

Returns whether a list is ordered; returns false for other kinds.

Method start#

silk
pub fn start(self: &Self, id: MarkdownNodeId) -> usize

Returns the initial ordered-list number, or zero for other kinds.

Method tight#

silk
pub fn tight(self: &Self, id: MarkdownNodeId) -> bool

Returns whether a list suppresses paragraph tags; returns false for other kinds.

Method marker#

silk
pub fn marker(self: &Self, id: MarkdownNodeId) -> u8

Returns the bullet or ordered-list delimiter byte, or zero for other kinds.

Method isFenced#

silk
pub fn isFenced(self: &Self, id: MarkdownNodeId) -> bool

Returns whether a code block uses a fence; returns false for other kinds.

Method literal#

silk
pub fn literal<'a>(self: &'a Self, id: MarkdownNodeId) -> string<'a>

Returns the literal of Text, Code, CodeBlock, HtmlBlock, or HtmlInline; returns empty text for other kinds. The result borrows the document. Text has decoded backslash escapes and character references. Code spans replace line endings with spaces and remove one enclosing space when required. Block literals normalize line endings to line feeds. All literals replace NUL with U+FFFD.

Method info#

silk
pub fn info<'a>(self: &'a Self, id: MarkdownNodeId) -> string<'a>

Returns the decoded fence info string, or empty text for other kinds. The result borrows the document.

Method destination#

silk
pub fn destination<'a>(self: &'a Self, id: MarkdownNodeId) -> string<'a>

Returns the decoded link or image destination, or empty text for other kinds. The result borrows the document.

Method isTableHeader#

silk
pub fn isTableHeader(self: &Self, id: MarkdownNodeId) -> bool

Returns whether a table row is the header row; returns false for other kinds.

Method tableColumns#

silk
pub fn tableColumns(self: &Self, id: MarkdownNodeId) -> usize

Returns the number of columns in a table; returns zero for other kinds or invalid ids.

Method columnAlignment#

silk
pub fn columnAlignment(self: &Self, id: MarkdownNodeId, column: usize) -> MarkdownAlignment

Returns a table column's alignment in constant time. Columns have zero-based indices. Returns None for other kinds, invalid ids, or out-of-range columns.

Method alignment#

silk
pub fn alignment(self: &Self, id: MarkdownNodeId) -> MarkdownAlignment

Returns a cell's column alignment; returns None for other kinds or invalid ids.

Method taskState#

silk
pub fn taskState(self: &Self, id: MarkdownNodeId) -> MarkdownTaskState

Returns a list item's task state; returns None for other kinds or items without a marker.

Method frontmatterKind#

silk
pub fn frontmatterKind(self: &Self, id: MarkdownNodeId) -> MarkdownFrontmatterKind

Returns the frontmatter delimiter kind; returns None for other kinds or invalid ids.

Method frontmatterBody#

silk
pub fn frontmatterBody<'a>(self: &'a Self, id: MarkdownNodeId) -> string<'a>

Borrows the unparsed frontmatter body directly from the source, including its final line ending. Excludes both delimiter lines. Returns empty text for other kinds or invalid ids.

Method alertKind#

silk
pub fn alertKind(self: &Self, id: MarkdownNodeId) -> MarkdownAlertKind

Returns the alert category; returns None for other kinds or invalid ids.

Method footnoteLabel#

silk
pub fn footnoteLabel<'a>(self: &'a Self, id: MarkdownNodeId) -> string<'a>

Borrows the source label of a footnote reference or definition, without brackets or the caret. Returns empty text for other kinds. Resolution collapses label whitespace and folds Unicode case.

Method footnoteDefinition#

silk
pub fn footnoteDefinition(self: &Self, id: MarkdownNodeId) -> Option<MarkdownNodeId>

Returns the definition resolved by a footnote reference; returns None for other kinds or invalid ids.

Method footnoteNumber#

silk
pub fn footnoteNumber(self: &Self, id: MarkdownNodeId) -> usize

Returns a footnote's one-based first-reference number; returns zero for unused definitions and other kinds.

Method footnoteReferences#

silk
pub fn footnoteReferences(self: &Self, id: MarkdownNodeId) -> usize

Returns a reference's one-based occurrence, or a definition's reference count; returns zero for other kinds.

Method title#

silk
pub fn title<'a>(self: &'a Self, id: MarkdownNodeId) -> Option<string<'a>>

Returns the decoded link or image title, or None for an absent title or another kind. A present empty title differs from an absent title. The text borrows the document.

Category: Data formats.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.

View the source of this page