Markdown parser fork with extended syntax for personal use.
at main 167 lines 4.7 kB view raw
1use markdown::{ 2 mdast::{Node, Root, Toml, Yaml}, 3 message, to_html, to_html_with_options, to_mdast, 4 unist::Position, 5 Constructs, Options, ParseOptions, 6}; 7use pretty_assertions::assert_eq; 8 9#[test] 10fn frontmatter() -> Result<(), message::Message> { 11 let frontmatter = Options { 12 parse: ParseOptions { 13 constructs: Constructs { 14 frontmatter: true, 15 ..Default::default() 16 }, 17 ..Default::default() 18 }, 19 ..Default::default() 20 }; 21 22 assert_eq!( 23 to_html("---\ntitle: Jupyter\n---"), 24 "<hr />\n<h2>title: Jupyter</h2>", 25 "should not support frontmatter by default" 26 ); 27 28 assert_eq!( 29 to_html_with_options("---\ntitle: Jupyter\n---", &frontmatter)?, 30 "", 31 "should support frontmatter (yaml)" 32 ); 33 34 assert_eq!( 35 to_html_with_options("+++\ntitle = \"Jupyter\"\n+++", &frontmatter)?, 36 "", 37 "should support frontmatter (toml)" 38 ); 39 40 assert_eq!( 41 to_html_with_options("---\n---", &frontmatter)?, 42 "", 43 "should support empty frontmatter" 44 ); 45 46 assert_eq!( 47 to_html_with_options("--\n---", &frontmatter)?, 48 "<h2>--</h2>", 49 "should not support 2 markers in an opening fence" 50 ); 51 52 assert_eq!( 53 to_html_with_options("----\n---", &frontmatter)?, 54 "<hr />\n<hr />", 55 "should not support 4 markers in an opening fence" 56 ); 57 58 assert_eq!( 59 to_html_with_options("---\n--", &frontmatter)?, 60 "<hr />\n<p>--</p>", 61 "should not support 2 markers in a closing fence" 62 ); 63 64 assert_eq!( 65 to_html_with_options("---\n--\n", &frontmatter)?, 66 "<hr />\n<p>--</p>\n", 67 "should not panic if newline after 2 marker closing fence" 68 ); 69 70 assert_eq!( 71 to_html_with_options("---\n----", &frontmatter)?, 72 "<hr />\n<hr />", 73 "should not support 4 markers in a closing fence" 74 ); 75 76 assert_eq!( 77 to_html_with_options("---\n---\n## Neptune", &frontmatter)?, 78 "<h2>Neptune</h2>", 79 "should support content after frontmatter" 80 ); 81 82 assert_eq!( 83 to_html_with_options("--- \t\n---", &frontmatter)?, 84 "", 85 "should support spaces and tabs after opening fence" 86 ); 87 88 assert_eq!( 89 to_html_with_options("---\n---\t ", &frontmatter)?, 90 "", 91 "should support spaces and tabs after closing fence" 92 ); 93 94 assert_eq!( 95 to_html_with_options("---\n---\na\nb", &frontmatter)?, 96 "<p>a\nb</p>", 97 "should support line endings after frontmatter" 98 ); 99 100 assert_eq!( 101 to_html_with_options("--- a\n---", &frontmatter)?, 102 "<h2>--- a</h2>", 103 "should not support content after opening fence" 104 ); 105 106 assert_eq!( 107 to_html_with_options("---\n--- b", &frontmatter)?, 108 "<hr />\n<p>--- b</p>", 109 "should not support content after closing fence" 110 ); 111 112 assert_eq!( 113 to_html_with_options("## Neptune\n---\n---", &frontmatter)?, 114 "<h2>Neptune</h2>\n<hr />\n<hr />", 115 "should not support frontmatter after content" 116 ); 117 118 assert_eq!( 119 to_html_with_options("> ---\n> ---\n> ## Neptune", &frontmatter)?, 120 "<blockquote>\n<hr />\n<hr />\n<h2>Neptune</h2>\n</blockquote>", 121 "should not support frontmatter in a container" 122 ); 123 124 assert_eq!( 125 to_html_with_options("---", &frontmatter)?, 126 "<hr />", 127 "should not support just an opening fence" 128 ); 129 130 assert_eq!( 131 to_html_with_options("---\ntitle: Neptune", &frontmatter)?, 132 "<hr />\n<p>title: Neptune</p>", 133 "should not support a missing closing fence" 134 ); 135 136 assert_eq!( 137 to_html_with_options("---\na\n\nb\n \t\nc\n---", &frontmatter)?, 138 "", 139 "should support blank lines in frontmatter" 140 ); 141 142 assert_eq!( 143 to_mdast("---\na: b\n---", &frontmatter.parse)?, 144 Node::Root(Root { 145 children: vec![Node::Yaml(Yaml { 146 value: "a: b".into(), 147 position: Some(Position::new(1, 1, 0, 3, 4, 12)) 148 })], 149 position: Some(Position::new(1, 1, 0, 3, 4, 12)) 150 }), 151 "should support yaml as `Yaml`s in mdast" 152 ); 153 154 assert_eq!( 155 to_mdast("+++\ntitle = \"Jupyter\"\n+++", &frontmatter.parse)?, 156 Node::Root(Root { 157 children: vec![Node::Toml(Toml { 158 value: "title = \"Jupyter\"".into(), 159 position: Some(Position::new(1, 1, 0, 3, 4, 25)) 160 })], 161 position: Some(Position::new(1, 1, 0, 3, 4, 25)) 162 }), 163 "should support toml as `Toml`s in mdast" 164 ); 165 166 Ok(()) 167}