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.

markdown.rs 22KB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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!(
  114. res.body.contains(&format!(r#"<iframe src="https://www.youtube.com/embed/{}""#, i))
  115. );
  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!(
  207. res.body.contains(r#"<iframe src="https://www.youtube.com/embed/ub36ffWAqgQ?autoplay=1""#)
  208. );
  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_make_valid_relative_link_in_header() {
  457. let mut permalinks = HashMap::new();
  458. permalinks.insert("pages/about.md".to_string(), "https://vincent.is/about/".to_string());
  459. let tera_ctx = Tera::default();
  460. let config = Config::default();
  461. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks, InsertAnchor::None);
  462. let res = render_content(r#" # [rel link](./pages/about.md)"#, &context).unwrap();
  463. assert_eq!(
  464. res.body,
  465. "<h1 id=\"rel-link\"><a href=\"https://vincent.is/about/\">rel link</a></h1>\n"
  466. );
  467. }
  468. #[test]
  469. fn can_make_permalinks_with_colocated_assets_for_link() {
  470. let permalinks_ctx = HashMap::new();
  471. let config = Config::default();
  472. let context = RenderContext::new(
  473. &ZOLA_TERA,
  474. &config,
  475. "https://vincent.is/about/",
  476. &permalinks_ctx,
  477. InsertAnchor::None,
  478. );
  479. let res = render_content("[an image](image.jpg)", &context).unwrap();
  480. assert_eq!(res.body, "<p><a href=\"https://vincent.is/about/image.jpg\">an image</a></p>\n");
  481. }
  482. #[test]
  483. fn can_make_permalinks_with_colocated_assets_for_image() {
  484. let permalinks_ctx = HashMap::new();
  485. let config = Config::default();
  486. let context = RenderContext::new(
  487. &ZOLA_TERA,
  488. &config,
  489. "https://vincent.is/about/",
  490. &permalinks_ctx,
  491. InsertAnchor::None,
  492. );
  493. let res = render_content("![alt text](image.jpg)", &context).unwrap();
  494. assert_eq!(
  495. res.body,
  496. "<p><img src=\"https://vincent.is/about/image.jpg\" alt=\"alt text\" /></p>\n"
  497. );
  498. }
  499. #[test]
  500. fn markdown_doesnt_wrap_html_in_paragraph() {
  501. let permalinks_ctx = HashMap::new();
  502. let config = Config::default();
  503. let context = RenderContext::new(
  504. &ZOLA_TERA,
  505. &config,
  506. "https://vincent.is/about/",
  507. &permalinks_ctx,
  508. InsertAnchor::None,
  509. );
  510. let res = render_content(
  511. r#"
  512. Some text
  513. <h1>Helo</h1>
  514. <div>
  515. <a href="mobx-flow.png">
  516. <img src="mobx-flow.png" alt="MobX flow">
  517. </a>
  518. </div>
  519. "#,
  520. &context,
  521. )
  522. .unwrap();
  523. assert_eq!(
  524. res.body,
  525. "<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"
  526. );
  527. }
  528. #[test]
  529. fn can_validate_valid_external_links() {
  530. let permalinks_ctx = HashMap::new();
  531. let mut config = Config::default();
  532. config.check_external_links = true;
  533. let context = RenderContext::new(
  534. &ZOLA_TERA,
  535. &config,
  536. "https://vincent.is/about/",
  537. &permalinks_ctx,
  538. InsertAnchor::None,
  539. );
  540. let res = render_content("[a link](http://google.com)", &context).unwrap();
  541. assert_eq!(res.body, "<p><a href=\"http://google.com\">a link</a></p>\n");
  542. }
  543. #[test]
  544. fn can_show_error_message_for_invalid_external_links() {
  545. let permalinks_ctx = HashMap::new();
  546. let mut config = Config::default();
  547. config.check_external_links = true;
  548. let context = RenderContext::new(
  549. &ZOLA_TERA,
  550. &config,
  551. "https://vincent.is/about/",
  552. &permalinks_ctx,
  553. InsertAnchor::None,
  554. );
  555. let res = render_content("[a link](http://google.comy)", &context);
  556. assert!(res.is_err());
  557. let err = res.unwrap_err();
  558. assert!(err.description().contains("Link http://google.comy is not valid"));
  559. }
  560. #[test]
  561. fn doesnt_try_to_validate_email_links_mailto() {
  562. let permalinks_ctx = HashMap::new();
  563. let mut config = Config::default();
  564. config.check_external_links = true;
  565. let context = RenderContext::new(
  566. &ZOLA_TERA,
  567. &config,
  568. "https://vincent.is/about/",
  569. &permalinks_ctx,
  570. InsertAnchor::None,
  571. );
  572. let res = render_content("Email: [foo@bar.baz](mailto:foo@bar.baz)", &context).unwrap();
  573. assert_eq!(res.body, "<p>Email: <a href=\"mailto:foo@bar.baz\">foo@bar.baz</a></p>\n");
  574. }
  575. #[test]
  576. fn doesnt_try_to_validate_email_links_angled_brackets() {
  577. let permalinks_ctx = HashMap::new();
  578. let mut config = Config::default();
  579. config.check_external_links = true;
  580. let context = RenderContext::new(
  581. &ZOLA_TERA,
  582. &config,
  583. "https://vincent.is/about/",
  584. &permalinks_ctx,
  585. InsertAnchor::None,
  586. );
  587. let res = render_content("Email: <foo@bar.baz>", &context).unwrap();
  588. assert_eq!(res.body, "<p>Email: <a href=\"mailto:foo@bar.baz\">foo@bar.baz</a></p>\n");
  589. }
  590. #[test]
  591. fn can_handle_summaries() {
  592. let tera_ctx = Tera::default();
  593. let permalinks_ctx = HashMap::new();
  594. let config = Config::default();
  595. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  596. let res = render_content(
  597. "Hello [world]\n\n<!-- more -->\n\nBla bla\n\n[world]: https://vincent.is/about/",
  598. &context,
  599. )
  600. .unwrap();
  601. assert_eq!(
  602. res.body,
  603. "<p>Hello <a href=\"https://vincent.is/about/\">world</a></p>\n<p><a name=\"continue-reading\"></a></p>\n<p>Bla bla</p>\n"
  604. );
  605. assert_eq!(
  606. res.summary_len,
  607. Some("<p>Hello <a href=\"https://vincent.is/about/\">world</a></p>\n".len())
  608. );
  609. }