Skip to content
lsdocs-doctree API

lsdocs_doctree #

Document tree (AST) types: nodes, spans and metadata.

A Document is what a parser produces and what transformers and renderers consume. Every type here crosses process boundaries, so the serialized shape is part of the public API:

  • Nodes are tagged by a type field, e.g. {"type": "heading", "level": 1, "children": [...]}.
  • Field names are snake_case.
  • Optional fields are omitted when empty, and unknown fields are ignored when reading, so new fields can be added without breaking older plugins.

Formats without a core node kind use Node::Custom.

The shape every parser produces

Parsers for any source format hand transformers the same shape:

  • Sections nest. Content under a title is wrapped in a Node::Section whose first child is the title Heading, with subsections nested inside it. A section's anchor id is Section::id; Heading::id is only for headings outside the section tree (e.g. inside a block quote). Content before the first title stays at the top level.
  • Ids are explicit or left out. Parsers set ids the author wrote (e.g. {#id} in Markdown, labels in reStructuredText). The host gives every other section a unique id derived from its title after parsing.
  • Cross-document links carry a LinkTarget. The host fills it in for relative links to other documents; parsers set it for references that are not paths, such as labels.

type alias Value #

pub type Value = Value;

An arbitrary structured value, as found in front matter or plugin data.

type alias Metadata #

pub type Metadata = BTreeMap<String, Value>;

Document-level metadata, such as front matter.

struct Span #

pub struct Span

A byte range within the document's source text.

Offsets count UTF-8 bytes from the start of the source, and end is exclusive. The source file is the enclosing Document::source.

Trait implementations

field start #

pub start: u32

Offset of the first byte.

field end #

pub end: u32

Offset one past the last byte.

function new #

pub const fn new(start: u32, end: u32) -> Self

Creates a span from start (inclusive) to end (exclusive).

struct Document #

pub struct Document

A parsed source document.

Trait implementations

field source #

pub source: String

Path of the source file, relative to the site's source directory, with / separators.

field metadata #

pub metadata: Metadata

Front matter and other document-level data.

field children #

pub children: Vec<Node>

Top-level nodes.

function new #

pub fn new(source: impl Into<String>) -> Self

Creates an empty document for the given source path.

method descendants #

pub fn descendants(&self) -> Descendants<'_>

Iterates over every node in the document, depth first, parents before children.

method title #

pub fn title(&self) -> Option<String>

The document's title: its title metadata if that is a string, else the text of its first heading.

function plain_text #

pub fn plain_text(nodes: &[Node]) -> String

The text of nodes without markup: text, inline code and image alt text, in order.

struct Descendants #

pub struct Descendants<'a> { /* private fields */ }

Depth-first iterator returned by Document::descendants and Node::descendants.

Trait implementations

enum Node #

pub enum Node

A node in the document tree.

Serialized with a type tag holding the Node::kind name, for core kinds, followed by the variant's fields.

Trait implementations

variant Section #

Section(Section)

A titled section; by convention its first child is a Heading.

variant Heading #

Heading(Heading)

A heading.

variant Paragraph #

Paragraph(Paragraph)

A paragraph.

variant List #

List(List)

A bulleted or numbered list whose children are list items.

variant ListItem #

ListItem(ListItem)

An item of a List.

variant BlockQuote #

BlockQuote(BlockQuote)

A block quote.

variant CodeBlock #

CodeBlock(CodeBlock)

A block of code or preformatted text.

variant Table #

Table(Table)

A table whose children are table rows.

variant TableRow #

TableRow(TableRow)

A row of a Table whose children are table cells.

variant TableCell #

TableCell(TableCell)

A cell of a TableRow.

variant ThematicBreak #

ThematicBreak(ThematicBreak)

A horizontal rule.

variant Raw #

Raw(Raw)

Content passed through verbatim to renderers of a matching format, such as inline HTML in Markdown.

variant Text #

Text(Text)

Plain text.

variant Emphasis #

Emphasis(Emphasis)

Emphasized (usually italic) text.

variant Strong #

Strong(Strong)

Strongly emphasized (usually bold) text.

variant InlineCode #

InlineCode(InlineCode)

Inline code.

variant Image #

Image(Image)

An image.

variant LineBreak #

LineBreak(LineBreak)

A hard line break.

variant Admonition #

Admonition(Admonition)

A callout such as a note or warning.

variant DefinitionList #

DefinitionList(DefinitionList)

A list of terms and their descriptions: children alternate between Node::DefinitionTerm and one or more Node::DefinitionDescriptions.

variant DefinitionTerm #

DefinitionTerm(DefinitionTerm)

A term in a DefinitionList.

variant DefinitionDescription #

DefinitionDescription(DefinitionDescription)

The description of the term before it in a DefinitionList.

variant FootnoteReference #

FootnoteReference(FootnoteReference)

A reference to a footnote, e.g. [^1].

variant FootnoteDefinition #

FootnoteDefinition(FootnoteDefinition)

The text of a footnote.

variant Math #

Math(Math)

A mathematical formula, inline or on its own line.

variant Figure #

Figure(Figure)

Content (usually an image) with an optional Node::Caption as its last child.

variant Caption #

Caption(Caption)

The caption of a Figure.

variant Anchor #

Anchor(Anchor)

A link target with no content of its own, e.g. a reStructuredText label in front of a paragraph.

variant PendingReference #

PendingReference(PendingReference)

A cross-reference no plugin has resolved yet, e.g. :py:func: to an object documented elsewhere. Resolvers replace it with a link, or leave it for a later one; the host reports and unwraps what is left.

variant ObjectTarget #

ObjectTarget(ObjectTarget)

An anchor naming a documented object (domain, kind, name), so that inventories of the site can list it. Otherwise like Node::Anchor.

variant Custom #

Custom(Custom)

A node kind defined by a plugin rather than by lsdocs.

method kind #

pub fn kind(&self) -> Cow<'static, str>

The name plugins use to select this node, e.g. in transformer capabilities: the type tag for core kinds ("heading"), or "custom:<kind>" for custom nodes.

method span #

pub fn span(&self) -> Option<Span>

Where this node came from in the source, if known.

method span_mut #

pub fn span_mut(&mut self) -> &mut Option<Span>

Mutable access to where this node came from, e.g. to move spans found in a piece of text to where that piece is in the source.

method children #

pub fn children(&self) -> &[Node]

This node's children; empty for leaf kinds.

method children_mut #

pub fn children_mut(&mut self) -> Option<&mut Vec<Node>>

Mutable access to this node's children, or None for leaf kinds.

method anchor_id #

pub fn anchor_id(&self) -> Option<&str>

The anchor id this node defines without content of its own: an Node::Anchor's or an Node::ObjectTarget's.

method descendants #

pub fn descendants(&self) -> Descendants<'_>

Iterates over this node and everything below it, depth first.

struct Section #

pub struct Section

See Node::Section.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field id #

pub id: Option<String>

Anchor identifier for linking to the section. Set by the parser if the author gave one; otherwise the host derives one from the title.

field children #

pub children: Vec<Node>

Content of the section.

struct Heading #

pub struct Heading

See Node::Heading.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field level #

pub level: u8

Heading level, from 1 (top) to 6. For a section title this is the level as written, which may skip levels; the nesting of sections is the structure.

field id #

pub id: Option<String>

Anchor identifier, only for headings that are not a section's title (a section's anchor is Section::id).

field children #

pub children: Vec<Node>

Inline content.

struct Paragraph #

pub struct Paragraph

See Node::Paragraph.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field children #

pub children: Vec<Node>

Inline content.

struct List #

pub struct List

See Node::List.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field ordered #

pub ordered: bool

Whether items are numbered.

field start #

pub start: Option<u64>

Number of the first item of an ordered list, if not 1.

field children #

pub children: Vec<Node>

The list's items.

struct ListItem #

pub struct ListItem

See Node::ListItem.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field checked #

pub checked: Option<bool>

For task list items, whether the box is checked.

field children #

pub children: Vec<Node>

Content of the item.

struct BlockQuote #

pub struct BlockQuote

See Node::BlockQuote.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field children #

pub children: Vec<Node>

Quoted content.

struct CodeBlock #

pub struct CodeBlock

See Node::CodeBlock.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field language #

pub language: Option<String>

Language name, e.g. "rust", if given.

field info #

pub info: Option<String>

Anything after the language on a Markdown fence's info line.

field value #

pub value: String

The code itself.

struct Table #

pub struct Table

See Node::Table.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field align #

pub align: Vec<Align>

Alignment of each column.

field children #

pub children: Vec<Node>

The table's rows.

struct TableRow #

pub struct TableRow

See Node::TableRow.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field header #

pub header: bool

Whether this is a header row.

field children #

pub children: Vec<Node>

The row's cells.

struct TableCell #

pub struct TableCell

See Node::TableCell.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field children #

pub children: Vec<Node>

Content of the cell.

struct ThematicBreak #

pub struct ThematicBreak

See Node::ThematicBreak.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

struct Raw #

pub struct Raw

See Node::Raw.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field format #

pub format: String

Output format the content is meant for, e.g. "html".

field value #

pub value: String

The verbatim content.

struct Text #

pub struct Text

See Node::Text.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field value #

pub value: String

The text.

struct Emphasis #

pub struct Emphasis

See Node::Emphasis.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field children #

pub children: Vec<Node>

Emphasized content.

struct Strong #

pub struct Strong

See Node::Strong.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field children #

pub children: Vec<Node>

Strongly emphasized content.

struct InlineCode #

pub struct InlineCode

See Node::InlineCode.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field value #

pub value: String

The code.

struct LinkTarget #

pub struct LinkTarget

The site-internal destination of a Link.

A resolved target names a document, an anchor, or both (an anchor alone means the linking document). An unresolved one has only a label, to be looked up among the anchors of every document, e.g. by a transformer.

Trait implementations

field document #

pub document: Option<String>

Source path of the target document, relative to the source directory with / separators.

field anchor #

pub anchor: Option<String>

Anchor id within the target document.

field label #

pub label: Option<String>

A label naming the target, not yet resolved to a document and anchor.

struct Image #

pub struct Image

See Node::Image.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field url #

pub url: String

Image location.

field alt #

pub alt: String

Alternative text.

field title #

pub title: Option<String>

Image title (often shown as a tooltip).

struct LineBreak #

pub struct LineBreak

See Node::LineBreak.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

struct Admonition #

pub struct Admonition

See Node::Admonition.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field kind #

pub kind: String

What kind of callout, e.g. "note", "tip", "warning".

field title #

pub title: Option<String>

A title replacing the kind's default one.

field children #

pub children: Vec<Node>

Content.

struct DefinitionList #

pub struct DefinitionList

See Node::DefinitionList.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field children #

pub children: Vec<Node>

Terms and descriptions, in order.

struct DefinitionTerm #

pub struct DefinitionTerm

See Node::DefinitionTerm.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field children #

pub children: Vec<Node>

Inline content.

struct DefinitionDescription #

pub struct DefinitionDescription

See Node::DefinitionDescription.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field children #

pub children: Vec<Node>

Content.

struct FootnoteReference #

pub struct FootnoteReference

See Node::FootnoteReference.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field label #

pub label: String

The footnote's label, e.g. "1" or "note".

struct FootnoteDefinition #

pub struct FootnoteDefinition

See Node::FootnoteDefinition.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field label #

pub label: String

The label references use.

field children #

pub children: Vec<Node>

Content.

struct Math #

pub struct Math

See Node::Math.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field value #

pub value: String

The formula, e.g. in TeX notation.

field display #

pub display: bool

Whether it is set on its own line rather than inline.

struct Figure #

pub struct Figure

See Node::Figure.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field id #

pub id: Option<String>

Anchor identifier, e.g. for linking to the figure.

field children #

pub children: Vec<Node>

Content, then an optional caption.

struct Caption #

pub struct Caption

See Node::Caption.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field children #

pub children: Vec<Node>

Content.

struct Anchor #

pub struct Anchor

See Node::Anchor.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field id #

pub id: String

The anchor identifier.

struct PendingReference #

pub struct PendingReference

See Node::PendingReference.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field domain #

pub domain: String

The domain: py, cpp, js, ..., or std for labels, terms and documents.

field role #

pub role: String

The role without its domain: func, class, ref, doc, ...

field target #

pub target: String

The target as written, title split off, ~ and a leading . removed.

field scope #

pub scope: Vec<String>

The qualified scope it was written in (module, class), innermost last, for scoped lookups.

field explicit #

pub explicit: bool

Whether the author gave a title. If not, a resolver may replace children with a better one.

field attrs #

pub attrs: BTreeMap<String, Value>

Domain-specific extras.

field children #

pub children: Vec<Node>

What to show, resolved or not.

struct ObjectTarget #

pub struct ObjectTarget

See Node::ObjectTarget.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field id #

pub id: String

The anchor identifier, as Anchor::id.

field domain #

pub domain: String

The domain: py, cpp, std, ...

field kind #

pub kind: String

The object type as inventories name it: function, class, label, term, ...

field name #

pub name: String

The fully qualified name, e.g. os.path.join.

field display #

pub display: Option<String>

A display name, if it differs from name (a section's title, say).

struct Custom #

pub struct Custom

See Node::Custom.

Trait implementations

field span #

pub span: Option<Span>

Where this node came from in the source, if known.

field kind #

pub kind: String

The plugin-defined kind, e.g. "mermaid". Selected as "custom:<kind>".

field attrs #

pub attrs: BTreeMap<String, Value>

Arbitrary attributes.

field children #

pub children: Vec<Node>

Child nodes, if any.

enum Align #

pub enum Align

Horizontal alignment of a table column.

Trait implementations

variant None #

None

No alignment specified.

variant Left #

Left

Left-aligned.

variant Center #

Center

Centered.

variant Right #

Right

Right-aligned.

function text #

pub fn text(value: impl Into<String>) -> Node

Shorthand for a Text node.