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.

833 lines
27KB

  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_handle_manual_ids_on_headings() {
  309. let tera_ctx = Tera::default();
  310. let permalinks_ctx = HashMap::new();
  311. let config = Config::default();
  312. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  313. // Tested things: manual IDs; whitespace flexibility; that automatic IDs avoid collision with
  314. // manual IDs; that duplicates are in fact permitted among manual IDs; that any non-plain-text
  315. // in the middle of `{#…}` will disrupt it from being acknowledged as a manual ID (that last
  316. // one could reasonably be considered a bug rather than a feature, but test it either way); one
  317. // workaround for the improbable case where you actually want `{#…}` at the end of a heading.
  318. let res = render_content(
  319. "\
  320. # Hello\n\
  321. # Hello{#hello}\n\
  322. # Hello {#hello}\n\
  323. # Hello {#Something_else} \n\
  324. # Workaround for literal {#…&#125;\n\
  325. # Hello\n\
  326. # Auto {#*matic*}",
  327. &context,
  328. )
  329. .unwrap();
  330. assert_eq!(
  331. res.body,
  332. "\
  333. <h1 id=\"hello-1\">Hello</h1>\n\
  334. <h1 id=\"hello\">Hello</h1>\n\
  335. <h1 id=\"hello\">Hello</h1>\n\
  336. <h1 id=\"Something_else\">Hello</h1>\n\
  337. <h1 id=\"workaround-for-literal\">Workaround for literal {#…}</h1>\n\
  338. <h1 id=\"hello-2\">Hello</h1>\n\
  339. <h1 id=\"auto-matic\">Auto {#<em>matic</em>}</h1>\n\
  340. "
  341. );
  342. }
  343. #[test]
  344. fn blank_headings() {
  345. let tera_ctx = Tera::default();
  346. let permalinks_ctx = HashMap::new();
  347. let config = Config::default();
  348. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  349. let res = render_content("# \n#\n# {#hmm} \n# {#}", &context).unwrap();
  350. assert_eq!(
  351. res.body,
  352. "<h1 id=\"-1\"></h1>\n<h1 id=\"-2\"></h1>\n<h1 id=\"hmm\"></h1>\n<h1 id=\"\"></h1>\n"
  353. );
  354. }
  355. #[test]
  356. fn can_insert_anchor_left() {
  357. let permalinks_ctx = HashMap::new();
  358. let config = Config::default();
  359. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Left);
  360. let res = render_content("# Hello", &context).unwrap();
  361. assert_eq!(
  362. res.body,
  363. "<h1 id=\"hello\"><a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>Hello</h1>\n"
  364. );
  365. }
  366. #[test]
  367. fn can_insert_anchor_right() {
  368. let permalinks_ctx = HashMap::new();
  369. let config = Config::default();
  370. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Right);
  371. let res = render_content("# Hello", &context).unwrap();
  372. assert_eq!(
  373. res.body,
  374. "<h1 id=\"hello\">Hello<a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a></h1>\n"
  375. );
  376. }
  377. #[test]
  378. fn can_insert_anchor_for_multi_heading() {
  379. let permalinks_ctx = HashMap::new();
  380. let config = Config::default();
  381. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Right);
  382. let res = render_content("# Hello\n# World", &context).unwrap();
  383. assert_eq!(
  384. res.body,
  385. "<h1 id=\"hello\">Hello<a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a></h1>\n\
  386. <h1 id=\"world\">World<a class=\"zola-anchor\" href=\"#world\" aria-label=\"Anchor link for: world\">🔗</a></h1>\n"
  387. );
  388. }
  389. // See https://github.com/Keats/gutenberg/issues/42
  390. #[test]
  391. fn can_insert_anchor_with_exclamation_mark() {
  392. let permalinks_ctx = HashMap::new();
  393. let config = Config::default();
  394. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Left);
  395. let res = render_content("# Hello!", &context).unwrap();
  396. assert_eq!(
  397. res.body,
  398. "<h1 id=\"hello\"><a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>Hello!</h1>\n"
  399. );
  400. }
  401. // See https://github.com/Keats/gutenberg/issues/53
  402. #[test]
  403. fn can_insert_anchor_with_link() {
  404. let permalinks_ctx = HashMap::new();
  405. let config = Config::default();
  406. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Left);
  407. let res = render_content("## [Rust](https://rust-lang.org)", &context).unwrap();
  408. assert_eq!(
  409. res.body,
  410. "<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"
  411. );
  412. }
  413. #[test]
  414. fn can_insert_anchor_with_other_special_chars() {
  415. let permalinks_ctx = HashMap::new();
  416. let config = Config::default();
  417. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Left);
  418. let res = render_content("# Hello*_()", &context).unwrap();
  419. assert_eq!(
  420. res.body,
  421. "<h1 id=\"hello\"><a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>Hello*_()</h1>\n"
  422. );
  423. }
  424. #[test]
  425. fn can_make_toc() {
  426. let permalinks_ctx = HashMap::new();
  427. let config = Config::default();
  428. let context = RenderContext::new(
  429. &ZOLA_TERA,
  430. &config,
  431. "https://mysite.com/something",
  432. &permalinks_ctx,
  433. InsertAnchor::Left,
  434. );
  435. let res = render_content(
  436. r#"
  437. # Heading 1
  438. ## Heading 2
  439. ## Another Heading 2
  440. ### Last one
  441. "#,
  442. &context,
  443. )
  444. .unwrap();
  445. let toc = res.toc;
  446. assert_eq!(toc.len(), 1);
  447. assert_eq!(toc[0].children.len(), 2);
  448. assert_eq!(toc[0].children[1].children.len(), 1);
  449. }
  450. #[test]
  451. fn can_ignore_tags_in_toc() {
  452. let permalinks_ctx = HashMap::new();
  453. let config = Config::default();
  454. let context = RenderContext::new(
  455. &ZOLA_TERA,
  456. &config,
  457. "https://mysite.com/something",
  458. &permalinks_ctx,
  459. InsertAnchor::Left,
  460. );
  461. let res = render_content(
  462. r#"
  463. ## heading with `code`
  464. ## [anchor](https://duckduckgo.com/) in heading
  465. ## **bold** and *italics*
  466. "#,
  467. &context,
  468. )
  469. .unwrap();
  470. let toc = res.toc;
  471. assert_eq!(toc[0].id, "heading-with-code");
  472. assert_eq!(toc[0].title, "heading with code");
  473. assert_eq!(toc[1].id, "anchor-in-heading");
  474. assert_eq!(toc[1].title, "anchor in heading");
  475. assert_eq!(toc[2].id, "bold-and-italics");
  476. assert_eq!(toc[2].title, "bold and italics");
  477. }
  478. #[test]
  479. fn can_understand_backtick_in_titles() {
  480. let permalinks_ctx = HashMap::new();
  481. let config = Config::default();
  482. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  483. let res = render_content("# `Hello`", &context).unwrap();
  484. assert_eq!(res.body, "<h1 id=\"hello\"><code>Hello</code></h1>\n");
  485. }
  486. #[test]
  487. fn can_understand_backtick_in_paragraphs() {
  488. let permalinks_ctx = HashMap::new();
  489. let config = Config::default();
  490. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  491. let res = render_content("Hello `world`", &context).unwrap();
  492. assert_eq!(res.body, "<p>Hello <code>world</code></p>\n");
  493. }
  494. // https://github.com/Keats/gutenberg/issues/297
  495. #[test]
  496. fn can_understand_links_in_heading() {
  497. let permalinks_ctx = HashMap::new();
  498. let config = Config::default();
  499. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  500. let res = render_content("# [Rust](https://rust-lang.org)", &context).unwrap();
  501. assert_eq!(res.body, "<h1 id=\"rust\"><a href=\"https://rust-lang.org\">Rust</a></h1>\n");
  502. }
  503. #[test]
  504. fn can_understand_link_with_title_in_heading() {
  505. let permalinks_ctx = HashMap::new();
  506. let config = Config::default();
  507. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  508. let res =
  509. render_content("# [Rust](https://rust-lang.org \"Rust homepage\")", &context).unwrap();
  510. assert_eq!(
  511. res.body,
  512. "<h1 id=\"rust\"><a href=\"https://rust-lang.org\" title=\"Rust homepage\">Rust</a></h1>\n"
  513. );
  514. }
  515. #[test]
  516. fn can_understand_emphasis_in_heading() {
  517. let permalinks_ctx = HashMap::new();
  518. let config = Config::default();
  519. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  520. let res = render_content("# *Emphasis* text", &context).unwrap();
  521. assert_eq!(res.body, "<h1 id=\"emphasis-text\"><em>Emphasis</em> text</h1>\n");
  522. }
  523. #[test]
  524. fn can_understand_strong_in_heading() {
  525. let permalinks_ctx = HashMap::new();
  526. let config = Config::default();
  527. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  528. let res = render_content("# **Strong** text", &context).unwrap();
  529. assert_eq!(res.body, "<h1 id=\"strong-text\"><strong>Strong</strong> text</h1>\n");
  530. }
  531. #[test]
  532. fn can_understand_code_in_heading() {
  533. let permalinks_ctx = HashMap::new();
  534. let config = Config::default();
  535. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  536. let res = render_content("# `Code` text", &context).unwrap();
  537. assert_eq!(res.body, "<h1 id=\"code-text\"><code>Code</code> text</h1>\n");
  538. }
  539. // See https://github.com/getzola/zola/issues/569
  540. #[test]
  541. fn can_understand_footnote_in_heading() {
  542. let permalinks_ctx = HashMap::new();
  543. let config = Config::default();
  544. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  545. let res = render_content("# text [^1] there\n[^1]: footnote", &context).unwrap();
  546. assert_eq!(res.body, r##"<h1 id="text-there">text <sup class="footnote-reference"><a href="#1">1</a></sup> there</h1>
  547. <div class="footnote-definition" id="1"><sup class="footnote-definition-label">1</sup>
  548. <p>footnote</p>
  549. </div>
  550. "##);
  551. }
  552. #[test]
  553. fn can_make_valid_relative_link_in_heading() {
  554. let mut permalinks = HashMap::new();
  555. permalinks.insert("pages/about.md".to_string(), "https://vincent.is/about/".to_string());
  556. let tera_ctx = Tera::default();
  557. let config = Config::default();
  558. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks, InsertAnchor::None);
  559. let res = render_content(r#" # [rel link](@/pages/about.md)"#, &context).unwrap();
  560. assert_eq!(
  561. res.body,
  562. "<h1 id=\"rel-link\"><a href=\"https://vincent.is/about/\">rel link</a></h1>\n"
  563. );
  564. }
  565. #[test]
  566. fn can_make_permalinks_with_colocated_assets_for_link() {
  567. let permalinks_ctx = HashMap::new();
  568. let config = Config::default();
  569. let context = RenderContext::new(
  570. &ZOLA_TERA,
  571. &config,
  572. "https://vincent.is/about/",
  573. &permalinks_ctx,
  574. InsertAnchor::None,
  575. );
  576. let res = render_content("[an image](image.jpg)", &context).unwrap();
  577. assert_eq!(res.body, "<p><a href=\"https://vincent.is/about/image.jpg\">an image</a></p>\n");
  578. }
  579. #[test]
  580. fn can_make_permalinks_with_colocated_assets_for_image() {
  581. let permalinks_ctx = HashMap::new();
  582. let config = Config::default();
  583. let context = RenderContext::new(
  584. &ZOLA_TERA,
  585. &config,
  586. "https://vincent.is/about/",
  587. &permalinks_ctx,
  588. InsertAnchor::None,
  589. );
  590. let res = render_content("![alt text](image.jpg)", &context).unwrap();
  591. assert_eq!(
  592. res.body,
  593. "<p><img src=\"https://vincent.is/about/image.jpg\" alt=\"alt text\" /></p>\n"
  594. );
  595. }
  596. #[test]
  597. fn markdown_doesnt_wrap_html_in_paragraph() {
  598. let permalinks_ctx = HashMap::new();
  599. let config = Config::default();
  600. let context = RenderContext::new(
  601. &ZOLA_TERA,
  602. &config,
  603. "https://vincent.is/about/",
  604. &permalinks_ctx,
  605. InsertAnchor::None,
  606. );
  607. let res = render_content(
  608. r#"
  609. Some text
  610. <h1>Helo</h1>
  611. <div>
  612. <a href="mobx-flow.png">
  613. <img src="mobx-flow.png" alt="MobX flow">
  614. </a>
  615. </div>
  616. "#,
  617. &context,
  618. )
  619. .unwrap();
  620. assert_eq!(
  621. res.body,
  622. "<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"
  623. );
  624. }
  625. #[test]
  626. fn correctly_captures_external_links() {
  627. let permalinks_ctx = HashMap::new();
  628. let config = Config::default();
  629. let context = RenderContext::new(
  630. &ZOLA_TERA,
  631. &config,
  632. "https://vincent.is/about/",
  633. &permalinks_ctx,
  634. InsertAnchor::None,
  635. );
  636. let content = "
  637. [a link](http://google.com)
  638. [a link](http://google.comy)
  639. Email: [foo@bar.baz](mailto:foo@bar.baz)
  640. Email: <foo@bar.baz>
  641. ";
  642. let res = render_content(content, &context).unwrap();
  643. assert_eq!(
  644. res.external_links,
  645. &["http://google.com".to_owned(), "http://google.comy".to_owned()]
  646. );
  647. }
  648. #[test]
  649. fn can_handle_summaries() {
  650. let tera_ctx = Tera::default();
  651. let permalinks_ctx = HashMap::new();
  652. let config = Config::default();
  653. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  654. let res = render_content(
  655. r#"
  656. Hello [My site][world]
  657. <!-- more -->
  658. Bla bla
  659. [world]: https://vincentprouillet.com
  660. "#,
  661. &context,
  662. )
  663. .unwrap();
  664. assert_eq!(
  665. res.body,
  666. "<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"
  667. );
  668. assert_eq!(
  669. res.summary_len,
  670. Some("<p>Hello <a href=\"https://vincentprouillet.com/\">My site</a></p>".len())
  671. );
  672. }
  673. // https://github.com/Keats/gutenberg/issues/522
  674. #[test]
  675. fn doesnt_try_to_highlight_content_from_shortcode() {
  676. let permalinks_ctx = HashMap::new();
  677. let mut tera = Tera::default();
  678. tera.extend(&ZOLA_TERA).unwrap();
  679. let shortcode = r#"
  680. <figure>
  681. {% if width %}
  682. <img src="/images/{{ src }}" alt="{{ caption }}" width="{{ width }}" />
  683. {% else %}
  684. <img src="/images/{{ src }}" alt="{{ caption }}" />
  685. {% endif %}
  686. <figcaption>{{ caption }}</figcaption>
  687. </figure>"#;
  688. let markdown_string = r#"{{ figure(src="spherecluster.png", caption="Some spheres.") }}"#;
  689. let expected = r#"<figure>
  690. <img src="/images/spherecluster.png" alt="Some spheres." />
  691. <figcaption>Some spheres.</figcaption>
  692. </figure>"#;
  693. tera.add_raw_template(&format!("shortcodes/{}.html", "figure"), shortcode).unwrap();
  694. let config = Config::default();
  695. let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
  696. let res = render_content(markdown_string, &context).unwrap();
  697. assert_eq!(res.body, expected);
  698. }
  699. // TODO: re-enable once it's fixed in Tera
  700. // https://github.com/Keats/tera/issues/373
  701. //#[test]
  702. //fn can_split_lines_shortcode_body() {
  703. // let permalinks_ctx = HashMap::new();
  704. // let mut tera = Tera::default();
  705. // tera.extend(&ZOLA_TERA).unwrap();
  706. //
  707. // let shortcode = r#"{{ body | split(pat="\n") }}"#;
  708. //
  709. // let markdown_string = r#"
  710. //{% alert() %}
  711. //multi
  712. //ple
  713. //lines
  714. //{% end %}
  715. // "#;
  716. //
  717. // let expected = r#"<p>["multi", "ple", "lines"]</p>"#;
  718. //
  719. // tera.add_raw_template(&format!("shortcodes/{}.html", "alert"), shortcode).unwrap();
  720. // let config = Config::default();
  721. // let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
  722. //
  723. // let res = render_content(markdown_string, &context).unwrap();
  724. // assert_eq!(res.body, expected);
  725. //}
  726. // https://github.com/getzola/zola/issues/747
  727. #[test]
  728. fn leaves_custom_url_scheme_untouched() {
  729. let tera_ctx = Tera::default();
  730. let permalinks_ctx = HashMap::new();
  731. let config = Config::default();
  732. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  733. let res = render_content("[foo@bar.tld](xmpp:foo@bar.tld)", &context).unwrap();
  734. assert_eq!(res.body, "<p><a href=\"xmpp:foo@bar.tld\">foo@bar.tld</a></p>\n");
  735. }