markdown-model 0.1.0

Owned, renderer-independent Markdown documents and navigation metadata
Documentation
use markdown_model::*;
const FIXTURE: &str = include_str!("../../../tests/fixtures/features.md");
#[test]
fn common_model_retains_semantics_and_source_metadata() {
    let doc = Document::parse(FIXTURE).unwrap();
    assert_eq!(
        doc.headings()
            .iter()
            .map(|heading| heading.id().as_str())
            .collect::<Vec<_>>(),
        ["mira-markdown-preview", "日本語", "日本語-1"]
    );
    let link = doc
        .links()
        .iter()
        .find(|link| link.destination().as_str() == "guide.markdown#日本語")
        .unwrap();
    assert_eq!(link.text(), "a local link");
    assert!(link.span().get(FIXTURE).unwrap().contains("guide.markdown"));
    assert!(doc.blocks().iter().any(|block| matches!(block.kind(), BlockKind::Code { kind, text } if kind.info() == Some("rust") && text.contains("println!"))));
    assert!(doc.blocks().iter().any(|block| matches!(block.kind(), BlockKind::Table(table) if table.alignments() == [Alignment::Left, Alignment::Center, Alignment::Right] && table.rows().len() == 2)));
    let list = doc
        .blocks()
        .iter()
        .find_map(|b| {
            if let BlockKind::List { kind, items } = b.kind() {
                Some((kind, items))
            } else {
                None
            }
        })
        .unwrap();
    assert_eq!(*list.0, ListKind::Ordered { start: 3 });
    let nested = list.1[1]
        .blocks()
        .iter()
        .find_map(|b| {
            if let BlockKind::List { items, .. } = b.kind() {
                Some(items)
            } else {
                None
            }
        })
        .unwrap();
    assert_eq!(
        nested.iter().map(ListItem::task).collect::<Vec<_>>(),
        [TaskState::Unchecked, TaskState::Checked]
    );
    assert!(
        doc.blocks()
            .iter()
            .any(|block| matches!(block.kind(), BlockKind::Literal(s) if s.contains("<script>")))
    );
    assert!(doc.blocks().iter().any(|block| matches!(block.kind(), BlockKind::Paragraph(content) if plain_text(content).contains("Incomplete **emphasis"))));
}
#[test]
fn unique_unicode_anchors_include_colliding_suffixes() {
    let doc = Document::parse("# X\n# X-1\n# X\n# !!!\n# 日本語\n# 日本語").unwrap();
    assert_eq!(
        doc.headings()
            .iter()
            .map(|heading| heading.id().as_str())
            .collect::<Vec<_>>(),
        ["x", "x-1", "x-2", "section", "日本語", "日本語-1"]
    );
}
#[test]
fn inline_structure_and_breaks_are_not_flattened() {
    let doc = Document::parse("a *b **c*** ~~d~~ `e`\nf  \ng ![**alt**](x.png)").unwrap();
    let BlockKind::Paragraph(c) = doc.blocks()[0].kind() else {
        panic!()
    };
    assert!(
        c.iter()
            .any(|inline| matches!(inline.kind(), InlineKind::Emphasis(_)))
    );
    assert!(
        c.iter()
            .any(|inline| matches!(inline.kind(), InlineKind::Strikethrough(_)))
    );
    assert!(
        c.iter()
            .any(|inline| matches!(inline.kind(), InlineKind::Code(_)))
    );
    assert!(
        c.iter()
            .any(|inline| matches!(inline.kind(), InlineKind::SoftBreak))
    );
    assert!(
        c.iter()
            .any(|inline| matches!(inline.kind(), InlineKind::HardBreak))
    );
    assert!(c.iter().any(
        |inline| matches!(inline.kind(), InlineKind::Image { alt, .. } if plain_text(alt) == "alt")
    ));
}
#[test]
fn resource_limits_fail_explicitly() {
    let capped = ParseLimits::new(8, usize::MAX, 16);
    assert_eq!(capped.max_nesting_depth(), ParseLimits::MAX_NESTING_DEPTH);
    assert!(matches!(
        Document::parse_with_limits("hello", ParseLimits::default().with_max_source_bytes(4)),
        Err(ParseError::TooLarge { .. })
    ));
    assert!(matches!(
        Document::parse_with_limits(
            "> > > > text",
            ParseLimits::default().with_max_nesting_depth(3)
        ),
        Err(ParseError::TooDeep { .. })
    ));
    assert!(matches!(
        Document::parse_with_limits("# hi", ParseLimits::default().with_max_events(1)),
        Err(ParseError::TooComplex { .. })
    ));
}