| 1 |
use std::borrow::Cow; |
| 2 |
|
| 3 |
use crate::{ |
| 4 |
adapters::{CodefenceRendererAdapter, HeadingAdapter, HeadingMeta, SyntaxHighlighterAdapter}, |
| 5 |
nodes::Sourcepos, |
| 6 |
}; |
| 7 |
|
| 8 |
use super::*; |
| 9 |
|
| 10 |
#[test] |
| 11 |
fn syntax_highlighter_plugin() { |
| 12 |
pub struct MockAdapter {} |
| 13 |
|
| 14 |
impl SyntaxHighlighterAdapter for MockAdapter { |
| 15 |
fn write_highlighted( |
| 16 |
&self, |
| 17 |
output: &mut dyn std::fmt::Write, |
| 18 |
lang: Option<&str>, |
| 19 |
code: &str, |
| 20 |
) -> std::fmt::Result { |
| 21 |
write!(output, "<!--{}--><span>{}</span>", lang.unwrap(), code) |
| 22 |
} |
| 23 |
|
| 24 |
fn write_pre_tag( |
| 25 |
&self, |
| 26 |
output: &mut dyn std::fmt::Write, |
| 27 |
attributes: HashMap<&'static str, Cow<str>>, |
| 28 |
) -> std::fmt::Result { |
| 29 |
html::write_opening_tag(output, "pre", attributes) |
| 30 |
} |
| 31 |
|
| 32 |
fn write_code_tag( |
| 33 |
&self, |
| 34 |
output: &mut dyn std::fmt::Write, |
| 35 |
attributes: HashMap<&'static str, Cow<str>>, |
| 36 |
) -> std::fmt::Result { |
| 37 |
html::write_opening_tag(output, "code", attributes) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
let input = concat!("``` rust yum\n", "fn main<'a>();\n", "```\n"); |
| 42 |
let expected = concat!( |
| 43 |
"<pre><code class=\"language-rust\"><!--rust--><span>fn main<'a>();\n</span>", |
| 44 |
"</code></pre>\n" |
| 45 |
); |
| 46 |
|
| 47 |
let mut plugins = options::Plugins::default(); |
| 48 |
let adapter = MockAdapter {}; |
| 49 |
plugins.render.codefence_syntax_highlighter = Some(&adapter); |
| 50 |
|
| 51 |
html_plugins(input, expected, &plugins); |
| 52 |
} |
| 53 |
|
| 54 |
#[test] |
| 55 |
fn language_specific_codefence_renderer_plugin() { |
| 56 |
struct MermaidAdapter; |
| 57 |
|
| 58 |
impl CodefenceRendererAdapter for MermaidAdapter { |
| 59 |
fn write( |
| 60 |
&self, |
| 61 |
output: &mut dyn std::fmt::Write, |
| 62 |
lang: &str, |
| 63 |
meta: &str, |
| 64 |
code: &str, |
| 65 |
_sourcepos: Option<Sourcepos>, |
| 66 |
) -> std::fmt::Result { |
| 67 |
write!( |
| 68 |
output, |
| 69 |
"<figure class=\"{lang}\" data-meta=\"{meta}\">{code}</figure>\n" |
| 70 |
) |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
let input = concat!("``` mermaid theme=dark\n", "graph TD;\n", "```\n"); |
| 75 |
let expected = "<figure class=\"mermaid\" data-meta=\"theme=dark\">graph TD;\n</figure>\n"; |
| 76 |
|
| 77 |
let mut plugins = options::Plugins::default(); |
| 78 |
let adapter = MermaidAdapter; |
| 79 |
plugins |
| 80 |
.render |
| 81 |
.codefence_renderers |
| 82 |
.insert("mermaid".to_string(), &adapter); |
| 83 |
|
| 84 |
html_plugins(input, expected, &plugins); |
| 85 |
} |
| 86 |
|
| 87 |
#[test] |
| 88 |
fn language_specific_codefence_renderer_precedes_highlighter() { |
| 89 |
struct MermaidAdapter; |
| 90 |
struct HighlighterAdapter; |
| 91 |
|
| 92 |
impl CodefenceRendererAdapter for MermaidAdapter { |
| 93 |
fn write( |
| 94 |
&self, |
| 95 |
output: &mut dyn std::fmt::Write, |
| 96 |
_lang: &str, |
| 97 |
_meta: &str, |
| 98 |
code: &str, |
| 99 |
_sourcepos: Option<Sourcepos>, |
| 100 |
) -> std::fmt::Result { |
| 101 |
write!(output, "<div>{code}</div>\n") |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
impl SyntaxHighlighterAdapter for HighlighterAdapter { |
| 106 |
fn write_highlighted( |
| 107 |
&self, |
| 108 |
output: &mut dyn std::fmt::Write, |
| 109 |
_lang: Option<&str>, |
| 110 |
code: &str, |
| 111 |
) -> std::fmt::Result { |
| 112 |
write!(output, "<span>{code}</span>") |
| 113 |
} |
| 114 |
|
| 115 |
fn write_pre_tag( |
| 116 |
&self, |
| 117 |
output: &mut dyn std::fmt::Write, |
| 118 |
attributes: HashMap<&'static str, Cow<str>>, |
| 119 |
) -> std::fmt::Result { |
| 120 |
html::write_opening_tag(output, "pre", attributes) |
| 121 |
} |
| 122 |
|
| 123 |
fn write_code_tag( |
| 124 |
&self, |
| 125 |
output: &mut dyn std::fmt::Write, |
| 126 |
attributes: HashMap<&'static str, Cow<str>>, |
| 127 |
) -> std::fmt::Result { |
| 128 |
html::write_opening_tag(output, "code", attributes) |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
let input = concat!("```mermaid\n", "graph LR;\n", "```\n"); |
| 133 |
let expected = "<div>graph LR;\n</div>\n"; |
| 134 |
|
| 135 |
let mut plugins = options::Plugins::default(); |
| 136 |
let codefence_renderer = MermaidAdapter; |
| 137 |
let syntax_highlighter = HighlighterAdapter; |
| 138 |
plugins |
| 139 |
.render |
| 140 |
.codefence_renderers |
| 141 |
.insert("mermaid".to_string(), &codefence_renderer); |
| 142 |
plugins.render.codefence_syntax_highlighter = Some(&syntax_highlighter); |
| 143 |
|
| 144 |
html_plugins(input, expected, &plugins); |
| 145 |
} |
| 146 |
|
| 147 |
#[test] |
| 148 |
fn language_specific_codefence_renderer_receives_sourcepos() { |
| 149 |
struct MermaidAdapter; |
| 150 |
|
| 151 |
impl CodefenceRendererAdapter for MermaidAdapter { |
| 152 |
fn write( |
| 153 |
&self, |
| 154 |
output: &mut dyn std::fmt::Write, |
| 155 |
_lang: &str, |
| 156 |
_meta: &str, |
| 157 |
code: &str, |
| 158 |
sourcepos: Option<Sourcepos>, |
| 159 |
) -> std::fmt::Result { |
| 160 |
let sourcepos = sourcepos.expect("sourcepos should be passed to adapter"); |
| 161 |
write!( |
| 162 |
output, |
| 163 |
"<figure data-sourcepos=\"{sourcepos}\">{code}</figure>\n" |
| 164 |
) |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
let input = concat!("```mermaid\n", "graph TD;\n", "```\n"); |
| 169 |
let expected = "<figure data-sourcepos=\"1:1-3:3\">graph TD;\n</figure>\n"; |
| 170 |
|
| 171 |
let mut plugins = options::Plugins::default(); |
| 172 |
let adapter = MermaidAdapter; |
| 173 |
plugins |
| 174 |
.render |
| 175 |
.codefence_renderers |
| 176 |
.insert("mermaid".to_string(), &adapter); |
| 177 |
|
| 178 |
let arena = Arena::new(); |
| 179 |
let mut options = Options::default(); |
| 180 |
options.render.sourcepos = true; |
| 181 |
let root = parse_document(&arena, input, &options); |
| 182 |
let mut output = String::new(); |
| 183 |
html::format_document_with_plugins(root, &options, &mut output, &plugins).unwrap(); |
| 184 |
compare_strs(&output, expected, "regular", input); |
| 185 |
} |
| 186 |
|
| 187 |
#[test] |
| 188 |
fn syntax_highlighter_still_handles_unmatched_codefence_renderer() { |
| 189 |
struct UnusedCodefenceRenderer; |
| 190 |
struct HighlighterAdapter; |
| 191 |
|
| 192 |
impl CodefenceRendererAdapter for UnusedCodefenceRenderer { |
| 193 |
fn write( |
| 194 |
&self, |
| 195 |
output: &mut dyn std::fmt::Write, |
| 196 |
_lang: &str, |
| 197 |
_meta: &str, |
| 198 |
_code: &str, |
| 199 |
_sourcepos: Option<Sourcepos>, |
| 200 |
) -> std::fmt::Result { |
| 201 |
output.write_str("<div>should-not-run</div>\n") |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
impl SyntaxHighlighterAdapter for HighlighterAdapter { |
| 206 |
fn write_highlighted( |
| 207 |
&self, |
| 208 |
output: &mut dyn std::fmt::Write, |
| 209 |
lang: Option<&str>, |
| 210 |
code: &str, |
| 211 |
) -> std::fmt::Result { |
| 212 |
write!(output, "<!--{}--><span>{code}</span>", lang.unwrap()) |
| 213 |
} |
| 214 |
|
| 215 |
fn write_pre_tag( |
| 216 |
&self, |
| 217 |
output: &mut dyn std::fmt::Write, |
| 218 |
attributes: HashMap<&'static str, Cow<str>>, |
| 219 |
) -> std::fmt::Result { |
| 220 |
html::write_opening_tag(output, "pre", attributes) |
| 221 |
} |
| 222 |
|
| 223 |
fn write_code_tag( |
| 224 |
&self, |
| 225 |
output: &mut dyn std::fmt::Write, |
| 226 |
attributes: HashMap<&'static str, Cow<str>>, |
| 227 |
) -> std::fmt::Result { |
| 228 |
html::write_opening_tag(output, "code", attributes) |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
let input = concat!("``` mermaid theme=dark\n", "graph TD;\n", "```\n"); |
| 233 |
let expected = concat!( |
| 234 |
"<pre><code class=\"language-mermaid\"><!--mermaid--><span>graph TD;\n</span>", |
| 235 |
"</code></pre>\n" |
| 236 |
); |
| 237 |
|
| 238 |
let mut plugins = options::Plugins::default(); |
| 239 |
let unused_renderer = UnusedCodefenceRenderer; |
| 240 |
let syntax_highlighter = HighlighterAdapter; |
| 241 |
plugins |
| 242 |
.render |
| 243 |
.codefence_renderers |
| 244 |
.insert("mermaid theme=dark".to_string(), &unused_renderer); |
| 245 |
plugins.render.codefence_syntax_highlighter = Some(&syntax_highlighter); |
| 246 |
|
| 247 |
html_plugins(input, expected, &plugins); |
| 248 |
} |
| 249 |
|
| 250 |
#[test] |
| 251 |
fn math_codefence_renderer_is_not_overridden_by_custom_renderer() { |
| 252 |
struct MathAdapter; |
| 253 |
|
| 254 |
impl CodefenceRendererAdapter for MathAdapter { |
| 255 |
fn write( |
| 256 |
&self, |
| 257 |
output: &mut dyn std::fmt::Write, |
| 258 |
_lang: &str, |
| 259 |
_meta: &str, |
| 260 |
_code: &str, |
| 261 |
_sourcepos: Option<Sourcepos>, |
| 262 |
) -> std::fmt::Result { |
| 263 |
output.write_str("<div>custom-math</div>\n") |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
let input = concat!("```math\n", "x^2\n", "```\n"); |
| 268 |
let expected = |
| 269 |
"<pre><code class=\"language-math\" data-math-style=\"display\">x^2\n</code></pre>\n"; |
| 270 |
|
| 271 |
let mut plugins = options::Plugins::default(); |
| 272 |
let adapter = MathAdapter; |
| 273 |
plugins |
| 274 |
.render |
| 275 |
.codefence_renderers |
| 276 |
.insert("math".to_string(), &adapter); |
| 277 |
|
| 278 |
html_plugins(input, expected, &plugins); |
| 279 |
} |
| 280 |
|
| 281 |
#[test] |
| 282 |
fn empty_info_codefence_does_not_use_custom_renderer() { |
| 283 |
struct EmptyLangAdapter; |
| 284 |
|
| 285 |
impl CodefenceRendererAdapter for EmptyLangAdapter { |
| 286 |
fn write( |
| 287 |
&self, |
| 288 |
output: &mut dyn std::fmt::Write, |
| 289 |
_lang: &str, |
| 290 |
_meta: &str, |
| 291 |
_code: &str, |
| 292 |
_sourcepos: Option<Sourcepos>, |
| 293 |
) -> std::fmt::Result { |
| 294 |
output.write_str("<div>should-not-run</div>\n") |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
let input = concat!("```\n", "plain\n", "```\n"); |
| 299 |
let expected = "<pre><code>plain\n</code></pre>\n"; |
| 300 |
|
| 301 |
let mut plugins = options::Plugins::default(); |
| 302 |
let adapter = EmptyLangAdapter; |
| 303 |
plugins |
| 304 |
.render |
| 305 |
.codefence_renderers |
| 306 |
.insert(String::new(), &adapter); |
| 307 |
|
| 308 |
html_plugins(input, expected, &plugins); |
| 309 |
} |
| 310 |
|
| 311 |
#[test] |
| 312 |
fn heading_adapter_plugin() { |
| 313 |
struct MockAdapter; |
| 314 |
|
| 315 |
impl HeadingAdapter for MockAdapter { |
| 316 |
fn enter( |
| 317 |
&self, |
| 318 |
output: &mut dyn std::fmt::Write, |
| 319 |
heading: &HeadingMeta, |
| 320 |
_sourcepos: Option<Sourcepos>, |
| 321 |
) -> std::fmt::Result { |
| 322 |
write!(output, "<h{} data-heading=\"true\">", heading.level + 1) |
| 323 |
} |
| 324 |
|
| 325 |
fn exit( |
| 326 |
&self, |
| 327 |
output: &mut dyn std::fmt::Write, |
| 328 |
heading: &HeadingMeta, |
| 329 |
) -> std::fmt::Result { |
| 330 |
write!(output, "</h{}>", heading.level + 1) |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
let mut plugins = options::Plugins::default(); |
| 335 |
let adapter = MockAdapter {}; |
| 336 |
plugins.render.heading_adapter = Some(&adapter); |
| 337 |
|
| 338 |
let cases: Vec<(&str, &str)> = vec![ |
| 339 |
( |
| 340 |
"# Simple heading", |
| 341 |
"<h2 data-heading=\"true\">Simple heading</h2>", |
| 342 |
), |
| 343 |
( |
| 344 |
"## Heading with **bold text** and `code`", |
| 345 |
"<h3 data-heading=\"true\">Heading with <strong>bold text</strong> and <code>code</code></h3>", |
| 346 |
), |
| 347 |
( |
| 348 |
"###### Whoa, an h7!", |
| 349 |
"<h7 data-heading=\"true\">Whoa, an h7!</h7>", |
| 350 |
), |
| 351 |
( |
| 352 |
"####### This is not a heading", |
| 353 |
"<p>####### This is not a heading</p>\n", |
| 354 |
), |
| 355 |
]; |
| 356 |
for (input, expected) in cases { |
| 357 |
html_plugins(input, expected, &plugins); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
#[test] |
| 362 |
#[cfg(feature = "syntect")] |
| 363 |
fn syntect_plugin_with_base16_ocean_dark_theme() { |
| 364 |
let adapter = crate::plugins::syntect::SyntectAdapter::new(Some("base16-ocean.dark")); |
| 365 |
|
| 366 |
let cases: [(&str, &str); 2] = [ |
| 367 |
( |
| 368 |
concat!("```rust\n", "fn main<'a>();\n", "```\n"), |
| 369 |
concat!( |
| 370 |
"<pre style=\"background-color:#2b303b;\"><code class=\"language-rust\">", |
| 371 |
"<span style=\"color:#b48ead;\">fn </span><span style=\"color:#8fa1b3;\">main</span><span style=\"color:#c0c5ce;\">", |
| 372 |
"<</span><span style=\"color:#b48ead;\">'a</span><span style=\"color:#c0c5ce;\">>();\n</span>", |
| 373 |
"</code></pre>\n" |
| 374 |
), |
| 375 |
), |
| 376 |
( |
| 377 |
concat!("```rust,ignore\n", "fn main() {}\n", "```\n"), |
| 378 |
concat!( |
| 379 |
"<pre style=\"background-color:#2b303b;\"><code class=\"language-rust,ignore\">", |
| 380 |
"<span style=\"color:#b48ead;\">fn </span><span style=\"color:#8fa1b3;\">main</span><span style=\"color:#c0c5ce;\">() {}\n</span>", |
| 381 |
"</code></pre>\n" |
| 382 |
), |
| 383 |
), |
| 384 |
]; |
| 385 |
|
| 386 |
let mut plugins = options::Plugins::default(); |
| 387 |
plugins.render.codefence_syntax_highlighter = Some(&adapter); |
| 388 |
|
| 389 |
for (input, expected) in cases { |
| 390 |
html_plugins(input, expected, &plugins); |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
#[test] |
| 395 |
#[cfg(feature = "syntect")] |
| 396 |
fn syntect_plugin_with_css_classes() { |
| 397 |
let adapter = crate::plugins::syntect::SyntectAdapter::new(None); |
| 398 |
|
| 399 |
let input = concat!("```rust\n", "fn main<'a>();\n", "```\n"); |
| 400 |
let expected = concat!( |
| 401 |
"<pre class=\"syntax-highlighting\"><code class=\"language-rust\">", |
| 402 |
"<span class=\"source rust\"><span class=\"meta function rust\"><span class=\"meta function rust\"><span class=\"storage type function rust\">fn</span> </span><span class=\"entity name function rust\">main</span></span><span class=\"meta generic rust\"><span class=\"punctuation definition generic begin rust\"><</span>", |
| 403 |
"<span class=\"storage modifier lifetime rust\">'a</span><span class=\"punctuation definition generic end rust\">></span></span><span class=\"meta function rust\"><span class=\"meta function parameters rust\"><span class=\"punctuation section parameters begin rust\">(</span></span><span class=\"meta function rust\">", |
| 404 |
"<span class=\"meta function parameters rust\"><span class=\"punctuation section parameters end rust\">)</span></span></span></span><span class=\"punctuation terminator rust\">;</span>\n</span>", |
| 405 |
"</code></pre>\n", |
| 406 |
); |
| 407 |
|
| 408 |
let mut plugins = options::Plugins::default(); |
| 409 |
plugins.render.codefence_syntax_highlighter = Some(&adapter); |
| 410 |
|
| 411 |
html_plugins(input, expected, &plugins); |
| 412 |
} |
| 413 |
|
| 414 |
#[test] |
| 415 |
#[cfg(feature = "syntect")] |
| 416 |
fn syntect_plugin_with_prefixed_css_classes() { |
| 417 |
let prefix = Box::leak(Box::new("prefix-")); |
| 418 |
let adapter = crate::plugins::syntect::SyntectAdapterBuilder::new() |
| 419 |
.css_with_class_prefix(prefix) |
| 420 |
.build(); |
| 421 |
|
| 422 |
let input = concat!("```rust\n", "fn main<'a>();\n", "```\n"); |
| 423 |
|
| 424 |
let expected = concat!( |
| 425 |
"<pre class=\"syntax-highlighting\"><code class=\"language-rust\">", |
| 426 |
"<span class=\"prefix-source prefix-rust\"><span class=\"prefix-meta prefix-function prefix-rust\"><span class=\"prefix-meta prefix-function prefix-rust\"><span class=\"prefix-storage prefix-type prefix-function prefix-rust\">fn</span> </span><span class=\"prefix-entity prefix-name prefix-function prefix-rust\">main</span></span><span class=\"prefix-meta prefix-generic prefix-rust\"><span class=\"prefix-punctuation prefix-definition prefix-generic prefix-begin prefix-rust\"><</span>", |
| 427 |
"<span class=\"prefix-storage prefix-modifier prefix-lifetime prefix-rust\">'a</span><span class=\"prefix-punctuation prefix-definition prefix-generic prefix-end prefix-rust\">></span></span><span class=\"prefix-meta prefix-function prefix-rust\"><span class=\"prefix-meta prefix-function prefix-parameters prefix-rust\"><span class=\"prefix-punctuation prefix-section prefix-parameters prefix-begin prefix-rust\">(</span></span><span class=\"prefix-meta prefix-function prefix-rust\">", |
| 428 |
"<span class=\"prefix-meta prefix-function prefix-parameters prefix-rust\"><span class=\"prefix-punctuation prefix-section prefix-parameters prefix-end prefix-rust\">)</span></span></span></span><span class=\"prefix-punctuation prefix-terminator prefix-rust\">;</span>\n</span>", |
| 429 |
"</code></pre>\n", |
| 430 |
); |
| 431 |
|
| 432 |
let mut plugins = options::Plugins::default(); |
| 433 |
plugins.render.codefence_syntax_highlighter = Some(&adapter); |
| 434 |
|
| 435 |
html_plugins(input, expected, &plugins); |
| 436 |
} |