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.

778 lines
25KB

  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</span><span style=\"color:#c0c5ce;\">$ 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_headers() {
  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_headers_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_insert_anchor_left() {
  309. let permalinks_ctx = HashMap::new();
  310. let config = Config::default();
  311. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Left);
  312. let res = render_content("# Hello", &context).unwrap();
  313. assert_eq!(
  314. res.body,
  315. "<h1 id=\"hello\"><a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>\nHello</h1>\n"
  316. );
  317. }
  318. #[test]
  319. fn can_insert_anchor_right() {
  320. let permalinks_ctx = HashMap::new();
  321. let config = Config::default();
  322. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Right);
  323. let res = render_content("# Hello", &context).unwrap();
  324. assert_eq!(
  325. res.body,
  326. "<h1 id=\"hello\">Hello<a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>\n</h1>\n"
  327. );
  328. }
  329. #[test]
  330. fn can_insert_anchor_for_multi_header() {
  331. let permalinks_ctx = HashMap::new();
  332. let config = Config::default();
  333. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Right);
  334. let res = render_content("# Hello\n# World", &context).unwrap();
  335. assert_eq!(
  336. res.body,
  337. "<h1 id=\"hello\">Hello<a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>\n</h1>\n\
  338. <h1 id=\"world\">World<a class=\"zola-anchor\" href=\"#world\" aria-label=\"Anchor link for: world\">🔗</a>\n</h1>\n"
  339. );
  340. }
  341. // See https://github.com/Keats/gutenberg/issues/42
  342. #[test]
  343. fn can_insert_anchor_with_exclamation_mark() {
  344. let permalinks_ctx = HashMap::new();
  345. let config = Config::default();
  346. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Left);
  347. let res = render_content("# Hello!", &context).unwrap();
  348. assert_eq!(
  349. res.body,
  350. "<h1 id=\"hello\"><a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>\nHello!</h1>\n"
  351. );
  352. }
  353. // See https://github.com/Keats/gutenberg/issues/53
  354. #[test]
  355. fn can_insert_anchor_with_link() {
  356. let permalinks_ctx = HashMap::new();
  357. let config = Config::default();
  358. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Left);
  359. let res = render_content("## [Rust](https://rust-lang.org)", &context).unwrap();
  360. assert_eq!(
  361. res.body,
  362. "<h2 id=\"rust\"><a class=\"zola-anchor\" href=\"#rust\" aria-label=\"Anchor link for: rust\">🔗</a>\n<a href=\"https://rust-lang.org\">Rust</a></h2>\n"
  363. );
  364. }
  365. #[test]
  366. fn can_insert_anchor_with_other_special_chars() {
  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>\nHello*_()</h1>\n"
  374. );
  375. }
  376. #[test]
  377. fn can_make_toc() {
  378. let permalinks_ctx = HashMap::new();
  379. let config = Config::default();
  380. let context = RenderContext::new(
  381. &ZOLA_TERA,
  382. &config,
  383. "https://mysite.com/something",
  384. &permalinks_ctx,
  385. InsertAnchor::Left,
  386. );
  387. let res = render_content(
  388. r#"
  389. # Header 1
  390. ## Header 2
  391. ## Another Header 2
  392. ### Last one
  393. "#,
  394. &context,
  395. )
  396. .unwrap();
  397. let toc = res.toc;
  398. assert_eq!(toc.len(), 1);
  399. assert_eq!(toc[0].children.len(), 2);
  400. assert_eq!(toc[0].children[1].children.len(), 1);
  401. }
  402. #[test]
  403. fn can_ignore_tags_in_toc() {
  404. let permalinks_ctx = HashMap::new();
  405. let config = Config::default();
  406. let context = RenderContext::new(
  407. &ZOLA_TERA,
  408. &config,
  409. "https://mysite.com/something",
  410. &permalinks_ctx,
  411. InsertAnchor::Left,
  412. );
  413. let res = render_content(
  414. r#"
  415. ## header with `code`
  416. ## [anchor](https://duckduckgo.com/) in header
  417. ## **bold** and *italics*
  418. "#,
  419. &context,
  420. )
  421. .unwrap();
  422. let toc = res.toc;
  423. assert_eq!(toc[0].id, "header-with-code");
  424. assert_eq!(toc[0].title, "header with code");
  425. assert_eq!(toc[1].id, "anchor-in-header");
  426. assert_eq!(toc[1].title, "anchor in header");
  427. assert_eq!(toc[2].id, "bold-and-italics");
  428. assert_eq!(toc[2].title, "bold and italics");
  429. }
  430. #[test]
  431. fn can_understand_backtick_in_titles() {
  432. let permalinks_ctx = HashMap::new();
  433. let config = Config::default();
  434. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  435. let res = render_content("# `Hello`", &context).unwrap();
  436. assert_eq!(res.body, "<h1 id=\"hello\"><code>Hello</code></h1>\n");
  437. }
  438. #[test]
  439. fn can_understand_backtick_in_paragraphs() {
  440. let permalinks_ctx = HashMap::new();
  441. let config = Config::default();
  442. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  443. let res = render_content("Hello `world`", &context).unwrap();
  444. assert_eq!(res.body, "<p>Hello <code>world</code></p>\n");
  445. }
  446. // https://github.com/Keats/gutenberg/issues/297
  447. #[test]
  448. fn can_understand_links_in_header() {
  449. let permalinks_ctx = HashMap::new();
  450. let config = Config::default();
  451. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  452. let res = render_content("# [Rust](https://rust-lang.org)", &context).unwrap();
  453. assert_eq!(res.body, "<h1 id=\"rust\"><a href=\"https://rust-lang.org\">Rust</a></h1>\n");
  454. }
  455. #[test]
  456. fn can_understand_link_with_title_in_header() {
  457. let permalinks_ctx = HashMap::new();
  458. let config = Config::default();
  459. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  460. let res =
  461. render_content("# [Rust](https://rust-lang.org \"Rust homepage\")", &context).unwrap();
  462. assert_eq!(
  463. res.body,
  464. "<h1 id=\"rust\"><a href=\"https://rust-lang.org\" title=\"Rust homepage\">Rust</a></h1>\n"
  465. );
  466. }
  467. #[test]
  468. fn can_understand_emphasis_in_header() {
  469. let permalinks_ctx = HashMap::new();
  470. let config = Config::default();
  471. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  472. let res = render_content("# *Emphasis* text", &context).unwrap();
  473. assert_eq!(res.body, "<h1 id=\"emphasis-text\"><em>Emphasis</em> text</h1>\n");
  474. }
  475. #[test]
  476. fn can_understand_strong_in_header() {
  477. let permalinks_ctx = HashMap::new();
  478. let config = Config::default();
  479. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  480. let res = render_content("# **Strong** text", &context).unwrap();
  481. assert_eq!(res.body, "<h1 id=\"strong-text\"><strong>Strong</strong> text</h1>\n");
  482. }
  483. #[test]
  484. fn can_understand_code_in_header() {
  485. let permalinks_ctx = HashMap::new();
  486. let config = Config::default();
  487. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  488. let res = render_content("# `Code` text", &context).unwrap();
  489. assert_eq!(res.body, "<h1 id=\"code-text\"><code>Code</code> text</h1>\n");
  490. }
  491. // See https://github.com/getzola/zola/issues/569
  492. #[test]
  493. fn can_understand_footnote_in_header() {
  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("# text [^1] there\n[^1]: footnote", &context).unwrap();
  498. assert_eq!(res.body, r##"<h1 id="text-there">text <sup class="footnote-reference"><a href="#1">1</a></sup> there</h1>
  499. <div class="footnote-definition" id="1"><sup class="footnote-definition-label">1</sup>
  500. <p>footnote</p>
  501. </div>
  502. "##);
  503. }
  504. #[test]
  505. fn can_make_valid_relative_link_in_header() {
  506. let mut permalinks = HashMap::new();
  507. permalinks.insert("pages/about.md".to_string(), "https://vincent.is/about/".to_string());
  508. let tera_ctx = Tera::default();
  509. let config = Config::default();
  510. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks, InsertAnchor::None);
  511. let res = render_content(r#" # [rel link](./pages/about.md)"#, &context).unwrap();
  512. assert_eq!(
  513. res.body,
  514. "<h1 id=\"rel-link\"><a href=\"https://vincent.is/about/\">rel link</a></h1>\n"
  515. );
  516. }
  517. #[test]
  518. fn can_make_permalinks_with_colocated_assets_for_link() {
  519. let permalinks_ctx = HashMap::new();
  520. let config = Config::default();
  521. let context = RenderContext::new(
  522. &ZOLA_TERA,
  523. &config,
  524. "https://vincent.is/about/",
  525. &permalinks_ctx,
  526. InsertAnchor::None,
  527. );
  528. let res = render_content("[an image](image.jpg)", &context).unwrap();
  529. assert_eq!(res.body, "<p><a href=\"https://vincent.is/about/image.jpg\">an image</a></p>\n");
  530. }
  531. #[test]
  532. fn can_make_permalinks_with_colocated_assets_for_image() {
  533. let permalinks_ctx = HashMap::new();
  534. let config = Config::default();
  535. let context = RenderContext::new(
  536. &ZOLA_TERA,
  537. &config,
  538. "https://vincent.is/about/",
  539. &permalinks_ctx,
  540. InsertAnchor::None,
  541. );
  542. let res = render_content("![alt text](image.jpg)", &context).unwrap();
  543. assert_eq!(
  544. res.body,
  545. "<p><img src=\"https://vincent.is/about/image.jpg\" alt=\"alt text\" /></p>\n"
  546. );
  547. }
  548. #[test]
  549. fn markdown_doesnt_wrap_html_in_paragraph() {
  550. let permalinks_ctx = HashMap::new();
  551. let config = Config::default();
  552. let context = RenderContext::new(
  553. &ZOLA_TERA,
  554. &config,
  555. "https://vincent.is/about/",
  556. &permalinks_ctx,
  557. InsertAnchor::None,
  558. );
  559. let res = render_content(
  560. r#"
  561. Some text
  562. <h1>Helo</h1>
  563. <div>
  564. <a href="mobx-flow.png">
  565. <img src="mobx-flow.png" alt="MobX flow">
  566. </a>
  567. </div>
  568. "#,
  569. &context,
  570. )
  571. .unwrap();
  572. assert_eq!(
  573. res.body,
  574. "<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"
  575. );
  576. }
  577. #[test]
  578. fn can_validate_valid_external_links() {
  579. let permalinks_ctx = HashMap::new();
  580. let mut config = Config::default();
  581. config.check_external_links = true;
  582. let context = RenderContext::new(
  583. &ZOLA_TERA,
  584. &config,
  585. "https://vincent.is/about/",
  586. &permalinks_ctx,
  587. InsertAnchor::None,
  588. );
  589. let res = render_content("[a link](http://google.com)", &context).unwrap();
  590. assert_eq!(res.body, "<p><a href=\"http://google.com\">a link</a></p>\n");
  591. }
  592. #[test]
  593. fn can_show_error_message_for_invalid_external_links() {
  594. let permalinks_ctx = HashMap::new();
  595. let mut config = Config::default();
  596. config.check_external_links = true;
  597. let context = RenderContext::new(
  598. &ZOLA_TERA,
  599. &config,
  600. "https://vincent.is/about/",
  601. &permalinks_ctx,
  602. InsertAnchor::None,
  603. );
  604. let res = render_content("[a link](http://google.comy)", &context);
  605. assert!(res.is_err());
  606. let err = res.unwrap_err();
  607. assert!(err.description().contains("Link http://google.comy is not valid"));
  608. }
  609. #[test]
  610. fn doesnt_try_to_validate_email_links_mailto() {
  611. let permalinks_ctx = HashMap::new();
  612. let mut config = Config::default();
  613. config.check_external_links = true;
  614. let context = RenderContext::new(
  615. &ZOLA_TERA,
  616. &config,
  617. "https://vincent.is/about/",
  618. &permalinks_ctx,
  619. InsertAnchor::None,
  620. );
  621. let res = render_content("Email: [foo@bar.baz](mailto:foo@bar.baz)", &context).unwrap();
  622. assert_eq!(res.body, "<p>Email: <a href=\"mailto:foo@bar.baz\">foo@bar.baz</a></p>\n");
  623. }
  624. #[test]
  625. fn doesnt_try_to_validate_email_links_angled_brackets() {
  626. let permalinks_ctx = HashMap::new();
  627. let mut config = Config::default();
  628. config.check_external_links = true;
  629. let context = RenderContext::new(
  630. &ZOLA_TERA,
  631. &config,
  632. "https://vincent.is/about/",
  633. &permalinks_ctx,
  634. InsertAnchor::None,
  635. );
  636. let res = render_content("Email: <foo@bar.baz>", &context).unwrap();
  637. assert_eq!(res.body, "<p>Email: <a href=\"mailto:foo@bar.baz\">foo@bar.baz</a></p>\n");
  638. }
  639. #[test]
  640. fn can_handle_summaries() {
  641. let tera_ctx = Tera::default();
  642. let permalinks_ctx = HashMap::new();
  643. let config = Config::default();
  644. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  645. let res = render_content(
  646. "Hello [world]\n\n<!-- more -->\n\nBla bla\n\n[world]: https://vincent.is/about/",
  647. &context,
  648. )
  649. .unwrap();
  650. assert_eq!(
  651. res.body,
  652. "<p>Hello <a href=\"https://vincent.is/about/\">world</a></p>\n<p id=\"zola-continue-reading\"><a name=\"continue-reading\"></a></p>\n<p>Bla bla</p>\n"
  653. );
  654. assert_eq!(
  655. res.summary_len,
  656. Some("<p>Hello <a href=\"https://vincent.is/about/\">world</a></p>\n".len())
  657. );
  658. }
  659. // https://github.com/Keats/gutenberg/issues/522
  660. #[test]
  661. fn doesnt_try_to_highlight_content_from_shortcode() {
  662. let permalinks_ctx = HashMap::new();
  663. let mut tera = Tera::default();
  664. tera.extend(&ZOLA_TERA).unwrap();
  665. let shortcode = r#"
  666. <figure>
  667. {% if width %}
  668. <img src="/images/{{ src }}" alt="{{ caption }}" width="{{ width }}" />
  669. {% else %}
  670. <img src="/images/{{ src }}" alt="{{ caption }}" />
  671. {% endif %}
  672. <figcaption>{{ caption }}</figcaption>
  673. </figure>"#;
  674. let markdown_string = r#"{{ figure(src="spherecluster.png", caption="Some spheres.") }}"#;
  675. let expected = r#"<figure>
  676. <img src="/images/spherecluster.png" alt="Some spheres." />
  677. <figcaption>Some spheres.</figcaption>
  678. </figure>"#;
  679. tera.add_raw_template(&format!("shortcodes/{}.html", "figure"), shortcode).unwrap();
  680. let config = Config::default();
  681. let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
  682. let res = render_content(markdown_string, &context).unwrap();
  683. assert_eq!(res.body, expected);
  684. }