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.

866 lines
28KB

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