Skip to main content

Document

Struct Document 

Source
pub struct Document {
    pub content: String,
    pub path: PathBuf,
    pub lines: Vec<String>,
    pub book_src_dir: Option<PathBuf>,
}
Expand description

Represents a parsed markdown document with position information

Fields§

§content: String

The original markdown content

§path: PathBuf

Path to the source file

§lines: Vec<String>

Lines split for line-based rule processing

§book_src_dir: Option<PathBuf>

Optional book source directory (used by mdbook rules for path resolution)

Implementations§

Source§

impl Document

Source

pub fn new(content: String, path: PathBuf) -> Result<Document, MdBookLintError>

Parse a markdown document from content and path

Source

pub fn with_book_src_dir( content: String, path: PathBuf, book_src_dir: Option<PathBuf>, ) -> Result<Document, MdBookLintError>

Parse a markdown document with an optional book source directory

The book_src_dir is used by mdbook rules (e.g., MDBOOK002) for resolving relative paths when running in preprocessor mode. When processing files directly from the filesystem, this can be None and the rules will discover the book source directory by looking for SUMMARY.md.

Source

pub fn parse_ast<'a>( &self, arena: &'a Arena<Node<'a, RefCell<Ast>>>, ) -> &'a Node<'a, RefCell<Ast>>

Parse the content into a comrak AST

Source

pub fn parse_ast_with_context<'a>( &self, arena: &'a Arena<Node<'a, RefCell<Ast>>>, ) -> Result<&'a Node<'a, RefCell<Ast>>, MdBookLintError>

Parse AST with error context

Source

pub fn line_number_at_offset(&self, offset: usize) -> usize

Get the line number (1-based) for a given byte offset

Source

pub fn column_number_at_offset(&self, offset: usize) -> usize

Get the column number (1-based) for a given byte offset

Source

pub fn line_ending(&self, line: usize) -> Option<&str>

Return the source terminator for a 1-based logical line.

The returned value is "\n", "\r\n", or None when the line has no terminator or does not exist. Rules that insert source lines should use this instead of assuming LF.

Source

pub fn headings<'a>( &self, ast: &'a Node<'a, RefCell<Ast>>, ) -> Vec<&'a Node<'a, RefCell<Ast>>>

Get all heading nodes from the AST

Source

pub fn headings_with_context<'a>( &self, ast: &'a Node<'a, RefCell<Ast>>, ) -> Result<Vec<&'a Node<'a, RefCell<Ast>>>, MdBookLintError>

Get all heading nodes with error context

Source

pub fn code_blocks<'a>( &self, ast: &'a Node<'a, RefCell<Ast>>, ) -> Vec<&'a Node<'a, RefCell<Ast>>>

Get all code block nodes from the AST

Source

pub fn code_blocks_with_context<'a>( &self, ast: &'a Node<'a, RefCell<Ast>>, ) -> Result<Vec<&'a Node<'a, RefCell<Ast>>>, MdBookLintError>

Get all code block nodes with error context

Source

pub fn heading_level<'a>(node: &'a Node<'a, RefCell<Ast>>) -> Option<u32>

Get the heading level for a heading node

Source

pub fn node_text<'a>(&self, node: &'a Node<'a, RefCell<Ast>>) -> String

Get the text content of a node

Source

pub fn node_position<'a>( &self, node: &'a Node<'a, RefCell<Ast>>, ) -> Option<(usize, usize)>

Get the source position of a node

Source

pub fn frontmatter_line_range(&self) -> Option<(usize, usize)>

Detect a leading YAML frontmatter block.

Recognition matches comrak’s front-matter extension (configured in Self::parse_ast with a --- delimiter): the block must begin on the very first line of the document, its opening and closing delimiter lines must each be exactly --- (no indentation, no trailing whitespace, no extra dashes), and a closing delimiter must be present. Returns the 1-based, inclusive (start_line, end_line) of the block with the delimiters included, or None when the document has no frontmatter.

Line-based rules use this to avoid treating frontmatter delimiters and YAML keys as Markdown headings or lists (see MD041, MD007).

Source

pub fn frontmatter_ast_offset<'a>( &self, ast: &'a Node<'a, RefCell<Ast>>, ) -> usize

Number of leading source lines that comrak folds into a front-matter node.

When frontmatter is present, comrak renumbers every following node as if the document began at line 1, so the source positions reported for nodes after the frontmatter are shifted up by the number of lines the frontmatter occupied (delimiters plus a single trailing blank line). Add this offset back to translate an AST line number into an index into Self::lines. Returns 0 when the document has no frontmatter.

Trait Implementations§

Source§

impl Debug for Document

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.