You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

885 lines
28KB

  1. use std::collections::HashMap;
  2. use tera::Tera;
  3. use config::Config;
  4. use front_matter::InsertAnchor;
  5. use rendering::{render_content, RenderContext};
  6. use templates::ZOLA_TERA;
  7. use utils::slugs::SlugifyStrategy;
  8. #[test]
  9. fn can_do_render_content_simple() {
  10. let tera_ctx = Tera::default();
  11. let permalinks_ctx = HashMap::new();
  12. let config = Config::default();
  13. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  14. let res = render_content("hello", &context).unwrap();
  15. assert_eq!(res.body, "<p>hello</p>\n");
  16. }
  17. #[test]
  18. fn doesnt_highlight_code_block_with_highlighting_off() {
  19. let tera_ctx = Tera::default();
  20. let permalinks_ctx = HashMap::new();
  21. let mut config = Config::default();
  22. config.highlight_code = false;
  23. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  24. let res = render_content("```\n$ gutenberg server\n```", &context).unwrap();
  25. assert_eq!(res.body, "<pre><code>$ gutenberg server\n</code></pre>\n");
  26. }
  27. #[test]
  28. fn can_highlight_code_block_no_lang() {
  29. let tera_ctx = Tera::default();
  30. let permalinks_ctx = HashMap::new();
  31. let mut config = Config::default();
  32. config.highlight_code = true;
  33. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  34. let res = render_content("```\n$ gutenberg server\n$ ping\n```", &context).unwrap();
  35. assert_eq!(
  36. res.body,
  37. "<pre style=\"background-color:#2b303b;\">\n<span style=\"color:#c0c5ce;\">$ gutenberg server\n$ ping\n</span></pre>"
  38. );
  39. }
  40. #[test]
  41. fn can_highlight_code_block_with_lang() {
  42. let tera_ctx = Tera::default();
  43. let permalinks_ctx = HashMap::new();
  44. let mut config = Config::default();
  45. config.highlight_code = true;
  46. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  47. let res = render_content("```python\nlist.append(1)\n```", &context).unwrap();
  48. assert_eq!(
  49. res.body,
  50. "<pre style=\"background-color:#2b303b;\">\n<span style=\"color:#c0c5ce;\">list.</span><span style=\"color:#bf616a;\">append</span><span style=\"color:#c0c5ce;\">(</span><span style=\"color:#d08770;\">1</span><span style=\"color:#c0c5ce;\">)\n</span></pre>"
  51. );
  52. }
  53. #[test]
  54. fn can_higlight_code_block_with_unknown_lang() {
  55. let tera_ctx = Tera::default();
  56. let permalinks_ctx = HashMap::new();
  57. let mut config = Config::default();
  58. config.highlight_code = true;
  59. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  60. let res = render_content("```yolo\nlist.append(1)\n```", &context).unwrap();
  61. // defaults to plain text
  62. assert_eq!(
  63. res.body,
  64. "<pre style=\"background-color:#2b303b;\">\n<span style=\"color:#c0c5ce;\">list.append(1)\n</span></pre>"
  65. );
  66. }
  67. #[test]
  68. fn can_render_shortcode() {
  69. let permalinks_ctx = HashMap::new();
  70. let config = Config::default();
  71. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  72. let res = render_content(
  73. r#"
  74. Hello
  75. {{ youtube(id="ub36ffWAqgQ") }}
  76. "#,
  77. &context,
  78. )
  79. .unwrap();
  80. assert!(res.body.contains("<p>Hello</p>\n<div >"));
  81. assert!(res.body.contains(r#"<iframe src="https://www.youtube.com/embed/ub36ffWAqgQ""#));
  82. }
  83. #[test]
  84. fn can_render_shortcode_with_markdown_char_in_args_name() {
  85. let permalinks_ctx = HashMap::new();
  86. let config = Config::default();
  87. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  88. let input = vec!["name", "na_me", "n_a_me", "n1"];
  89. for i in input {
  90. let res =
  91. render_content(&format!("{{{{ youtube(id=\"hey\", {}=1) }}}}", i), &context).unwrap();
  92. assert!(res.body.contains(r#"<iframe src="https://www.youtube.com/embed/hey""#));
  93. }
  94. }
  95. #[test]
  96. fn can_render_shortcode_with_markdown_char_in_args_value() {
  97. let permalinks_ctx = HashMap::new();
  98. let config = Config::default();
  99. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  100. let input = vec![
  101. "ub36ffWAqgQ-hey",
  102. "ub36ffWAqgQ_hey",
  103. "ub36ffWAqgQ_he_y",
  104. "ub36ffWAqgQ*hey",
  105. "ub36ffWAqgQ#hey",
  106. ];
  107. for i in input {
  108. let res = render_content(&format!("{{{{ youtube(id=\"{}\") }}}}", i), &context).unwrap();
  109. assert!(res
  110. .body
  111. .contains(&format!(r#"<iframe src="https://www.youtube.com/embed/{}""#, i)));
  112. }
  113. }
  114. #[test]
  115. fn can_render_body_shortcode_with_markdown_char_in_name() {
  116. let permalinks_ctx = HashMap::new();
  117. let mut tera = Tera::default();
  118. tera.extend(&ZOLA_TERA).unwrap();
  119. let input = vec!["quo_te", "qu_o_te"];
  120. let config = Config::default();
  121. for i in input {
  122. tera.add_raw_template(
  123. &format!("shortcodes/{}.html", i),
  124. "<blockquote>{{ body }} - {{ author}}</blockquote>",
  125. )
  126. .unwrap();
  127. let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
  128. let res =
  129. render_content(&format!("{{% {}(author=\"Bob\") %}}\nhey\n{{% end %}}", i), &context)
  130. .unwrap();
  131. println!("{:?}", res);
  132. assert!(res.body.contains("<blockquote>hey - Bob</blockquote>"));
  133. }
  134. }
  135. #[test]
  136. fn can_render_body_shortcode_and_paragraph_after() {
  137. let permalinks_ctx = HashMap::new();
  138. let mut tera = Tera::default();
  139. tera.extend(&ZOLA_TERA).unwrap();
  140. let shortcode = "<p>{{ body }}</p>";
  141. let markdown_string = r#"
  142. {% figure() %}
  143. This is a figure caption.
  144. {% end %}
  145. Here is another paragraph.
  146. "#;
  147. let expected = "<p>This is a figure caption.</p>
  148. <p>Here is another paragraph.</p>
  149. ";
  150. tera.add_raw_template(&format!("shortcodes/{}.html", "figure"), shortcode).unwrap();
  151. let config = Config::default();
  152. let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
  153. let res = render_content(markdown_string, &context).unwrap();
  154. println!("{:?}", res);
  155. assert_eq!(res.body, expected);
  156. }
  157. #[test]
  158. fn can_render_two_body_shortcode_and_paragraph_after_with_line_break_between() {
  159. let permalinks_ctx = HashMap::new();
  160. let mut tera = Tera::default();
  161. tera.extend(&ZOLA_TERA).unwrap();
  162. let shortcode = "<p>{{ body }}</p>";
  163. let markdown_string = r#"
  164. {% figure() %}
  165. This is a figure caption.
  166. {% end %}
  167. {% figure() %}
  168. This is a figure caption.
  169. {% end %}
  170. Here is another paragraph.
  171. "#;
  172. let expected = "<p>This is a figure caption.</p>
  173. <p>This is a figure caption.</p>
  174. <p>Here is another paragraph.</p>
  175. ";
  176. tera.add_raw_template(&format!("shortcodes/{}.html", "figure"), shortcode).unwrap();
  177. let config = Config::default();
  178. let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
  179. let res = render_content(markdown_string, &context).unwrap();
  180. println!("{:?}", res);
  181. assert_eq!(res.body, expected);
  182. }
  183. #[test]
  184. fn can_render_several_shortcode_in_row() {
  185. let permalinks_ctx = HashMap::new();
  186. let config = Config::default();
  187. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  188. let res = render_content(
  189. r#"
  190. Hello
  191. {{ youtube(id="ub36ffWAqgQ") }}
  192. {{ youtube(id="ub36ffWAqgQ", autoplay=true) }}
  193. {{ vimeo(id="210073083") }}
  194. {{ streamable(id="c0ic") }}
  195. {{ gist(url="https://gist.github.com/Keats/32d26f699dcc13ebd41b") }}
  196. "#,
  197. &context,
  198. )
  199. .unwrap();
  200. assert!(res.body.contains("<p>Hello</p>\n<div >"));
  201. assert!(res.body.contains(r#"<iframe src="https://www.youtube.com/embed/ub36ffWAqgQ""#));
  202. assert!(res
  203. .body
  204. .contains(r#"<iframe src="https://www.youtube.com/embed/ub36ffWAqgQ?autoplay=1""#));
  205. assert!(res.body.contains(r#"<iframe src="https://www.streamable.com/e/c0ic""#));
  206. assert!(res.body.contains(r#"//player.vimeo.com/video/210073083""#));
  207. }
  208. #[test]
  209. fn doesnt_render_ignored_shortcodes() {
  210. let permalinks_ctx = HashMap::new();
  211. let mut config = Config::default();
  212. config.highlight_code = false;
  213. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  214. let res = render_content(r#"```{{/* youtube(id="w7Ft2ymGmfc") */}}```"#, &context).unwrap();
  215. assert_eq!(res.body, "<p><code>{{ youtube(id=&quot;w7Ft2ymGmfc&quot;) }}</code></p>\n");
  216. }
  217. #[test]
  218. fn can_render_shortcode_with_body() {
  219. let mut tera = Tera::default();
  220. tera.extend(&ZOLA_TERA).unwrap();
  221. tera.add_raw_template(
  222. "shortcodes/quote.html",
  223. "<blockquote>{{ body }} - {{ author }}</blockquote>",
  224. )
  225. .unwrap();
  226. let permalinks_ctx = HashMap::new();
  227. let config = Config::default();
  228. let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
  229. let res = render_content(
  230. r#"
  231. Hello
  232. {% quote(author="Keats") %}
  233. A quote
  234. {% end %}
  235. "#,
  236. &context,
  237. )
  238. .unwrap();
  239. assert_eq!(res.body, "<p>Hello</p>\n<blockquote>A quote - Keats</blockquote>\n");
  240. }
  241. #[test]
  242. fn errors_rendering_unknown_shortcode() {
  243. let tera_ctx = Tera::default();
  244. let permalinks_ctx = HashMap::new();
  245. let config = Config::default();
  246. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  247. let res = render_content("{{ hello(flash=true) }}", &context);
  248. assert!(res.is_err());
  249. }
  250. #[test]
  251. fn can_make_valid_relative_link() {
  252. let mut permalinks = HashMap::new();
  253. permalinks.insert("pages/about.md".to_string(), "https://vincent.is/about".to_string());
  254. let tera_ctx = Tera::default();
  255. let config = Config::default();
  256. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks, InsertAnchor::None);
  257. let res = render_content(
  258. r#"[rel link](@/pages/about.md), [abs link](https://vincent.is/about)"#,
  259. &context,
  260. )
  261. .unwrap();
  262. assert!(
  263. res.body.contains(r#"<p><a href="https://vincent.is/about">rel link</a>, <a href="https://vincent.is/about">abs link</a></p>"#)
  264. );
  265. }
  266. #[test]
  267. fn can_make_relative_links_with_anchors() {
  268. let mut permalinks = HashMap::new();
  269. permalinks.insert("pages/about.md".to_string(), "https://vincent.is/about".to_string());
  270. let tera_ctx = Tera::default();
  271. let config = Config::default();
  272. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks, InsertAnchor::None);
  273. let res = render_content(r#"[rel link](@/pages/about.md#cv)"#, &context).unwrap();
  274. assert!(res.body.contains(r#"<p><a href="https://vincent.is/about#cv">rel link</a></p>"#));
  275. }
  276. #[test]
  277. fn errors_relative_link_inexistant() {
  278. let tera_ctx = Tera::default();
  279. let permalinks_ctx = HashMap::new();
  280. let config = Config::default();
  281. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  282. let res = render_content("[rel link](@/pages/about.md)", &context);
  283. assert!(res.is_err());
  284. }
  285. #[test]
  286. fn can_add_id_to_headings() {
  287. let tera_ctx = Tera::default();
  288. let permalinks_ctx = HashMap::new();
  289. let config = Config::default();
  290. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  291. let res = render_content(r#"# Hello"#, &context).unwrap();
  292. assert_eq!(res.body, "<h1 id=\"hello\">Hello</h1>\n");
  293. }
  294. #[test]
  295. fn can_add_id_to_headings_same_slug() {
  296. let tera_ctx = Tera::default();
  297. let permalinks_ctx = HashMap::new();
  298. let config = Config::default();
  299. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  300. let res = render_content("# Hello\n# Hello", &context).unwrap();
  301. assert_eq!(res.body, "<h1 id=\"hello\">Hello</h1>\n<h1 id=\"hello-1\">Hello</h1>\n");
  302. }
  303. #[test]
  304. fn can_add_non_slug_id_to_headings() {
  305. let tera_ctx = Tera::default();
  306. let permalinks_ctx = HashMap::new();
  307. let mut config = Config::default();
  308. config.slugify.anchors = SlugifyStrategy::Safe;
  309. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  310. let res = render_content(r#"# L'écologie et vous"#, &context).unwrap();
  311. assert_eq!(res.body, "<h1 id=\"L'écologie_et_vous\">L'écologie et vous</h1>\n");
  312. }
  313. #[test]
  314. fn can_handle_manual_ids_on_headings() {
  315. let tera_ctx = Tera::default();
  316. let permalinks_ctx = HashMap::new();
  317. let config = Config::default();
  318. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  319. // Tested things: manual IDs; whitespace flexibility; that automatic IDs avoid collision with
  320. // manual IDs; that duplicates are in fact permitted among manual IDs; that any non-plain-text
  321. // in the middle of `{#…}` will disrupt it from being acknowledged as a manual ID (that last
  322. // one could reasonably be considered a bug rather than a feature, but test it either way); one
  323. // workaround for the improbable case where you actually want `{#…}` at the end of a heading.
  324. let res = render_content(
  325. "\
  326. # Hello\n\
  327. # Hello{#hello}\n\
  328. # Hello {#hello}\n\
  329. # Hello {#Something_else} \n\
  330. # Workaround for literal {#…&#125;\n\
  331. # Hello\n\
  332. # Auto {#*matic*}",
  333. &context,
  334. )
  335. .unwrap();
  336. assert_eq!(
  337. res.body,
  338. "\
  339. <h1 id=\"hello-1\">Hello</h1>\n\
  340. <h1 id=\"hello\">Hello</h1>\n\
  341. <h1 id=\"hello\">Hello</h1>\n\
  342. <h1 id=\"Something_else\">Hello</h1>\n\
  343. <h1 id=\"workaround-for-literal\">Workaround for literal {#…}</h1>\n\
  344. <h1 id=\"hello-2\">Hello</h1>\n\
  345. <h1 id=\"auto-matic\">Auto {#<em>matic</em>}</h1>\n\
  346. "
  347. );
  348. }
  349. #[test]
  350. fn blank_headings() {
  351. let tera_ctx = Tera::default();
  352. let permalinks_ctx = HashMap::new();
  353. let config = Config::default();
  354. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  355. let res = render_content("# \n#\n# {#hmm} \n# {#}", &context).unwrap();
  356. assert_eq!(
  357. res.body,
  358. "<h1 id=\"-1\"></h1>\n<h1 id=\"-2\"></h1>\n<h1 id=\"hmm\"></h1>\n<h1 id=\"\"></h1>\n"
  359. );
  360. }
  361. #[test]
  362. fn can_insert_anchor_left() {
  363. let permalinks_ctx = HashMap::new();
  364. let config = Config::default();
  365. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Left);
  366. let res = render_content("# Hello", &context).unwrap();
  367. assert_eq!(
  368. res.body,
  369. "<h1 id=\"hello\"><a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>Hello</h1>\n"
  370. );
  371. }
  372. #[test]
  373. fn can_insert_anchor_right() {
  374. let permalinks_ctx = HashMap::new();
  375. let config = Config::default();
  376. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Right);
  377. let res = render_content("# Hello", &context).unwrap();
  378. assert_eq!(
  379. res.body,
  380. "<h1 id=\"hello\">Hello<a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a></h1>\n"
  381. );
  382. }
  383. #[test]
  384. fn can_insert_anchor_for_multi_heading() {
  385. let permalinks_ctx = HashMap::new();
  386. let config = Config::default();
  387. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Right);
  388. let res = render_content("# Hello\n# World", &context).unwrap();
  389. assert_eq!(
  390. res.body,
  391. "<h1 id=\"hello\">Hello<a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a></h1>\n\
  392. <h1 id=\"world\">World<a class=\"zola-anchor\" href=\"#world\" aria-label=\"Anchor link for: world\">🔗</a></h1>\n"
  393. );
  394. }
  395. // See https://github.com/Keats/gutenberg/issues/42
  396. #[test]
  397. fn can_insert_anchor_with_exclamation_mark() {
  398. let permalinks_ctx = HashMap::new();
  399. let config = Config::default();
  400. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Left);
  401. let res = render_content("# Hello!", &context).unwrap();
  402. assert_eq!(
  403. res.body,
  404. "<h1 id=\"hello\"><a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>Hello!</h1>\n"
  405. );
  406. }
  407. // See https://github.com/Keats/gutenberg/issues/53
  408. #[test]
  409. fn can_insert_anchor_with_link() {
  410. let permalinks_ctx = HashMap::new();
  411. let config = Config::default();
  412. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Left);
  413. let res = render_content("## [Rust](https://rust-lang.org)", &context).unwrap();
  414. assert_eq!(
  415. res.body,
  416. "<h2 id=\"rust\"><a class=\"zola-anchor\" href=\"#rust\" aria-label=\"Anchor link for: rust\">🔗</a><a href=\"https://rust-lang.org\">Rust</a></h2>\n"
  417. );
  418. }
  419. #[test]
  420. fn can_insert_anchor_with_other_special_chars() {
  421. let permalinks_ctx = HashMap::new();
  422. let config = Config::default();
  423. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Left);
  424. let res = render_content("# Hello*_()", &context).unwrap();
  425. assert_eq!(
  426. res.body,
  427. "<h1 id=\"hello\"><a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>Hello*_()</h1>\n"
  428. );
  429. }
  430. #[test]
  431. fn can_make_toc() {
  432. let permalinks_ctx = HashMap::new();
  433. let config = Config::default();
  434. let context = RenderContext::new(
  435. &ZOLA_TERA,
  436. &config,
  437. "https://mysite.com/something",
  438. &permalinks_ctx,
  439. InsertAnchor::Left,
  440. );
  441. let res = render_content(
  442. r#"
  443. # Heading 1
  444. ## Heading 2
  445. ## Another Heading 2
  446. ### Last one
  447. "#,
  448. &context,
  449. )
  450. .unwrap();
  451. let toc = res.toc;
  452. assert_eq!(toc.len(), 1);
  453. assert_eq!(toc[0].children.len(), 2);
  454. assert_eq!(toc[0].children[1].children.len(), 1);
  455. }
  456. #[test]
  457. fn can_ignore_tags_in_toc() {
  458. let permalinks_ctx = HashMap::new();
  459. let config = Config::default();
  460. let context = RenderContext::new(
  461. &ZOLA_TERA,
  462. &config,
  463. "https://mysite.com/something",
  464. &permalinks_ctx,
  465. InsertAnchor::Left,
  466. );
  467. let res = render_content(
  468. r#"
  469. ## heading with `code`
  470. ## [anchor](https://duckduckgo.com/) in heading
  471. ## **bold** and *italics*
  472. "#,
  473. &context,
  474. )
  475. .unwrap();
  476. let toc = res.toc;
  477. assert_eq!(toc[0].id, "heading-with-code");
  478. assert_eq!(toc[0].title, "heading with code");
  479. assert_eq!(toc[1].id, "anchor-in-heading");
  480. assert_eq!(toc[1].title, "anchor in heading");
  481. assert_eq!(toc[2].id, "bold-and-italics");
  482. assert_eq!(toc[2].title, "bold and italics");
  483. }
  484. #[test]
  485. fn can_understand_backtick_in_titles() {
  486. let permalinks_ctx = HashMap::new();
  487. let config = Config::default();
  488. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  489. let res = render_content("# `Hello`", &context).unwrap();
  490. assert_eq!(res.body, "<h1 id=\"hello\"><code>Hello</code></h1>\n");
  491. }
  492. #[test]
  493. fn can_understand_backtick_in_paragraphs() {
  494. let permalinks_ctx = HashMap::new();
  495. let config = Config::default();
  496. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  497. let res = render_content("Hello `world`", &context).unwrap();
  498. assert_eq!(res.body, "<p>Hello <code>world</code></p>\n");
  499. }
  500. // https://github.com/Keats/gutenberg/issues/297
  501. #[test]
  502. fn can_understand_links_in_heading() {
  503. let permalinks_ctx = HashMap::new();
  504. let config = Config::default();
  505. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  506. let res = render_content("# [Rust](https://rust-lang.org)", &context).unwrap();
  507. assert_eq!(res.body, "<h1 id=\"rust\"><a href=\"https://rust-lang.org\">Rust</a></h1>\n");
  508. }
  509. #[test]
  510. fn can_understand_link_with_title_in_heading() {
  511. let permalinks_ctx = HashMap::new();
  512. let config = Config::default();
  513. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  514. let res =
  515. render_content("# [Rust](https://rust-lang.org \"Rust homepage\")", &context).unwrap();
  516. assert_eq!(
  517. res.body,
  518. "<h1 id=\"rust\"><a href=\"https://rust-lang.org\" title=\"Rust homepage\">Rust</a></h1>\n"
  519. );
  520. }
  521. #[test]
  522. fn can_understand_emphasis_in_heading() {
  523. let permalinks_ctx = HashMap::new();
  524. let config = Config::default();
  525. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  526. let res = render_content("# *Emphasis* text", &context).unwrap();
  527. assert_eq!(res.body, "<h1 id=\"emphasis-text\"><em>Emphasis</em> text</h1>\n");
  528. }
  529. #[test]
  530. fn can_understand_strong_in_heading() {
  531. let permalinks_ctx = HashMap::new();
  532. let config = Config::default();
  533. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  534. let res = render_content("# **Strong** text", &context).unwrap();
  535. assert_eq!(res.body, "<h1 id=\"strong-text\"><strong>Strong</strong> text</h1>\n");
  536. }
  537. #[test]
  538. fn can_understand_code_in_heading() {
  539. let permalinks_ctx = HashMap::new();
  540. let config = Config::default();
  541. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  542. let res = render_content("# `Code` text", &context).unwrap();
  543. assert_eq!(res.body, "<h1 id=\"code-text\"><code>Code</code> text</h1>\n");
  544. }
  545. // See https://github.com/getzola/zola/issues/569
  546. #[test]
  547. fn can_understand_footnote_in_heading() {
  548. let permalinks_ctx = HashMap::new();
  549. let config = Config::default();
  550. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  551. let res = render_content("# text [^1] there\n[^1]: footnote", &context).unwrap();
  552. assert_eq!(res.body, r##"<h1 id="text-there">text <sup class="footnote-reference"><a href="#1">1</a></sup> there</h1>
  553. <div class="footnote-definition" id="1"><sup class="footnote-definition-label">1</sup>
  554. <p>footnote</p>
  555. </div>
  556. "##);
  557. }
  558. #[test]
  559. fn can_make_valid_relative_link_in_heading() {
  560. let mut permalinks = HashMap::new();
  561. permalinks.insert("pages/about.md".to_string(), "https://vincent.is/about/".to_string());
  562. let tera_ctx = Tera::default();
  563. let config = Config::default();
  564. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks, InsertAnchor::None);
  565. let res = render_content(r#" # [rel link](@/pages/about.md)"#, &context).unwrap();
  566. assert_eq!(
  567. res.body,
  568. "<h1 id=\"rel-link\"><a href=\"https://vincent.is/about/\">rel link</a></h1>\n"
  569. );
  570. }
  571. #[test]
  572. fn can_make_permalinks_with_colocated_assets_for_link() {
  573. let permalinks_ctx = HashMap::new();
  574. let config = Config::default();
  575. let context = RenderContext::new(
  576. &ZOLA_TERA,
  577. &config,
  578. "https://vincent.is/about/",
  579. &permalinks_ctx,
  580. InsertAnchor::None,
  581. );
  582. let res = render_content("[an image](image.jpg)", &context).unwrap();
  583. assert_eq!(res.body, "<p><a href=\"https://vincent.is/about/image.jpg\">an image</a></p>\n");
  584. }
  585. #[test]
  586. fn can_make_permalinks_with_colocated_assets_for_image() {
  587. let permalinks_ctx = HashMap::new();
  588. let config = Config::default();
  589. let context = RenderContext::new(
  590. &ZOLA_TERA,
  591. &config,
  592. "https://vincent.is/about/",
  593. &permalinks_ctx,
  594. InsertAnchor::None,
  595. );
  596. let res = render_content("![alt text](image.jpg)", &context).unwrap();
  597. assert_eq!(
  598. res.body,
  599. "<p><img src=\"https://vincent.is/about/image.jpg\" alt=\"alt text\" /></p>\n"
  600. );
  601. }
  602. #[test]
  603. fn markdown_doesnt_wrap_html_in_paragraph() {
  604. let permalinks_ctx = HashMap::new();
  605. let config = Config::default();
  606. let context = RenderContext::new(
  607. &ZOLA_TERA,
  608. &config,
  609. "https://vincent.is/about/",
  610. &permalinks_ctx,
  611. InsertAnchor::None,
  612. );
  613. let res = render_content(
  614. r#"
  615. Some text
  616. <h1>Helo</h1>
  617. <div>
  618. <a href="mobx-flow.png">
  619. <img src="mobx-flow.png" alt="MobX flow">
  620. </a>
  621. </div>
  622. "#,
  623. &context,
  624. )
  625. .unwrap();
  626. assert_eq!(
  627. res.body,
  628. "<p>Some text</p>\n<h1>Helo</h1>\n<div>\n<a href=\"mobx-flow.png\">\n <img src=\"mobx-flow.png\" alt=\"MobX flow\">\n </a>\n</div>\n"
  629. );
  630. }
  631. #[test]
  632. fn correctly_captures_external_links() {
  633. let permalinks_ctx = HashMap::new();
  634. let config = Config::default();
  635. let context = RenderContext::new(
  636. &ZOLA_TERA,
  637. &config,
  638. "https://vincent.is/about/",
  639. &permalinks_ctx,
  640. InsertAnchor::None,
  641. );
  642. let content = "
  643. [a link](http://google.com)
  644. [a link](http://google.comy)
  645. Email: [foo@bar.baz](mailto:foo@bar.baz)
  646. Email: <foo@bar.baz>
  647. ";
  648. let res = render_content(content, &context).unwrap();
  649. assert_eq!(
  650. res.external_links,
  651. &["http://google.com".to_owned(), "http://google.comy".to_owned()]
  652. );
  653. }
  654. #[test]
  655. fn can_handle_summaries() {
  656. let tera_ctx = Tera::default();
  657. let permalinks_ctx = HashMap::new();
  658. let config = Config::default();
  659. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  660. let res = render_content(
  661. r#"
  662. Hello [My site][world]
  663. <!-- more -->
  664. Bla bla
  665. [world]: https://vincentprouillet.com
  666. "#,
  667. &context,
  668. )
  669. .unwrap();
  670. assert_eq!(
  671. res.body,
  672. "<p>Hello <a href=\"https://vincentprouillet.com\">My site</a></p>\n<span id=\"continue-reading\"></span>\n<p>Bla bla</p>\n"
  673. );
  674. assert_eq!(
  675. res.summary_len,
  676. Some("<p>Hello <a href=\"https://vincentprouillet.com/\">My site</a></p>".len())
  677. );
  678. }
  679. // https://github.com/Keats/gutenberg/issues/522
  680. #[test]
  681. fn doesnt_try_to_highlight_content_from_shortcode() {
  682. let permalinks_ctx = HashMap::new();
  683. let mut tera = Tera::default();
  684. tera.extend(&ZOLA_TERA).unwrap();
  685. let shortcode = r#"
  686. <figure>
  687. {% if width %}
  688. <img src="/images/{{ src }}" alt="{{ caption }}" width="{{ width }}" />
  689. {% else %}
  690. <img src="/images/{{ src }}" alt="{{ caption }}" />
  691. {% endif %}
  692. <figcaption>{{ caption }}</figcaption>
  693. </figure>"#;
  694. let markdown_string = r#"{{ figure(src="spherecluster.png", caption="Some spheres.") }}"#;
  695. let expected = r#"<figure>
  696. <img src="/images/spherecluster.png" alt="Some spheres." />
  697. <figcaption>Some spheres.</figcaption>
  698. </figure>"#;
  699. tera.add_raw_template(&format!("shortcodes/{}.html", "figure"), shortcode).unwrap();
  700. let config = Config::default();
  701. let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
  702. let res = render_content(markdown_string, &context).unwrap();
  703. assert_eq!(res.body, expected);
  704. }
  705. // TODO: re-enable once it's fixed in Tera
  706. // https://github.com/Keats/tera/issues/373
  707. //#[test]
  708. //fn can_split_lines_shortcode_body() {
  709. // let permalinks_ctx = HashMap::new();
  710. // let mut tera = Tera::default();
  711. // tera.extend(&ZOLA_TERA).unwrap();
  712. //
  713. // let shortcode = r#"{{ body | split(pat="\n") }}"#;
  714. //
  715. // let markdown_string = r#"
  716. //{% alert() %}
  717. //multi
  718. //ple
  719. //lines
  720. //{% end %}
  721. // "#;
  722. //
  723. // let expected = r#"<p>["multi", "ple", "lines"]</p>"#;
  724. //
  725. // tera.add_raw_template(&format!("shortcodes/{}.html", "alert"), shortcode).unwrap();
  726. // let config = Config::default();
  727. // let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
  728. //
  729. // let res = render_content(markdown_string, &context).unwrap();
  730. // assert_eq!(res.body, expected);
  731. //}
  732. // https://github.com/getzola/zola/issues/747
  733. // https://github.com/getzola/zola/issues/816
  734. #[test]
  735. fn leaves_custom_url_scheme_untouched() {
  736. let content = r#"[foo@bar.tld](xmpp:foo@bar.tld)
  737. [(123) 456-7890](tel:+11234567890)
  738. [blank page](about:blank)
  739. "#;
  740. let tera_ctx = Tera::default();
  741. let config = Config::default();
  742. let permalinks_ctx = HashMap::new();
  743. let context = RenderContext::new(
  744. &tera_ctx,
  745. &config,
  746. "https://vincent.is/",
  747. &permalinks_ctx,
  748. InsertAnchor::None,
  749. );
  750. let res = render_content(content, &context).unwrap();
  751. let expected = r#"<p><a href="xmpp:foo@bar.tld">foo@bar.tld</a></p>
  752. <p><a href="tel:+11234567890">(123) 456-7890</a></p>
  753. <p><a href="about:blank">blank page</a></p>
  754. "#;
  755. assert_eq!(res.body, expected);
  756. }
  757. #[test]
  758. fn stops_with_an_error_on_an_empty_link() {
  759. let content = r#"[some link]()"#;
  760. let tera_ctx = Tera::default();
  761. let config = Config::default();
  762. let permalinks_ctx = HashMap::new();
  763. let context = RenderContext::new(
  764. &tera_ctx,
  765. &config,
  766. "https://vincent.is/",
  767. &permalinks_ctx,
  768. InsertAnchor::None,
  769. );
  770. let res = render_content(content, &context);
  771. let expected = "There is a link that is missing a URL";
  772. assert!(res.is_err());
  773. assert_eq!(res.unwrap_err().to_string(), expected);
  774. }