1
use pulldown_cmark_frontmatter::FrontmatterExtractor;
2

            
3
use crate::shared::ExampleAttributes;
4

            
5
mod shared;
6

            
7
1
fn main() {
8
1
    // begin rustme snippet: readme
9
1
    // This example renders the example Markdown to html using
10
1
    // `pulldown_cmark::html`, while also extracting the frontmatter from
11
1
    // Markdown.
12
1
    let mut extractor = FrontmatterExtractor::new(pulldown_cmark::Parser::new(include_str!(
13
1
        "../frontmatter-example.md"
14
1
    )));
15
1

            
16
1
    // The only difference from using the FrontmatterExtractor and the regular
17
1
    // pulldown_cmark::Parser is that you must pass a mutable reference to the
18
1
    // extractor to be able to read the Frontmatter it extracts.
19
1
    let mut rendered = String::new();
20
1
    pulldown_cmark::html::push_html(&mut rendered, &mut extractor);
21
1
    assert_eq!(rendered, include_str!("../frontmatter-example.html"));
22

            
23
1
    let frontmatter = extractor.frontmatter.expect("frontmatter not detected");
24
1
    assert_eq!(
25
1
        frontmatter.title.expect("title not detected"),
26
1
        "Frontmatter Example Document"
27
1
    );
28
1
    let code_block = frontmatter.code_block.expect("code block not detected");
29
1
    assert_eq!(code_block.language.as_deref(), Some("toml"));
30
1
    let attrs: ExampleAttributes = toml::from_str(&code_block.source).expect("invalid toml");
31
1
    assert_eq!(attrs.author, "https://fosstodon.org/@ecton");
32
    // end rustme snippet
33
1
}
34

            
35
#[test]
36
1
fn runs() {
37
1
    main()
38
1
}