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 27KB

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
Allow manual specification of header IDs (#685) Justification for this feature is added in the docs. Precedent for the precise syntax: Hugo. Hugo puts this syntax behind a preference named headerIds, and automatic header ID generation behind a preference named autoHeaderIds, with both enabled by default. I have not implemented a switch to disable this. My suggestion for a workaround for the improbable case of desiring a literal “{#…}” at the end of a header is to replace `}` with `&#125;`. The algorithm I have used is not identical to [that which Hugo uses][0], because Hugo’s looks to work at the source level, whereas here we work at the pulldown-cmark event level, which is generally more sane, but potentially limiting for extremely esoteric IDs. Practical differences in implementation from Hugo (based purely on reading [blackfriday’s implementation][0], not actually trying it): - I believe Hugo would treat `# Foo {#*bar*}` as a heading with text “Foo” and ID `*bar*`, since it is working at the source level; whereas this code turns it into a heading with HTML `Foo {#<em>bar</em>}`, as it works at the pulldown-cmark event level and doesn’t go out of its way to make that work (I’m not familiar with pulldown-cmark, but I get the impression that you could make it work Hugo’s way on this point). The difference should be negligible: only *very* esoteric hashes would include magic Markdown characters. - Hugo will automatically generate an ID for `{#}`, whereas what I’ve coded here will yield a blank ID instead (which feels more correct to me—`None` versus `Some("")`, and all that). In practice the results should be identical. Fixes #433. [0]: https://github.com/russross/blackfriday/blob/a477dd1646916742841ed20379f941cfa6c5bb6f/block.go#L218-L234
5 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. extern crate config;
  2. extern crate front_matter;
  3. extern crate rendering;
  4. extern crate templates;
  5. extern crate tera;
  6. use std::collections::HashMap;
  7. use tera::Tera;
  8. use config::Config;
  9. use front_matter::InsertAnchor;
  10. use rendering::{render_content, RenderContext};
  11. use templates::ZOLA_TERA;
  12. #[test]
  13. fn can_do_render_content_simple() {
  14. let tera_ctx = Tera::default();
  15. let permalinks_ctx = HashMap::new();
  16. let config = Config::default();
  17. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  18. let res = render_content("hello", &context).unwrap();
  19. assert_eq!(res.body, "<p>hello</p>\n");
  20. }
  21. #[test]
  22. fn doesnt_highlight_code_block_with_highlighting_off() {
  23. let tera_ctx = Tera::default();
  24. let permalinks_ctx = HashMap::new();
  25. let mut config = Config::default();
  26. config.highlight_code = false;
  27. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  28. let res = render_content("```\n$ gutenberg server\n```", &context).unwrap();
  29. assert_eq!(res.body, "<pre><code>$ gutenberg server\n</code></pre>\n");
  30. }
  31. #[test]
  32. fn can_highlight_code_block_no_lang() {
  33. let tera_ctx = Tera::default();
  34. let permalinks_ctx = HashMap::new();
  35. let mut config = Config::default();
  36. config.highlight_code = true;
  37. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  38. let res = render_content("```\n$ gutenberg server\n$ ping\n```", &context).unwrap();
  39. assert_eq!(
  40. res.body,
  41. "<pre style=\"background-color:#2b303b;\">\n<span style=\"color:#c0c5ce;\">$ gutenberg server\n$ ping\n</span></pre>"
  42. );
  43. }
  44. #[test]
  45. fn can_highlight_code_block_with_lang() {
  46. let tera_ctx = Tera::default();
  47. let permalinks_ctx = HashMap::new();
  48. let mut config = Config::default();
  49. config.highlight_code = true;
  50. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  51. let res = render_content("```python\nlist.append(1)\n```", &context).unwrap();
  52. assert_eq!(
  53. res.body,
  54. "<pre style=\"background-color:#2b303b;\">\n<span style=\"color:#c0c5ce;\">list.</span><span style=\"color:#bf616a;\">append</span><span style=\"color:#c0c5ce;\">(</span><span style=\"color:#d08770;\">1</span><span style=\"color:#c0c5ce;\">)\n</span></pre>"
  55. );
  56. }
  57. #[test]
  58. fn can_higlight_code_block_with_unknown_lang() {
  59. let tera_ctx = Tera::default();
  60. let permalinks_ctx = HashMap::new();
  61. let mut config = Config::default();
  62. config.highlight_code = true;
  63. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  64. let res = render_content("```yolo\nlist.append(1)\n```", &context).unwrap();
  65. // defaults to plain text
  66. assert_eq!(
  67. res.body,
  68. "<pre style=\"background-color:#2b303b;\">\n<span style=\"color:#c0c5ce;\">list.append(1)\n</span></pre>"
  69. );
  70. }
  71. #[test]
  72. fn can_render_shortcode() {
  73. let permalinks_ctx = HashMap::new();
  74. let config = Config::default();
  75. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  76. let res = render_content(
  77. r#"
  78. Hello
  79. {{ youtube(id="ub36ffWAqgQ") }}
  80. "#,
  81. &context,
  82. )
  83. .unwrap();
  84. assert!(res.body.contains("<p>Hello</p>\n<div >"));
  85. assert!(res.body.contains(r#"<iframe src="https://www.youtube.com/embed/ub36ffWAqgQ""#));
  86. }
  87. #[test]
  88. fn can_render_shortcode_with_markdown_char_in_args_name() {
  89. let permalinks_ctx = HashMap::new();
  90. let config = Config::default();
  91. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  92. let input = vec!["name", "na_me", "n_a_me", "n1"];
  93. for i in input {
  94. let res =
  95. render_content(&format!("{{{{ youtube(id=\"hey\", {}=1) }}}}", i), &context).unwrap();
  96. assert!(res.body.contains(r#"<iframe src="https://www.youtube.com/embed/hey""#));
  97. }
  98. }
  99. #[test]
  100. fn can_render_shortcode_with_markdown_char_in_args_value() {
  101. let permalinks_ctx = HashMap::new();
  102. let config = Config::default();
  103. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  104. let input = vec![
  105. "ub36ffWAqgQ-hey",
  106. "ub36ffWAqgQ_hey",
  107. "ub36ffWAqgQ_he_y",
  108. "ub36ffWAqgQ*hey",
  109. "ub36ffWAqgQ#hey",
  110. ];
  111. for i in input {
  112. let res = render_content(&format!("{{{{ youtube(id=\"{}\") }}}}", i), &context).unwrap();
  113. assert!(res
  114. .body
  115. .contains(&format!(r#"<iframe src="https://www.youtube.com/embed/{}""#, i)));
  116. }
  117. }
  118. #[test]
  119. fn can_render_body_shortcode_with_markdown_char_in_name() {
  120. let permalinks_ctx = HashMap::new();
  121. let mut tera = Tera::default();
  122. tera.extend(&ZOLA_TERA).unwrap();
  123. let input = vec!["quo_te", "qu_o_te"];
  124. let config = Config::default();
  125. for i in input {
  126. tera.add_raw_template(
  127. &format!("shortcodes/{}.html", i),
  128. "<blockquote>{{ body }} - {{ author}}</blockquote>",
  129. )
  130. .unwrap();
  131. let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
  132. let res =
  133. render_content(&format!("{{% {}(author=\"Bob\") %}}\nhey\n{{% end %}}", i), &context)
  134. .unwrap();
  135. println!("{:?}", res);
  136. assert!(res.body.contains("<blockquote>hey - Bob</blockquote>"));
  137. }
  138. }
  139. #[test]
  140. fn can_render_body_shortcode_and_paragraph_after() {
  141. let permalinks_ctx = HashMap::new();
  142. let mut tera = Tera::default();
  143. tera.extend(&ZOLA_TERA).unwrap();
  144. let shortcode = "<p>{{ body }}</p>";
  145. let markdown_string = r#"
  146. {% figure() %}
  147. This is a figure caption.
  148. {% end %}
  149. Here is another paragraph.
  150. "#;
  151. let expected = "<p>This is a figure caption.</p>
  152. <p>Here is another paragraph.</p>
  153. ";
  154. tera.add_raw_template(&format!("shortcodes/{}.html", "figure"), shortcode).unwrap();
  155. let config = Config::default();
  156. let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
  157. let res = render_content(markdown_string, &context).unwrap();
  158. println!("{:?}", res);
  159. assert_eq!(res.body, expected);
  160. }
  161. #[test]
  162. fn can_render_two_body_shortcode_and_paragraph_after_with_line_break_between() {
  163. let permalinks_ctx = HashMap::new();
  164. let mut tera = Tera::default();
  165. tera.extend(&ZOLA_TERA).unwrap();
  166. let shortcode = "<p>{{ body }}</p>";
  167. let markdown_string = r#"
  168. {% figure() %}
  169. This is a figure caption.
  170. {% end %}
  171. {% figure() %}
  172. This is a figure caption.
  173. {% end %}
  174. Here is another paragraph.
  175. "#;
  176. let expected = "<p>This is a figure caption.</p>
  177. <p>This is a figure caption.</p>
  178. <p>Here is another paragraph.</p>
  179. ";
  180. tera.add_raw_template(&format!("shortcodes/{}.html", "figure"), shortcode).unwrap();
  181. let config = Config::default();
  182. let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
  183. let res = render_content(markdown_string, &context).unwrap();
  184. println!("{:?}", res);
  185. assert_eq!(res.body, expected);
  186. }
  187. #[test]
  188. fn can_render_several_shortcode_in_row() {
  189. let permalinks_ctx = HashMap::new();
  190. let config = Config::default();
  191. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  192. let res = render_content(
  193. r#"
  194. Hello
  195. {{ youtube(id="ub36ffWAqgQ") }}
  196. {{ youtube(id="ub36ffWAqgQ", autoplay=true) }}
  197. {{ vimeo(id="210073083") }}
  198. {{ streamable(id="c0ic") }}
  199. {{ gist(url="https://gist.github.com/Keats/32d26f699dcc13ebd41b") }}
  200. "#,
  201. &context,
  202. )
  203. .unwrap();
  204. assert!(res.body.contains("<p>Hello</p>\n<div >"));
  205. assert!(res.body.contains(r#"<iframe src="https://www.youtube.com/embed/ub36ffWAqgQ""#));
  206. assert!(res
  207. .body
  208. .contains(r#"<iframe src="https://www.youtube.com/embed/ub36ffWAqgQ?autoplay=1""#));
  209. assert!(res.body.contains(r#"<iframe src="https://www.streamable.com/e/c0ic""#));
  210. assert!(res.body.contains(r#"//player.vimeo.com/video/210073083""#));
  211. }
  212. #[test]
  213. fn doesnt_render_ignored_shortcodes() {
  214. let permalinks_ctx = HashMap::new();
  215. let mut config = Config::default();
  216. config.highlight_code = false;
  217. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  218. let res = render_content(r#"```{{/* youtube(id="w7Ft2ymGmfc") */}}```"#, &context).unwrap();
  219. assert_eq!(res.body, "<p><code>{{ youtube(id=&quot;w7Ft2ymGmfc&quot;) }}</code></p>\n");
  220. }
  221. #[test]
  222. fn can_render_shortcode_with_body() {
  223. let mut tera = Tera::default();
  224. tera.extend(&ZOLA_TERA).unwrap();
  225. tera.add_raw_template(
  226. "shortcodes/quote.html",
  227. "<blockquote>{{ body }} - {{ author }}</blockquote>",
  228. )
  229. .unwrap();
  230. let permalinks_ctx = HashMap::new();
  231. let config = Config::default();
  232. let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
  233. let res = render_content(
  234. r#"
  235. Hello
  236. {% quote(author="Keats") %}
  237. A quote
  238. {% end %}
  239. "#,
  240. &context,
  241. )
  242. .unwrap();
  243. assert_eq!(res.body, "<p>Hello</p>\n<blockquote>A quote - Keats</blockquote>\n");
  244. }
  245. #[test]
  246. fn errors_rendering_unknown_shortcode() {
  247. let tera_ctx = Tera::default();
  248. let permalinks_ctx = HashMap::new();
  249. let config = Config::default();
  250. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  251. let res = render_content("{{ hello(flash=true) }}", &context);
  252. assert!(res.is_err());
  253. }
  254. #[test]
  255. fn can_make_valid_relative_link() {
  256. let mut permalinks = HashMap::new();
  257. permalinks.insert("pages/about.md".to_string(), "https://vincent.is/about".to_string());
  258. let tera_ctx = Tera::default();
  259. let config = Config::default();
  260. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks, InsertAnchor::None);
  261. let res = render_content(
  262. r#"[rel link](@/pages/about.md), [abs link](https://vincent.is/about)"#,
  263. &context,
  264. )
  265. .unwrap();
  266. assert!(
  267. res.body.contains(r#"<p><a href="https://vincent.is/about">rel link</a>, <a href="https://vincent.is/about">abs link</a></p>"#)
  268. );
  269. }
  270. #[test]
  271. fn can_make_relative_links_with_anchors() {
  272. let mut permalinks = HashMap::new();
  273. permalinks.insert("pages/about.md".to_string(), "https://vincent.is/about".to_string());
  274. let tera_ctx = Tera::default();
  275. let config = Config::default();
  276. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks, InsertAnchor::None);
  277. let res = render_content(r#"[rel link](@/pages/about.md#cv)"#, &context).unwrap();
  278. assert!(res.body.contains(r#"<p><a href="https://vincent.is/about#cv">rel link</a></p>"#));
  279. }
  280. #[test]
  281. fn errors_relative_link_inexistant() {
  282. let tera_ctx = Tera::default();
  283. let permalinks_ctx = HashMap::new();
  284. let config = Config::default();
  285. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  286. let res = render_content("[rel link](@/pages/about.md)", &context);
  287. assert!(res.is_err());
  288. }
  289. #[test]
  290. fn can_add_id_to_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_handle_manual_ids_on_headers() {
  309. let tera_ctx = Tera::default();
  310. let permalinks_ctx = HashMap::new();
  311. let config = Config::default();
  312. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  313. // Tested things: manual IDs; whitespace flexibility; that automatic IDs avoid collision with
  314. // manual IDs; that duplicates are in fact permitted among manual IDs; that any non-plain-text
  315. // in the middle of `{#…}` will disrupt it from being acknowledged as a manual ID (that last
  316. // one could reasonably be considered a bug rather than a feature, but test it either way); one
  317. // workaround for the improbable case where you actually want `{#…}` at the end of a header.
  318. let res = render_content("\
  319. # Hello\n\
  320. # Hello{#hello}\n\
  321. # Hello {#hello}\n\
  322. # Hello {#Something_else} \n\
  323. # Workaround for literal {#…&#125;\n\
  324. # Hello\n\
  325. # Auto {#*matic*}", &context).unwrap();
  326. assert_eq!(res.body, "\
  327. <h1 id=\"hello-1\">Hello</h1>\n\
  328. <h1 id=\"hello\">Hello</h1>\n\
  329. <h1 id=\"hello\">Hello</h1>\n\
  330. <h1 id=\"Something_else\">Hello</h1>\n\
  331. <h1 id=\"workaround-for-literal\">Workaround for literal {#…}</h1>\n\
  332. <h1 id=\"hello-2\">Hello</h1>\n\
  333. <h1 id=\"auto-matic\">Auto {#<em>matic</em>}</h1>\n\
  334. ");
  335. }
  336. #[test]
  337. fn blank_headers() {
  338. let tera_ctx = Tera::default();
  339. let permalinks_ctx = HashMap::new();
  340. let config = Config::default();
  341. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  342. let res = render_content("# \n#\n# {#hmm} \n# {#}", &context).unwrap();
  343. assert_eq!(res.body, "<h1 id=\"-1\"></h1>\n<h1 id=\"-2\"></h1>\n<h1 id=\"hmm\"></h1>\n<h1 id=\"\"></h1>\n");
  344. }
  345. #[test]
  346. fn can_insert_anchor_left() {
  347. let permalinks_ctx = HashMap::new();
  348. let config = Config::default();
  349. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Left);
  350. let res = render_content("# Hello", &context).unwrap();
  351. assert_eq!(
  352. res.body,
  353. "<h1 id=\"hello\"><a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>\nHello</h1>\n"
  354. );
  355. }
  356. #[test]
  357. fn can_insert_anchor_right() {
  358. let permalinks_ctx = HashMap::new();
  359. let config = Config::default();
  360. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Right);
  361. let res = render_content("# Hello", &context).unwrap();
  362. assert_eq!(
  363. res.body,
  364. "<h1 id=\"hello\">Hello<a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>\n</h1>\n"
  365. );
  366. }
  367. #[test]
  368. fn can_insert_anchor_for_multi_header() {
  369. let permalinks_ctx = HashMap::new();
  370. let config = Config::default();
  371. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Right);
  372. let res = render_content("# Hello\n# World", &context).unwrap();
  373. assert_eq!(
  374. res.body,
  375. "<h1 id=\"hello\">Hello<a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>\n</h1>\n\
  376. <h1 id=\"world\">World<a class=\"zola-anchor\" href=\"#world\" aria-label=\"Anchor link for: world\">🔗</a>\n</h1>\n"
  377. );
  378. }
  379. // See https://github.com/Keats/gutenberg/issues/42
  380. #[test]
  381. fn can_insert_anchor_with_exclamation_mark() {
  382. let permalinks_ctx = HashMap::new();
  383. let config = Config::default();
  384. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Left);
  385. let res = render_content("# Hello!", &context).unwrap();
  386. assert_eq!(
  387. res.body,
  388. "<h1 id=\"hello\"><a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>\nHello!</h1>\n"
  389. );
  390. }
  391. // See https://github.com/Keats/gutenberg/issues/53
  392. #[test]
  393. fn can_insert_anchor_with_link() {
  394. let permalinks_ctx = HashMap::new();
  395. let config = Config::default();
  396. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Left);
  397. let res = render_content("## [Rust](https://rust-lang.org)", &context).unwrap();
  398. assert_eq!(
  399. res.body,
  400. "<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"
  401. );
  402. }
  403. #[test]
  404. fn can_insert_anchor_with_other_special_chars() {
  405. let permalinks_ctx = HashMap::new();
  406. let config = Config::default();
  407. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Left);
  408. let res = render_content("# Hello*_()", &context).unwrap();
  409. assert_eq!(
  410. res.body,
  411. "<h1 id=\"hello\"><a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>\nHello*_()</h1>\n"
  412. );
  413. }
  414. #[test]
  415. fn can_make_toc() {
  416. let permalinks_ctx = HashMap::new();
  417. let config = Config::default();
  418. let context = RenderContext::new(
  419. &ZOLA_TERA,
  420. &config,
  421. "https://mysite.com/something",
  422. &permalinks_ctx,
  423. InsertAnchor::Left,
  424. );
  425. let res = render_content(
  426. r#"
  427. # Header 1
  428. ## Header 2
  429. ## Another Header 2
  430. ### Last one
  431. "#,
  432. &context,
  433. )
  434. .unwrap();
  435. let toc = res.toc;
  436. assert_eq!(toc.len(), 1);
  437. assert_eq!(toc[0].children.len(), 2);
  438. assert_eq!(toc[0].children[1].children.len(), 1);
  439. }
  440. #[test]
  441. fn can_ignore_tags_in_toc() {
  442. let permalinks_ctx = HashMap::new();
  443. let config = Config::default();
  444. let context = RenderContext::new(
  445. &ZOLA_TERA,
  446. &config,
  447. "https://mysite.com/something",
  448. &permalinks_ctx,
  449. InsertAnchor::Left,
  450. );
  451. let res = render_content(
  452. r#"
  453. ## header with `code`
  454. ## [anchor](https://duckduckgo.com/) in header
  455. ## **bold** and *italics*
  456. "#,
  457. &context,
  458. )
  459. .unwrap();
  460. let toc = res.toc;
  461. assert_eq!(toc[0].id, "header-with-code");
  462. assert_eq!(toc[0].title, "header with code");
  463. assert_eq!(toc[1].id, "anchor-in-header");
  464. assert_eq!(toc[1].title, "anchor in header");
  465. assert_eq!(toc[2].id, "bold-and-italics");
  466. assert_eq!(toc[2].title, "bold and italics");
  467. }
  468. #[test]
  469. fn can_understand_backtick_in_titles() {
  470. let permalinks_ctx = HashMap::new();
  471. let config = Config::default();
  472. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  473. let res = render_content("# `Hello`", &context).unwrap();
  474. assert_eq!(res.body, "<h1 id=\"hello\"><code>Hello</code></h1>\n");
  475. }
  476. #[test]
  477. fn can_understand_backtick_in_paragraphs() {
  478. let permalinks_ctx = HashMap::new();
  479. let config = Config::default();
  480. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  481. let res = render_content("Hello `world`", &context).unwrap();
  482. assert_eq!(res.body, "<p>Hello <code>world</code></p>\n");
  483. }
  484. // https://github.com/Keats/gutenberg/issues/297
  485. #[test]
  486. fn can_understand_links_in_header() {
  487. let permalinks_ctx = HashMap::new();
  488. let config = Config::default();
  489. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  490. let res = render_content("# [Rust](https://rust-lang.org)", &context).unwrap();
  491. assert_eq!(res.body, "<h1 id=\"rust\"><a href=\"https://rust-lang.org\">Rust</a></h1>\n");
  492. }
  493. #[test]
  494. fn can_understand_link_with_title_in_header() {
  495. let permalinks_ctx = HashMap::new();
  496. let config = Config::default();
  497. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  498. let res =
  499. render_content("# [Rust](https://rust-lang.org \"Rust homepage\")", &context).unwrap();
  500. assert_eq!(
  501. res.body,
  502. "<h1 id=\"rust\"><a href=\"https://rust-lang.org\" title=\"Rust homepage\">Rust</a></h1>\n"
  503. );
  504. }
  505. #[test]
  506. fn can_understand_emphasis_in_header() {
  507. let permalinks_ctx = HashMap::new();
  508. let config = Config::default();
  509. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  510. let res = render_content("# *Emphasis* text", &context).unwrap();
  511. assert_eq!(res.body, "<h1 id=\"emphasis-text\"><em>Emphasis</em> text</h1>\n");
  512. }
  513. #[test]
  514. fn can_understand_strong_in_header() {
  515. let permalinks_ctx = HashMap::new();
  516. let config = Config::default();
  517. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  518. let res = render_content("# **Strong** text", &context).unwrap();
  519. assert_eq!(res.body, "<h1 id=\"strong-text\"><strong>Strong</strong> text</h1>\n");
  520. }
  521. #[test]
  522. fn can_understand_code_in_header() {
  523. let permalinks_ctx = HashMap::new();
  524. let config = Config::default();
  525. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  526. let res = render_content("# `Code` text", &context).unwrap();
  527. assert_eq!(res.body, "<h1 id=\"code-text\"><code>Code</code> text</h1>\n");
  528. }
  529. // See https://github.com/getzola/zola/issues/569
  530. #[test]
  531. fn can_understand_footnote_in_header() {
  532. let permalinks_ctx = HashMap::new();
  533. let config = Config::default();
  534. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  535. let res = render_content("# text [^1] there\n[^1]: footnote", &context).unwrap();
  536. assert_eq!(res.body, r##"<h1 id="text-there">text <sup class="footnote-reference"><a href="#1">1</a></sup> there</h1>
  537. <div class="footnote-definition" id="1"><sup class="footnote-definition-label">1</sup>
  538. <p>footnote</p>
  539. </div>
  540. "##);
  541. }
  542. #[test]
  543. fn can_make_valid_relative_link_in_header() {
  544. let mut permalinks = HashMap::new();
  545. permalinks.insert("pages/about.md".to_string(), "https://vincent.is/about/".to_string());
  546. let tera_ctx = Tera::default();
  547. let config = Config::default();
  548. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks, InsertAnchor::None);
  549. let res = render_content(r#" # [rel link](@/pages/about.md)"#, &context).unwrap();
  550. assert_eq!(
  551. res.body,
  552. "<h1 id=\"rel-link\"><a href=\"https://vincent.is/about/\">rel link</a></h1>\n"
  553. );
  554. }
  555. #[test]
  556. fn can_make_permalinks_with_colocated_assets_for_link() {
  557. let permalinks_ctx = HashMap::new();
  558. let config = Config::default();
  559. let context = RenderContext::new(
  560. &ZOLA_TERA,
  561. &config,
  562. "https://vincent.is/about/",
  563. &permalinks_ctx,
  564. InsertAnchor::None,
  565. );
  566. let res = render_content("[an image](image.jpg)", &context).unwrap();
  567. assert_eq!(res.body, "<p><a href=\"https://vincent.is/about/image.jpg\">an image</a></p>\n");
  568. }
  569. #[test]
  570. fn can_make_permalinks_with_colocated_assets_for_image() {
  571. let permalinks_ctx = HashMap::new();
  572. let config = Config::default();
  573. let context = RenderContext::new(
  574. &ZOLA_TERA,
  575. &config,
  576. "https://vincent.is/about/",
  577. &permalinks_ctx,
  578. InsertAnchor::None,
  579. );
  580. let res = render_content("![alt text](image.jpg)", &context).unwrap();
  581. assert_eq!(
  582. res.body,
  583. "<p><img src=\"https://vincent.is/about/image.jpg\" alt=\"alt text\" /></p>\n"
  584. );
  585. }
  586. #[test]
  587. fn markdown_doesnt_wrap_html_in_paragraph() {
  588. let permalinks_ctx = HashMap::new();
  589. let config = Config::default();
  590. let context = RenderContext::new(
  591. &ZOLA_TERA,
  592. &config,
  593. "https://vincent.is/about/",
  594. &permalinks_ctx,
  595. InsertAnchor::None,
  596. );
  597. let res = render_content(
  598. r#"
  599. Some text
  600. <h1>Helo</h1>
  601. <div>
  602. <a href="mobx-flow.png">
  603. <img src="mobx-flow.png" alt="MobX flow">
  604. </a>
  605. </div>
  606. "#,
  607. &context,
  608. )
  609. .unwrap();
  610. assert_eq!(
  611. res.body,
  612. "<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"
  613. );
  614. }
  615. #[test]
  616. fn correctly_captures_external_links() {
  617. let permalinks_ctx = HashMap::new();
  618. let config = Config::default();
  619. let context = RenderContext::new(
  620. &ZOLA_TERA,
  621. &config,
  622. "https://vincent.is/about/",
  623. &permalinks_ctx,
  624. InsertAnchor::None,
  625. );
  626. let content = "
  627. [a link](http://google.com)
  628. [a link](http://google.comy)
  629. Email: [foo@bar.baz](mailto:foo@bar.baz)
  630. Email: <foo@bar.baz>
  631. ";
  632. let res = render_content(content, &context).unwrap();
  633. assert_eq!(res.external_links, &["http://google.com".to_owned(), "http://google.comy".to_owned()]);
  634. }
  635. #[test]
  636. fn can_handle_summaries() {
  637. let tera_ctx = Tera::default();
  638. let permalinks_ctx = HashMap::new();
  639. let config = Config::default();
  640. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  641. let res = render_content(
  642. r#"
  643. Hello [My site][world]
  644. <!-- more -->
  645. Bla bla
  646. [world]: https://vincentprouillet.com
  647. "#,
  648. &context,
  649. )
  650. .unwrap();
  651. assert_eq!(
  652. res.body,
  653. "<p>Hello <a href=\"https://vincentprouillet.com\">My site</a></p>\n<p id=\"zola-continue-reading\"><a name=\"continue-reading\"></a></p>\n<p>Bla bla</p>\n"
  654. );
  655. assert_eq!(
  656. res.summary_len,
  657. Some("<p>Hello <a href=\"https://vincentprouillet.com/\">My site</a></p>".len())
  658. );
  659. }
  660. // https://github.com/Keats/gutenberg/issues/522
  661. #[test]
  662. fn doesnt_try_to_highlight_content_from_shortcode() {
  663. let permalinks_ctx = HashMap::new();
  664. let mut tera = Tera::default();
  665. tera.extend(&ZOLA_TERA).unwrap();
  666. let shortcode = r#"
  667. <figure>
  668. {% if width %}
  669. <img src="/images/{{ src }}" alt="{{ caption }}" width="{{ width }}" />
  670. {% else %}
  671. <img src="/images/{{ src }}" alt="{{ caption }}" />
  672. {% endif %}
  673. <figcaption>{{ caption }}</figcaption>
  674. </figure>"#;
  675. let markdown_string = r#"{{ figure(src="spherecluster.png", caption="Some spheres.") }}"#;
  676. let expected = r#"<figure>
  677. <img src="/images/spherecluster.png" alt="Some spheres." />
  678. <figcaption>Some spheres.</figcaption>
  679. </figure>"#;
  680. tera.add_raw_template(&format!("shortcodes/{}.html", "figure"), shortcode).unwrap();
  681. let config = Config::default();
  682. let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
  683. let res = render_content(markdown_string, &context).unwrap();
  684. assert_eq!(res.body, expected);
  685. }
  686. // TODO: re-enable once it's fixed in Tera
  687. // https://github.com/Keats/tera/issues/373
  688. //#[test]
  689. //fn can_split_lines_shortcode_body() {
  690. // let permalinks_ctx = HashMap::new();
  691. // let mut tera = Tera::default();
  692. // tera.extend(&ZOLA_TERA).unwrap();
  693. //
  694. // let shortcode = r#"{{ body | split(pat="\n") }}"#;
  695. //
  696. // let markdown_string = r#"
  697. //{% alert() %}
  698. //multi
  699. //ple
  700. //lines
  701. //{% end %}
  702. // "#;
  703. //
  704. // let expected = r#"<p>["multi", "ple", "lines"]</p>"#;
  705. //
  706. // tera.add_raw_template(&format!("shortcodes/{}.html", "alert"), shortcode).unwrap();
  707. // let config = Config::default();
  708. // let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
  709. //
  710. // let res = render_content(markdown_string, &context).unwrap();
  711. // assert_eq!(res.body, expected);
  712. //}