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.

612 lines
22KB

  1. extern crate tera;
  2. extern crate front_matter;
  3. extern crate templates;
  4. extern crate rendering;
  5. extern crate config;
  6. use std::collections::HashMap;
  7. use std::path::Path;
  8. use tera::Tera;
  9. use config::Config;
  10. use front_matter::InsertAnchor;
  11. use templates::GUTENBERG_TERA;
  12. use rendering::{RenderContext, render_content};
  13. #[test]
  14. fn can_do_render_content_simple() {
  15. let tera_ctx = Tera::default();
  16. let permalinks_ctx = HashMap::new();
  17. let config = Config::default();
  18. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  19. let res = render_content("hello", &context).unwrap();
  20. assert_eq!(res.body, "<p>hello</p>\n");
  21. }
  22. #[test]
  23. fn doesnt_highlight_code_block_with_highlighting_off() {
  24. let tera_ctx = Tera::default();
  25. let permalinks_ctx = HashMap::new();
  26. let mut config = Config::default();
  27. config.highlight_code = false;
  28. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  29. let res = render_content("```\n$ gutenberg server\n```", &context).unwrap();
  30. assert_eq!(
  31. res.body,
  32. "<pre><code>$ gutenberg server\n</code></pre>\n"
  33. );
  34. }
  35. #[test]
  36. fn can_highlight_code_block_no_lang() {
  37. let tera_ctx = Tera::default();
  38. let permalinks_ctx = HashMap::new();
  39. let mut config = Config::default();
  40. config.highlight_code = true;
  41. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  42. let res = render_content("```\n$ gutenberg server\n$ ping\n```", &context).unwrap();
  43. assert_eq!(
  44. res.body,
  45. "<pre style=\"background-color:#2b303b\">\n<span style=\"color:#c0c5ce;\">$ gutenberg server\n</span><span style=\"color:#c0c5ce;\">$ ping\n</span></pre>"
  46. );
  47. }
  48. #[test]
  49. fn can_highlight_code_block_with_lang() {
  50. let tera_ctx = Tera::default();
  51. let permalinks_ctx = HashMap::new();
  52. let mut config = Config::default();
  53. config.highlight_code = true;
  54. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  55. let res = render_content("```python\nlist.append(1)\n```", &context).unwrap();
  56. assert_eq!(
  57. res.body,
  58. "<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>"
  59. );
  60. }
  61. #[test]
  62. fn can_higlight_code_block_with_unknown_lang() {
  63. let tera_ctx = Tera::default();
  64. let permalinks_ctx = HashMap::new();
  65. let mut config = Config::default();
  66. config.highlight_code = true;
  67. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  68. let res = render_content("```yolo\nlist.append(1)\n```", &context).unwrap();
  69. // defaults to plain text
  70. assert_eq!(
  71. res.body,
  72. "<pre style=\"background-color:#2b303b\">\n<span style=\"color:#c0c5ce;\">list.append(1)\n</span></pre>"
  73. );
  74. }
  75. #[test]
  76. fn can_render_shortcode() {
  77. let permalinks_ctx = HashMap::new();
  78. let config = Config::default();
  79. let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  80. let res = render_content(r#"
  81. Hello
  82. {{ youtube(id="ub36ffWAqgQ") }}
  83. "#, &context).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(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  92. let input = vec![
  93. "name",
  94. "na_me",
  95. "n_a_me",
  96. "n1",
  97. ];
  98. for i in input {
  99. let res = render_content(&format!("{{{{ youtube(id=\"hey\", {}=1) }}}}", i), &context).unwrap();
  100. assert!(res.body.contains(r#"<iframe src="https://www.youtube.com/embed/hey""#));
  101. }
  102. }
  103. #[test]
  104. fn can_render_shortcode_with_markdown_char_in_args_value() {
  105. let permalinks_ctx = HashMap::new();
  106. let config = Config::default();
  107. let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  108. let input = vec![
  109. "ub36ffWAqgQ-hey",
  110. "ub36ffWAqgQ_hey",
  111. "ub36ffWAqgQ_he_y",
  112. "ub36ffWAqgQ*hey",
  113. "ub36ffWAqgQ#hey",
  114. ];
  115. for i in input {
  116. let res = render_content(&format!("{{{{ youtube(id=\"{}\") }}}}", i), &context).unwrap();
  117. assert!(res.body.contains(&format!(r#"<iframe src="https://www.youtube.com/embed/{}""#, i)));
  118. }
  119. }
  120. #[test]
  121. fn can_render_body_shortcode_with_markdown_char_in_name() {
  122. let permalinks_ctx = HashMap::new();
  123. let mut tera = Tera::default();
  124. tera.extend(&GUTENBERG_TERA).unwrap();
  125. let input = vec![
  126. "quo_te",
  127. "qu_o_te",
  128. ];
  129. let config = Config::default();
  130. for i in input {
  131. tera.add_raw_template(&format!("shortcodes/{}.html", i), "<blockquote>{{ body }} - {{ author}}</blockquote>").unwrap();
  132. let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  133. let res = render_content(&format!("{{% {}(author=\"Bob\") %}}\nhey\n{{% end %}}", i), &context).unwrap();
  134. println!("{:?}", res);
  135. assert!(res.body.contains("<blockquote>hey - Bob</blockquote>"));
  136. }
  137. }
  138. #[test]
  139. fn can_render_body_shortcode_and_paragraph_after() {
  140. let permalinks_ctx = HashMap::new();
  141. let mut tera = Tera::default();
  142. tera.extend(&GUTENBERG_TERA).unwrap();
  143. let shortcode = "<p>{{ body }}</p>";
  144. let markdown_string = r#"
  145. {% figure() %}
  146. This is a figure caption.
  147. {% end %}
  148. Here is another paragraph.
  149. "#;
  150. let expected = "<p>This is a figure caption.</p>
  151. <p>Here is another paragraph.</p>
  152. ";
  153. tera.add_raw_template(&format!("shortcodes/{}.html", "figure"), shortcode).unwrap();
  154. let config = Config::default();
  155. let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  156. let res = render_content(markdown_string, &context).unwrap();
  157. println!("{:?}", res);
  158. assert_eq!(res.body, expected);
  159. }
  160. #[test]
  161. fn can_render_two_body_shortcode_and_paragraph_after_with_line_break_between() {
  162. let permalinks_ctx = HashMap::new();
  163. let mut tera = Tera::default();
  164. tera.extend(&GUTENBERG_TERA).unwrap();
  165. let shortcode = "<p>{{ body }}</p>";
  166. let markdown_string = r#"
  167. {% figure() %}
  168. This is a figure caption.
  169. {% end %}
  170. {% figure() %}
  171. This is a figure caption.
  172. {% end %}
  173. Here is another paragraph.
  174. "#;
  175. let expected = "<p>This is a figure caption.</p>
  176. <p>This is a figure caption.</p>
  177. <p>Here is another paragraph.</p>
  178. ";
  179. tera.add_raw_template(&format!("shortcodes/{}.html", "figure"), shortcode).unwrap();
  180. let config = Config::default();
  181. let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  182. let res = render_content(markdown_string, &context).unwrap();
  183. println!("{:?}", res);
  184. assert_eq!(res.body, expected);
  185. }
  186. #[test]
  187. fn can_render_several_shortcode_in_row() {
  188. let permalinks_ctx = HashMap::new();
  189. let config = Config::default();
  190. let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  191. let res = render_content(r#"
  192. Hello
  193. {{ youtube(id="ub36ffWAqgQ") }}
  194. {{ youtube(id="ub36ffWAqgQ", autoplay=true) }}
  195. {{ vimeo(id="210073083") }}
  196. {{ streamable(id="c0ic") }}
  197. {{ gist(url="https://gist.github.com/Keats/32d26f699dcc13ebd41b") }}
  198. "#, &context).unwrap();
  199. assert!(res.body.contains("<p>Hello</p>\n<div >"));
  200. assert!(res.body.contains(r#"<iframe src="https://www.youtube.com/embed/ub36ffWAqgQ""#));
  201. assert!(res.body.contains(r#"<iframe src="https://www.youtube.com/embed/ub36ffWAqgQ?autoplay=1""#));
  202. assert!(res.body.contains(r#"<iframe src="https://www.streamable.com/e/c0ic""#));
  203. assert!(res.body.contains(r#"//player.vimeo.com/video/210073083""#));
  204. }
  205. #[test]
  206. fn doesnt_render_ignored_shortcodes() {
  207. let permalinks_ctx = HashMap::new();
  208. let mut config = Config::default();
  209. config.highlight_code = false;
  210. let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  211. let res = render_content(r#"```{{/* youtube(id="w7Ft2ymGmfc") */}}```"#, &context).unwrap();
  212. assert_eq!(res.body, "<p><code>{{ youtube(id=&quot;w7Ft2ymGmfc&quot;) }}</code></p>\n");
  213. }
  214. #[test]
  215. fn can_render_shortcode_with_body() {
  216. let mut tera = Tera::default();
  217. tera.extend(&GUTENBERG_TERA).unwrap();
  218. tera.add_raw_template("shortcodes/quote.html", "<blockquote>{{ body }} - {{ author }}</blockquote>").unwrap();
  219. let permalinks_ctx = HashMap::new();
  220. let config = Config::default();
  221. let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  222. let res = render_content(r#"
  223. Hello
  224. {% quote(author="Keats") %}
  225. A quote
  226. {% end %}
  227. "#, &context).unwrap();
  228. assert_eq!(res.body, "<p>Hello</p>\n<blockquote>A quote - Keats</blockquote>\n");
  229. }
  230. #[test]
  231. fn errors_rendering_unknown_shortcode() {
  232. let tera_ctx = Tera::default();
  233. let permalinks_ctx = HashMap::new();
  234. let config = Config::default();
  235. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  236. let res = render_content("{{ hello(flash=true) }}", &context);
  237. assert!(res.is_err());
  238. }
  239. #[test]
  240. fn can_make_valid_relative_link() {
  241. let mut permalinks = HashMap::new();
  242. permalinks.insert("pages/about.md".to_string(), "https://vincent.is/about".to_string());
  243. let tera_ctx = Tera::default();
  244. let config = Config::default();
  245. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks, Path::new("something"), InsertAnchor::None);
  246. let res = render_content(
  247. r#"[rel link](./pages/about.md), [abs link](https://vincent.is/about)"#,
  248. &context,
  249. ).unwrap();
  250. assert!(
  251. res.body.contains(r#"<p><a href="https://vincent.is/about">rel link</a>, <a href="https://vincent.is/about">abs link</a></p>"#)
  252. );
  253. }
  254. #[test]
  255. fn can_make_relative_links_with_anchors() {
  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, Path::new("something"), InsertAnchor::None);
  261. let res = render_content(r#"[rel link](./pages/about.md#cv)"#, &context).unwrap();
  262. assert!(
  263. res.body.contains(r#"<p><a href="https://vincent.is/about#cv">rel link</a></p>"#)
  264. );
  265. }
  266. #[test]
  267. fn errors_relative_link_inexistant() {
  268. let tera_ctx = Tera::default();
  269. let permalinks_ctx = HashMap::new();
  270. let config = Config::default();
  271. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  272. let res = render_content("[rel link](./pages/about.md)", &context);
  273. assert!(res.is_err());
  274. }
  275. #[test]
  276. fn can_add_id_to_headers() {
  277. let tera_ctx = Tera::default();
  278. let permalinks_ctx = HashMap::new();
  279. let config = Config::default();
  280. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  281. let res = render_content(r#"# Hello"#, &context).unwrap();
  282. assert_eq!(res.body, "<h1 id=\"hello\">Hello</h1>\n");
  283. }
  284. #[test]
  285. fn can_add_id_to_headers_same_slug() {
  286. let tera_ctx = Tera::default();
  287. let permalinks_ctx = HashMap::new();
  288. let config = Config::default();
  289. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  290. let res = render_content("# Hello\n# Hello", &context).unwrap();
  291. assert_eq!(res.body, "<h1 id=\"hello\">Hello</h1>\n<h1 id=\"hello-1\">Hello</h1>\n");
  292. }
  293. #[test]
  294. fn can_insert_anchor_left() {
  295. let permalinks_ctx = HashMap::new();
  296. let config = Config::default();
  297. let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::Left);
  298. let res = render_content("# Hello", &context).unwrap();
  299. assert_eq!(
  300. res.body,
  301. "<h1 id=\"hello\"><a class=\"gutenberg-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>\nHello</h1>\n"
  302. );
  303. }
  304. #[test]
  305. fn can_insert_anchor_right() {
  306. let permalinks_ctx = HashMap::new();
  307. let config = Config::default();
  308. let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::Right);
  309. let res = render_content("# Hello", &context).unwrap();
  310. assert_eq!(
  311. res.body,
  312. "<h1 id=\"hello\">Hello<a class=\"gutenberg-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>\n</h1>\n"
  313. );
  314. }
  315. // See https://github.com/Keats/gutenberg/issues/42
  316. #[test]
  317. fn can_insert_anchor_with_exclamation_mark() {
  318. let permalinks_ctx = HashMap::new();
  319. let config = Config::default();
  320. let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::Left);
  321. let res = render_content("# Hello!", &context).unwrap();
  322. assert_eq!(
  323. res.body,
  324. "<h1 id=\"hello\"><a class=\"gutenberg-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>\nHello!</h1>\n"
  325. );
  326. }
  327. // See https://github.com/Keats/gutenberg/issues/53
  328. #[test]
  329. fn can_insert_anchor_with_link() {
  330. let permalinks_ctx = HashMap::new();
  331. let config = Config::default();
  332. let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::Left);
  333. let res = render_content("## [Rust](https://rust-lang.org)", &context).unwrap();
  334. assert_eq!(
  335. res.body,
  336. "<h2 id=\"rust\"><a class=\"gutenberg-anchor\" href=\"#rust\" aria-label=\"Anchor link for: rust\">🔗</a>\n<a href=\"https://rust-lang.org\">Rust</a></h2>\n"
  337. );
  338. }
  339. #[test]
  340. fn can_insert_anchor_with_other_special_chars() {
  341. let permalinks_ctx = HashMap::new();
  342. let config = Config::default();
  343. let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::Left);
  344. let res = render_content("# Hello*_()", &context).unwrap();
  345. assert_eq!(
  346. res.body,
  347. "<h1 id=\"hello\"><a class=\"gutenberg-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>\nHello*_()</h1>\n"
  348. );
  349. }
  350. #[test]
  351. fn can_make_toc() {
  352. let permalinks_ctx = HashMap::new();
  353. let config = Config::default();
  354. let context = RenderContext::new(
  355. &GUTENBERG_TERA,
  356. &config,
  357. "https://mysite.com/something",
  358. &permalinks_ctx,
  359. Path::new("something"),
  360. InsertAnchor::Left,
  361. );
  362. let res = render_content(r#"
  363. # Header 1
  364. ## Header 2
  365. ## Another Header 2
  366. ### Last one
  367. "#, &context).unwrap();
  368. let toc = res.toc;
  369. assert_eq!(toc.len(), 1);
  370. assert_eq!(toc[0].children.len(), 2);
  371. assert_eq!(toc[0].children[1].children.len(), 1);
  372. }
  373. #[test]
  374. fn can_understand_backtick_in_titles() {
  375. let permalinks_ctx = HashMap::new();
  376. let config = Config::default();
  377. let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  378. let res = render_content("# `Hello`", &context).unwrap();
  379. assert_eq!(
  380. res.body,
  381. "<h1 id=\"hello\"><code>Hello</code></h1>\n"
  382. );
  383. }
  384. #[test]
  385. fn can_understand_backtick_in_paragraphs() {
  386. let permalinks_ctx = HashMap::new();
  387. let config = Config::default();
  388. let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  389. let res = render_content("Hello `world`", &context).unwrap();
  390. assert_eq!(
  391. res.body,
  392. "<p>Hello <code>world</code></p>\n"
  393. );
  394. }
  395. // https://github.com/Keats/gutenberg/issues/297
  396. #[test]
  397. fn can_understand_links_in_header() {
  398. let permalinks_ctx = HashMap::new();
  399. let config = Config::default();
  400. let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  401. let res = render_content("# [Rust](https://rust-lang.org)", &context).unwrap();
  402. assert_eq!(
  403. res.body,
  404. "<h1 id=\"rust\"><a href=\"https://rust-lang.org\">Rust</a></h1>\n"
  405. );
  406. }
  407. #[test]
  408. fn can_understand_link_with_title_in_header() {
  409. let permalinks_ctx = HashMap::new();
  410. let config = Config::default();
  411. let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  412. let res = render_content("# [Rust](https://rust-lang.org \"Rust homepage\")", &context).unwrap();
  413. assert_eq!(
  414. res.body,
  415. "<h1 id=\"rust\"><a href=\"https://rust-lang.org\" title=\"Rust homepage\">Rust</a></h1>\n"
  416. );
  417. }
  418. #[test]
  419. fn can_make_valid_relative_link_in_header() {
  420. let mut permalinks = HashMap::new();
  421. permalinks.insert("pages/about.md".to_string(), "https://vincent.is/about/".to_string());
  422. let tera_ctx = Tera::default();
  423. let config = Config::default();
  424. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks, Path::new("something"), InsertAnchor::None);
  425. let res = render_content(
  426. r#" # [rel link](./pages/about.md)"#,
  427. &context,
  428. ).unwrap();
  429. assert_eq!(
  430. res.body,
  431. "<h1 id=\"rel-link\"><a href=\"https://vincent.is/about/\">rel link</a></h1>\n"
  432. );
  433. }
  434. #[test]
  435. fn can_make_permalinks_with_colocated_assets_for_link() {
  436. let permalinks_ctx = HashMap::new();
  437. let config = Config::default();
  438. let context = RenderContext::new(&GUTENBERG_TERA, &config, "https://vincent.is/about/", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  439. let res = render_content("[an image](image.jpg)", &context).unwrap();
  440. assert_eq!(
  441. res.body,
  442. "<p><a href=\"https://vincent.is/about/image.jpg\">an image</a></p>\n"
  443. );
  444. }
  445. #[test]
  446. fn can_make_permalinks_with_colocated_assets_for_image() {
  447. let permalinks_ctx = HashMap::new();
  448. let config = Config::default();
  449. let context = RenderContext::new(&GUTENBERG_TERA, &config, "https://vincent.is/about/", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  450. let res = render_content("![alt text](image.jpg)", &context).unwrap();
  451. assert_eq!(
  452. res.body,
  453. "<p><img src=\"https://vincent.is/about/image.jpg\" alt=\"alt text\" /></p>\n"
  454. );
  455. }
  456. #[test]
  457. fn markdown_doesnt_wrap_html_in_paragraph() {
  458. let permalinks_ctx = HashMap::new();
  459. let config = Config::default();
  460. let context = RenderContext::new(&GUTENBERG_TERA, &config, "https://vincent.is/about/", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  461. let res = render_content(r#"
  462. Some text
  463. <h1>Helo</h1>
  464. <div>
  465. <a href="mobx-flow.png">
  466. <img src="mobx-flow.png" alt="MobX flow">
  467. </a>
  468. </div>
  469. "#, &context).unwrap();
  470. assert_eq!(
  471. res.body,
  472. "<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"
  473. );
  474. }
  475. #[test]
  476. fn can_validate_valid_external_links() {
  477. let permalinks_ctx = HashMap::new();
  478. let mut config = Config::default();
  479. config.check_external_links = true;
  480. let context = RenderContext::new(&GUTENBERG_TERA, &config, "https://vincent.is/about/", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  481. let res = render_content("[a link](http://google.com)", &context).unwrap();
  482. assert_eq!(
  483. res.body,
  484. "<p><a href=\"http://google.com\">a link</a></p>\n"
  485. );
  486. }
  487. #[test]
  488. fn can_show_error_message_for_invalid_external_links() {
  489. let permalinks_ctx = HashMap::new();
  490. let mut config = Config::default();
  491. config.check_external_links = true;
  492. let context = RenderContext::new(&GUTENBERG_TERA, &config, "https://vincent.is/about/", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  493. let res = render_content("[a link](http://google.comy)", &context);
  494. assert!(res.is_err());
  495. let err = res.unwrap_err();
  496. assert!(err.description().contains("Link http://google.comy is not valid"));
  497. }
  498. #[test]
  499. fn doesnt_try_to_validate_email_links_mailto() {
  500. let permalinks_ctx = HashMap::new();
  501. let mut config = Config::default();
  502. config.check_external_links = true;
  503. let context = RenderContext::new(&GUTENBERG_TERA, &config, "https://vincent.is/about/", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  504. let res = render_content("Email: [foo@bar.baz](mailto:foo@bar.baz)", &context).unwrap();
  505. assert_eq!(
  506. res.body,
  507. "<p>Email: <a href=\"mailto:foo@bar.baz\">foo@bar.baz</a></p>\n"
  508. );
  509. }
  510. #[test]
  511. fn doesnt_try_to_validate_email_links_angled_brackets() {
  512. let permalinks_ctx = HashMap::new();
  513. let mut config = Config::default();
  514. config.check_external_links = true;
  515. let context = RenderContext::new(&GUTENBERG_TERA, &config, "https://vincent.is/about/", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  516. let res = render_content("Email: <foo@bar.baz>", &context).unwrap();
  517. assert_eq!(
  518. res.body,
  519. "<p>Email: <a href=\"mailto:foo@bar.baz\">foo@bar.baz</a></p>\n"
  520. );
  521. }
  522. #[test]
  523. fn can_handle_summaries() {
  524. let tera_ctx = Tera::default();
  525. let permalinks_ctx = HashMap::new();
  526. let config = Config::default();
  527. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
  528. let res = render_content("Hello [world]\n\n<!-- more -->\n\nBla bla\n\n[world]: https://vincent.is/about/", &context).unwrap();
  529. assert_eq!(
  530. res.body,
  531. "<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"
  532. );
  533. assert_eq!(
  534. res.summary_len,
  535. Some("<p>Hello <a href=\"https://vincent.is/about/\">world</a></p>\n".len())
  536. );
  537. }