Markdown parser fork with extended syntax for personal use.
at hack 156 lines 4.2 kB view raw
1use markdown::{ 2 mdast::{Break, Node, Paragraph, Root, Text}, 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 hard_break_trailing() -> Result<(), message::Message> { 11 assert_eq!( 12 to_html("foo \nbaz"), 13 "<p>foo<br />\nbaz</p>", 14 "should support two trailing spaces to form a hard break" 15 ); 16 17 assert_eq!( 18 to_html("foo \nbaz"), 19 "<p>foo<br />\nbaz</p>", 20 "should support multiple trailing spaces" 21 ); 22 23 assert_eq!( 24 to_html("foo \n bar"), 25 "<p>foo<br />\nbar</p>", 26 "should support leading spaces after a trailing hard break" 27 ); 28 29 assert_eq!( 30 to_html("*foo \nbar*"), 31 "<p><em>foo<br />\nbar</em></p>", 32 "should support trailing hard breaks in emphasis" 33 ); 34 35 assert_eq!( 36 to_html("`code \ntext`"), 37 "<p><code>code text</code></p>", 38 "should not support trailing hard breaks in code" 39 ); 40 41 assert_eq!( 42 to_html("foo "), 43 "<p>foo</p>", 44 "should not support trailing hard breaks at the end of a paragraph" 45 ); 46 47 assert_eq!( 48 to_html("### foo "), 49 "<h3>foo</h3>", 50 "should not support trailing hard breaks at the end of a heading" 51 ); 52 53 assert_eq!( 54 to_html("aaa \t\nbb"), 55 "<p>aaa\nbb</p>", 56 "should support a mixed line suffix (1)" 57 ); 58 59 assert_eq!( 60 to_html("aaa\t \nbb"), 61 "<p>aaa\nbb</p>", 62 "should support a mixed line suffix (2)" 63 ); 64 65 assert_eq!( 66 to_html("aaa \t \nbb"), 67 "<p>aaa\nbb</p>", 68 "should support a mixed line suffix (3)" 69 ); 70 71 assert_eq!( 72 to_html("aaa\0 \nbb"), 73 "<p>aaa�<br />\nbb</p>", 74 "should support a hard break after a replacement character" 75 ); 76 77 assert_eq!( 78 to_html("aaa\0\t\nbb"), 79 "<p>aaa�\nbb</p>", 80 "should support a line suffix after a replacement character" 81 ); 82 83 assert_eq!( 84 to_html("*a* \nbb"), 85 "<p><em>a</em><br />\nbb</p>", 86 "should support a hard break after a span" 87 ); 88 89 assert_eq!( 90 to_html("*a*\t\nbb"), 91 "<p><em>a</em>\nbb</p>", 92 "should support a line suffix after a span" 93 ); 94 95 assert_eq!( 96 to_html("*a* \t\nbb"), 97 "<p><em>a</em>\nbb</p>", 98 "should support a mixed line suffix after a span (1)" 99 ); 100 101 assert_eq!( 102 to_html("*a*\t \nbb"), 103 "<p><em>a</em>\nbb</p>", 104 "should support a mixed line suffix after a span (2)" 105 ); 106 107 assert_eq!( 108 to_html("*a* \t \nbb"), 109 "<p><em>a</em>\nbb</p>", 110 "should support a mixed line suffix after a span (3)" 111 ); 112 113 assert_eq!( 114 to_html_with_options( 115 "a \nb", 116 &Options { 117 parse: ParseOptions { 118 constructs: Constructs { 119 hard_break_trailing: false, 120 ..Default::default() 121 }, 122 ..Default::default() 123 }, 124 ..Default::default() 125 } 126 )?, 127 "<p>a\nb</p>", 128 "should support turning off hard break (trailing)" 129 ); 130 131 assert_eq!( 132 to_mdast("a \nb.", &Default::default())?, 133 Node::Root(Root { 134 children: vec![Node::Paragraph(Paragraph { 135 children: vec![ 136 Node::Text(Text { 137 value: "a".into(), 138 position: Some(Position::new(1, 1, 0, 1, 2, 1)) 139 }), 140 Node::Break(Break { 141 position: Some(Position::new(1, 2, 1, 2, 1, 4)) 142 }), 143 Node::Text(Text { 144 value: "b.".into(), 145 position: Some(Position::new(2, 1, 4, 2, 3, 6)) 146 }), 147 ], 148 position: Some(Position::new(1, 1, 0, 2, 3, 6)) 149 })], 150 position: Some(Position::new(1, 1, 0, 2, 3, 6)) 151 }), 152 "should support hard break (trailing) as `Break`s in mdast" 153 ); 154 155 Ok(()) 156}