#node #markdown #walker #traits #cell #ast #comrak

markdown-walker

A markdown walker trait for the comrak crate

1 unstable release

new 0.0.1 Jan 2, 2025

#17 in #walker

MIT license

18KB
368 lines

Markdown Walker

A markdown walker trait for traversing markdown AST made by the Comrak crate.

Example

use std::{cell::RefCell, io};
use comrak::{arena_tree::Node, nodes::{Ast, NodeLink}};
use markdown_walker::MarkdownWalker;

#[derive(Debug, Default, PartialEq)]
struct ImageCount(usize);

impl MarkdownWalker for ImageCount {
    fn visit_image<'arena>(
        &mut self,
        _node: &'arena Node<'arena, RefCell<Ast>>,
        _link: &NodeLink,
    ) -> io::Result<()> {
        self.0 += 1;
        Ok(())
    }
}

#[test]
fn test_image_count() {
    let markdown = r#"
![Image 1](image1.png)
![Image 2](image2.png)
![Image 3](image3.png)
"#;

    let image_count = ImageCount::from_markdown(markdown).unwrap();
    assert_eq!(image_count, ImageCount(3));
}

Dependencies

~8–16MB
~244K SLoC