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