Something went wrong. Try again.
Markdown parser fork with extended syntax for personal use.
Something went wrong. Try again.
6.2 kB · 244 lines
Rust
at hack
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245use markdown::{ mdast::{Heading, Node, Root, Text}, message, to_html, to_html_with_options, to_mdast, unist::Position, Constructs, Options, ParseOptions,};use pretty_assertions::assert_eq;
#[test]fn heading_atx() -> Result<(), message::Message> { assert_eq!( to_html("# foo"), "<h1>foo</h1>", "should support a heading w/ rank 1" );
assert_eq!( to_html("## foo"), "<h2>foo</h2>", "should support a heading w/ rank 2" );
assert_eq!( to_html("### foo"), "<h3>foo</h3>", "should support a heading w/ rank 3" );
assert_eq!( to_html("#### foo"), "<h4>foo</h4>", "should support a heading w/ rank 4" );
assert_eq!( to_html("##### foo"), "<h5>foo</h5>", "should support a heading w/ rank 5" );
assert_eq!( to_html("###### foo"), "<h6>foo</h6>", "should support a heading w/ rank 6" );
assert_eq!( to_html("####### foo"), "<p>####### foo</p>", "should not support a heading w/ rank 7" );
assert_eq!( to_html("#5 bolt"), "<p>#5 bolt</p>", "should not support a heading for a number sign not followed by whitespace (1)" );
assert_eq!( to_html("#hashtag"), "<p>#hashtag</p>", "should not support a heading for a number sign not followed by whitespace (2)" );
assert_eq!( to_html("\\## foo"), "<p>## foo</p>", "should not support a heading for an escaped number sign" );
assert_eq!( to_html("# foo *bar* \\*baz\\*"), "<h1>foo <em>bar</em> *baz*</h1>", "should support text content in headings" );
assert_eq!( to_html("# foo "), "<h1>foo</h1>", "should support arbitrary initial and final whitespace" );
assert_eq!( to_html(" ### foo"), "<h3>foo</h3>", "should support an initial space" );
assert_eq!( to_html(" ## foo"), "<h2>foo</h2>", "should support two initial spaces" );
assert_eq!( to_html(" # foo"), "<h1>foo</h1>", "should support three initial spaces" );
assert_eq!( to_html(" # foo"), "<pre><code># foo\n</code></pre>", "should not support four initial spaces" );
assert_eq!( to_html("foo\n # bar"), "<p>foo\n# bar</p>", "should not support four initial spaces when interrupting" );
assert_eq!( to_html("## foo ##"), "<h2>foo</h2>", "should support a closing sequence (1)" );
assert_eq!( to_html(" ### bar ###"), "<h3>bar</h3>", "should support a closing sequence (2)" );
assert_eq!( to_html("# foo ##################################"), "<h1>foo</h1>", "should support a closing sequence w/ an arbitrary number of number signs (1)" );
assert_eq!( to_html("##### foo ##"), "<h5>foo</h5>", "should support a closing sequence w/ an arbitrary number of number signs (2)" );
assert_eq!( to_html("### foo ### "), "<h3>foo</h3>", "should support trailing whitespace after a closing sequence" );
assert_eq!( to_html("### foo ### b"), "<h3>foo ### b</h3>", "should not support other content after a closing sequence" );
assert_eq!( to_html("# foo#"), "<h1>foo#</h1>", "should not support a closing sequence w/o whitespace before it" );
assert_eq!( to_html("### foo \\###"), "<h3>foo ###</h3>", "should not support an “escaped” closing sequence (1)" );
assert_eq!( to_html("## foo #\\##"), "<h2>foo ###</h2>", "should not support an “escaped” closing sequence (2)" );
assert_eq!( to_html("# foo \\#"), "<h1>foo #</h1>", "should not support an “escaped” closing sequence (3)" );
assert_eq!( to_html("****\n## foo\n****"), "<hr />\n<h2>foo</h2>\n<hr />", "should support atx headings when not surrounded by blank lines" );
assert_eq!( to_html("Foo bar\n# baz\nBar foo"), "<p>Foo bar</p>\n<h1>baz</h1>\n<p>Bar foo</p>", "should support atx headings interrupting paragraphs" );
assert_eq!( to_html("## \n#\n### ###"), "<h2></h2>\n<h1></h1>\n<h3></h3>", "should support empty atx headings (1)" );
assert_eq!( to_html("#\na\n# b"), "<h1></h1>\n<p>a</p>\n<h1>b</h1>", "should support empty atx headings (2)" );
assert_eq!( to_html("> #\na"), "<blockquote>\n<h1></h1>\n</blockquote>\n<p>a</p>", "should not support lazyness (1)" );
assert_eq!( to_html("> a\n#"), "<blockquote>\n<p>a</p>\n</blockquote>\n<h1></h1>", "should not support lazyness (2)" );
assert_eq!( to_html_with_options( "# a", &Options { parse: ParseOptions { constructs: Constructs { heading_atx: false, ..Default::default() }, ..Default::default() }, ..Default::default() } )?, "<p># a</p>", "should support turning off heading (atx)" );
assert_eq!( to_mdast("## alpha #", &Default::default())?, Node::Root(Root { children: vec![Node::Heading(Heading { depth: 2, children: vec![Node::Text(Text { value: "alpha".into(), position: Some(Position::new(1, 4, 3, 1, 9, 8)) }),], position: Some(Position::new(1, 1, 0, 1, 11, 10)) })], position: Some(Position::new(1, 1, 0, 1, 11, 10)) }), "should support heading (atx) as `Heading`s in mdast" );
Ok(())}