| 1 | #![cfg(feature = "linkify")] |
| 2 | fn run(input: &str, output: &str) { |
| 3 | let output = if output.is_empty() { "".to_owned() } else { output.to_owned() + "\n" }; |
| 4 | let md = &mut markdown_it::MarkdownIt::new(); |
| 5 | markdown_it::plugins::cmark::add(md); |
| 6 | markdown_it::plugins::html::add(md); |
| 7 | markdown_it::plugins::extra::linkify::add(md); |
| 8 | let node = md.parse(&(input.to_owned() + "\n")); |
| 9 | |
| 10 | // make sure we have sourcemaps for everything |
| 11 | node.walk(|node, _| assert!(node.srcmap.is_some())); |
| 12 | |
| 13 | let result = node.render(); |
| 14 | assert_eq!(result, output); |
| 15 | |
| 16 | // make sure it doesn't crash without trailing \n |
| 17 | let _ = md.parse(input.trim_end()); |
| 18 | } |
| 19 | |
| 20 | #[test] |
| 21 | fn linkify() { |
| 22 | let input = r#"url http://www.youtube.com/watch?v=5Jt5GEr4AYg."#; |
| 23 | let output = r#"<p>url <a href="http://www.youtube.com/watch?v=5Jt5GEr4AYg">http://www.youtube.com/watch?v=5Jt5GEr4AYg</a>.</p>"#; |
| 24 | run(input, output); |
| 25 | } |
| 26 | |
| 27 | #[test] |
| 28 | fn don_t_touch_text_in_links() { |
| 29 | let input = r#"[https://example.com](https://example.com)"#; |
| 30 | let output = r#"<p><a href="https://example.com">https://example.com</a></p>"#; |
| 31 | run(input, output); |
| 32 | } |
| 33 | |
| 34 | #[test] |
| 35 | fn don_t_touch_text_in_autolinks() { |
| 36 | let input = r#"<https://example.com>"#; |
| 37 | let output = r#"<p><a href="https://example.com">https://example.com</a></p>"#; |
| 38 | run(input, output); |
| 39 | } |
| 40 | |
| 41 | #[test] |
| 42 | fn don_t_touch_text_in_html_a_tags() { |
| 43 | let input = r#"<a href="https://example.com">https://example.com</a>"#; |
| 44 | let output = r#"<p><a href="https://example.com">https://example.com</a></p>"#; |
| 45 | run(input, output); |
| 46 | } |
| 47 | |
| 48 | #[test] |
| 49 | fn entities_inside_raw_links() { |
| 50 | let input = r#"https://example.com/foo&amp;bar"#; |
| 51 | let output = r#"<p><a href="https://example.com/foo&amp;amp;bar">https://example.com/foo&amp;amp;bar</a></p>"#; |
| 52 | run(input, output); |
| 53 | } |
| 54 | |
| 55 | #[test] |
| 56 | fn emphasis_inside_raw_links_asterisk_can_happen_in_links_with_params() { |
| 57 | let input = r#"https://example.com/foo*bar*baz"#; |
| 58 | let output = r#"<p><a href="https://example.com/foo*bar*baz">https://example.com/foo*bar*baz</a></p>"#; |
| 59 | run(input, output); |
| 60 | } |
| 61 | |
| 62 | #[test] |
| 63 | fn emphasis_inside_raw_links_underscore() { |
| 64 | let input = r#"http://example.org/foo._bar_-_baz"#; |
| 65 | let output = r#"<p><a href="http://example.org/foo._bar_-_baz">http://example.org/foo._bar_-_baz</a></p>"#; |
| 66 | run(input, output); |
| 67 | } |
| 68 | |
| 69 | // not accepted as link by rust linkify |
| 70 | /*#[test] |
| 71 | fn backticks_inside_raw_links() { |
| 72 | let input = r#"https://example.com/foo`bar`baz"#; |
| 73 | let output = r#"<p><a href="https://example.com/foo%60bar%60baz">https://example.com/foo`bar`baz</a></p>"#; |
| 74 | run(input, output); |
| 75 | }*/ |
| 76 | |
| 77 | #[test] |
| 78 | fn links_inside_raw_links() { |
| 79 | let input = r#"https://example.com/foo[123](456)bar"#; |
| 80 | let output = r#"<p><a href="https://example.com/foo%5B123%5D(456)bar">https://example.com/foo[123](456)bar</a></p>"#; |
| 81 | run(input, output); |
| 82 | } |
| 83 | |
| 84 | #[test] |
| 85 | fn escapes_not_allowed_at_the_start() { |
| 86 | let input = r#"\https://example.com"#; |
| 87 | let output = r#"<p>\https://example.com</p>"#; |
| 88 | run(input, output); |
| 89 | } |
| 90 | |
| 91 | #[test] |
| 92 | fn escapes_not_allowed_at_comma() { |
| 93 | let input = r#"https\://example.com"#; |
| 94 | let output = r#"<p>https://example.com</p>"#; |
| 95 | run(input, output); |
| 96 | } |
| 97 | |
| 98 | #[test] |
| 99 | fn escapes_not_allowed_at_slashes() { |
| 100 | let input = r#"https:\//aa.org https://bb.org"#; |
| 101 | let output = r#"<p>https://aa.org <a href="https://bb.org">https://bb.org</a></p>"#; |
| 102 | run(input, output); |
| 103 | } |
| 104 | |
| 105 | #[test] |
| 106 | fn fuzzy_link_shouldn_t_match_cc_org() { |
| 107 | let input = r#"https:/\/cc.org"#; |
| 108 | let output = r#"<p>https://cc.org</p>"#; |
| 109 | run(input, output); |
| 110 | } |
| 111 | |
| 112 | #[test] |
| 113 | fn bold_links_exclude_markup_of_pairs_from_link_tail() { |
| 114 | let input = r#"**http://example.com/foobar**"#; |
| 115 | let output = r#"<p><strong><a href="http://example.com/foobar">http://example.com/foobar</a></strong></p>"#; |
| 116 | run(input, output); |
| 117 | } |
| 118 | |
| 119 | /*#[test] |
| 120 | fn match_links_without_protocol() { |
| 121 | let input = r#"www.example.org"#; |
| 122 | let output = r#"<p><a href="http://www.example.org">www.example.org</a></p>"#; |
| 123 | run(input, output); |
| 124 | }*/ |
| 125 | |
| 126 | /*#[test] |
| 127 | fn emails() { |
| 128 | let input = r#"test@example.com |
| 129 | |
| 130 | mailto:test@example.com"#; |
| 131 | let output = r#"<p><a href="mailto:test@example.com">test@example.com</a></p> |
| 132 | <p><a href="mailto:test@example.com">mailto:test@example.com</a></p>"#; |
| 133 | run(input, output); |
| 134 | }*/ |
| 135 | |
| 136 | #[test] |
| 137 | fn typorgapher_should_not_break_href() { |
| 138 | let input = r#"http://example.com/(c)"#; |
| 139 | let output = r#"<p><a href="http://example.com/(c)">http://example.com/(c)</a></p>"#; |
| 140 | run(input, output); |
| 141 | } |
| 142 | |
| 143 | #[test] |
| 144 | fn coverage_prefix_not_valid() { |
| 145 | let input = r#"http:/example.com/"#; |
| 146 | let output = r#"<p>http:/example.com/</p>"#; |
| 147 | run(input, output); |
| 148 | } |
| 149 | |
| 150 | #[test] |
| 151 | fn coverage_negative_link_level() { |
| 152 | let input = r#"</a>[https://example.com](https://example.com)"#; |
| 153 | let output = r#"<p></a><a href="https://example.com"><a href="https://example.com">https://example.com</a></a></p>"#; |
| 154 | run(input, output); |
| 155 | } |
| 156 | |
| 157 | /*#[test] |
| 158 | fn emphasis_with_real_link() { |
| 159 | let input = r#"http://cdecl.ridiculousfish.com/?q=int+%28*f%29+%28float+*%29%3B"#; |
| 160 | let output = r#"<p><a href="http://cdecl.ridiculousfish.com/?q=int+%28*f%29+%28float+*%29%3B">http://cdecl.ridiculousfish.com/?q=int+(*f)+(float+*)%3B</a></p>"#; |
| 161 | run(input, output); |
| 162 | }*/ |
| 163 | |
| 164 | #[test] |
| 165 | fn emphasis_with_real_link_1() { |
| 166 | let input = r#"https://www.sell.fi/sites/default/files/elainlaakarilehti/tieteelliset_artikkelit/kahkonen_t._et_al.canine_pancreatitis-_review.pdf"#; |
| 167 | let output = r#"<p><a href="https://www.sell.fi/sites/default/files/elainlaakarilehti/tieteelliset_artikkelit/kahkonen_t._et_al.canine_pancreatitis-_review.pdf">https://www.sell.fi/sites/default/files/elainlaakarilehti/tieteelliset_artikkelit/kahkonen_t._et_al.canine_pancreatitis-_review.pdf</a></p>"#; |
| 168 | run(input, output); |
| 169 | } |
| 170 | |