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

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
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
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
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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. use std::collections::HashMap;
  2. use tera::Tera;
  3. use config::Config;
  4. use front_matter::InsertAnchor;
  5. use rendering::{render_content, RenderContext};
  6. use templates::ZOLA_TERA;
  7. use utils::slugs::SlugifyStrategy;
  8. #[test]
  9. fn can_do_render_content_simple() {
  10. let tera_ctx = Tera::default();
  11. let permalinks_ctx = HashMap::new();
  12. let config = Config::default();
  13. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  14. let res = render_content("hello", &context).unwrap();
  15. assert_eq!(res.body, "<p>hello</p>\n");
  16. }
  17. #[test]
  18. fn doesnt_highlight_code_block_with_highlighting_off() {
  19. let tera_ctx = Tera::default();
  20. let permalinks_ctx = HashMap::new();
  21. let mut config = Config::default();
  22. config.highlight_code = false;
  23. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  24. let res = render_content("```\n$ gutenberg server\n```", &context).unwrap();
  25. assert_eq!(res.body, "<pre><code>$ gutenberg server\n</code></pre>\n");
  26. }
  27. #[test]
  28. fn can_highlight_code_block_no_lang() {
  29. let tera_ctx = Tera::default();
  30. let permalinks_ctx = HashMap::new();
  31. let mut config = Config::default();
  32. config.highlight_code = true;
  33. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  34. let res = render_content("```\n$ gutenberg server\n$ ping\n```", &context).unwrap();
  35. assert_eq!(
  36. res.body,
  37. "<pre style=\"background-color:#2b303b;\">\n<span style=\"color:#c0c5ce;\">$ gutenberg server\n$ ping\n</span></pre>"
  38. );
  39. }
  40. #[test]
  41. fn can_highlight_code_block_with_lang() {
  42. let tera_ctx = Tera::default();
  43. let permalinks_ctx = HashMap::new();
  44. let mut config = Config::default();
  45. config.highlight_code = true;
  46. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  47. let res = render_content("```python\nlist.append(1)\n```", &context).unwrap();
  48. assert_eq!(
  49. res.body,
  50. "<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>"
  51. );
  52. }
  53. #[test]
  54. fn can_higlight_code_block_with_unknown_lang() {
  55. let tera_ctx = Tera::default();
  56. let permalinks_ctx = HashMap::new();
  57. let mut config = Config::default();
  58. config.highlight_code = true;
  59. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  60. let res = render_content("```yolo\nlist.append(1)\n```", &context).unwrap();
  61. // defaults to plain text
  62. assert_eq!(
  63. res.body,
  64. "<pre style=\"background-color:#2b303b;\">\n<span style=\"color:#c0c5ce;\">list.append(1)\n</span></pre>"
  65. );
  66. }
  67. #[test]
  68. fn can_render_shortcode() {
  69. let permalinks_ctx = HashMap::new();
  70. let config = Config::default();
  71. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  72. let res = render_content(
  73. r#"
  74. Hello
  75. {{ youtube(id="ub36ffWAqgQ") }}
  76. "#,
  77. &context,
  78. )
  79. .unwrap();
  80. assert!(res.body.contains("<p>Hello</p>\n<div >"));
  81. assert!(res.body.contains(r#"<iframe src="https://www.youtube.com/embed/ub36ffWAqgQ""#));
  82. }
  83. #[test]
  84. fn can_render_shortcode_with_markdown_char_in_args_name() {
  85. let permalinks_ctx = HashMap::new();
  86. let config = Config::default();
  87. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  88. let input = vec!["name", "na_me", "n_a_me", "n1"];
  89. for i in input {
  90. let res =
  91. render_content(&format!("{{{{ youtube(id=\"hey\", {}=1) }}}}", i), &context).unwrap();
  92. assert!(res.body.contains(r#"<iframe src="https://www.youtube.com/embed/hey""#));
  93. }
  94. }
  95. #[test]
  96. fn can_render_shortcode_with_markdown_char_in_args_value() {
  97. let permalinks_ctx = HashMap::new();
  98. let config = Config::default();
  99. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  100. let input = vec![
  101. "ub36ffWAqgQ-hey",
  102. "ub36ffWAqgQ_hey",
  103. "ub36ffWAqgQ_he_y",
  104. "ub36ffWAqgQ*hey",
  105. "ub36ffWAqgQ#hey",
  106. ];
  107. for i in input {
  108. let res = render_content(&format!("{{{{ youtube(id=\"{}\") }}}}", i), &context).unwrap();
  109. assert!(res
  110. .body
  111. .contains(&format!(r#"<iframe src="https://www.youtube.com/embed/{}""#, i)));
  112. }
  113. }
  114. #[test]
  115. fn can_render_body_shortcode_with_markdown_char_in_name() {
  116. let permalinks_ctx = HashMap::new();
  117. let mut tera = Tera::default();
  118. tera.extend(&ZOLA_TERA).unwrap();
  119. let input = vec!["quo_te", "qu_o_te"];
  120. let config = Config::default();
  121. for i in input {
  122. tera.add_raw_template(
  123. &format!("shortcodes/{}.html", i),
  124. "<blockquote>{{ body }} - {{ author}}</blockquote>",
  125. )
  126. .unwrap();
  127. let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
  128. let res =
  129. render_content(&format!("{{% {}(author=\"Bob\") %}}\nhey\n{{% end %}}", i), &context)
  130. .unwrap();
  131. println!("{:?}", res);
  132. assert!(res.body.contains("<blockquote>hey - Bob</blockquote>"));
  133. }
  134. }
  135. #[test]
  136. fn can_render_body_shortcode_and_paragraph_after() {
  137. let permalinks_ctx = HashMap::new();
  138. let mut tera = Tera::default();
  139. tera.extend(&ZOLA_TERA).unwrap();
  140. let shortcode = "<p>{{ body }}</p>";
  141. let markdown_string = r#"
  142. {% figure() %}
  143. This is a figure caption.
  144. {% end %}
  145. Here is another paragraph.
  146. "#;
  147. let expected = "<p>This is a figure caption.</p>
  148. <p>Here is another paragraph.</p>
  149. ";
  150. tera.add_raw_template(&format!("shortcodes/{}.html", "figure"), shortcode).unwrap();
  151. let config = Config::default();
  152. let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
  153. let res = render_content(markdown_string, &context).unwrap();
  154. println!("{:?}", res);
  155. assert_eq!(res.body, expected);
  156. }
  157. #[test]
  158. fn can_render_two_body_shortcode_and_paragraph_after_with_line_break_between() {
  159. let permalinks_ctx = HashMap::new();
  160. let mut tera = Tera::default();
  161. tera.extend(&ZOLA_TERA).unwrap();
  162. let shortcode = "<p>{{ body }}</p>";
  163. let markdown_string = r#"
  164. {% figure() %}
  165. This is a figure caption.
  166. {% end %}
  167. {% figure() %}
  168. This is a figure caption.
  169. {% end %}
  170. Here is another paragraph.
  171. "#;
  172. let expected = "<p>This is a figure caption.</p>
  173. <p>This is a figure caption.</p>
  174. <p>Here is another paragraph.</p>
  175. ";
  176. tera.add_raw_template(&format!("shortcodes/{}.html", "figure"), shortcode).unwrap();
  177. let config = Config::default();
  178. let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
  179. let res = render_content(markdown_string, &context).unwrap();
  180. println!("{:?}", res);
  181. assert_eq!(res.body, expected);
  182. }
  183. #[test]
  184. fn can_render_several_shortcode_in_row() {
  185. let permalinks_ctx = HashMap::new();
  186. let config = Config::default();
  187. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  188. let res = render_content(
  189. r#"
  190. Hello
  191. {{ youtube(id="ub36ffWAqgQ") }}
  192. {{ youtube(id="ub36ffWAqgQ", autoplay=true) }}
  193. {{ vimeo(id="210073083") }}
  194. {{ streamable(id="c0ic") }}
  195. {{ gist(url="https://gist.github.com/Keats/32d26f699dcc13ebd41b") }}
  196. "#,
  197. &context,
  198. )
  199. .unwrap();
  200. assert!(res.body.contains("<p>Hello</p>\n<div >"));
  201. assert!(res.body.contains(r#"<iframe src="https://www.youtube.com/embed/ub36ffWAqgQ""#));
  202. assert!(res
  203. .body
  204. .contains(r#"<iframe src="https://www.youtube.com/embed/ub36ffWAqgQ?autoplay=1""#));
  205. assert!(res.body.contains(r#"<iframe src="https://www.streamable.com/e/c0ic""#));
  206. assert!(res.body.contains(r#"//player.vimeo.com/video/210073083""#));
  207. }
  208. #[test]
  209. fn doesnt_render_ignored_shortcodes() {
  210. let permalinks_ctx = HashMap::new();
  211. let mut config = Config::default();
  212. config.highlight_code = false;
  213. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  214. let res = render_content(r#"```{{/* youtube(id="w7Ft2ymGmfc") */}}```"#, &context).unwrap();
  215. assert_eq!(res.body, "<p><code>{{ youtube(id=&quot;w7Ft2ymGmfc&quot;) }}</code></p>\n");
  216. }
  217. #[test]
  218. fn can_render_shortcode_with_body() {
  219. let mut tera = Tera::default();
  220. tera.extend(&ZOLA_TERA).unwrap();
  221. tera.add_raw_template(
  222. "shortcodes/quote.html",
  223. "<blockquote>{{ body }} - {{ author }}</blockquote>",
  224. )
  225. .unwrap();
  226. let permalinks_ctx = HashMap::new();
  227. let config = Config::default();
  228. let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
  229. let res = render_content(
  230. r#"
  231. Hello
  232. {% quote(author="Keats") %}
  233. A quote
  234. {% end %}
  235. "#,
  236. &context,
  237. )
  238. .unwrap();
  239. assert_eq!(res.body, "<p>Hello</p>\n<blockquote>A quote - Keats</blockquote>\n");
  240. }
  241. #[test]
  242. fn errors_rendering_unknown_shortcode() {
  243. let tera_ctx = Tera::default();
  244. let permalinks_ctx = HashMap::new();
  245. let config = Config::default();
  246. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  247. let res = render_content("{{ hello(flash=true) }}", &context);
  248. assert!(res.is_err());
  249. }
  250. #[test]
  251. fn can_make_valid_relative_link() {
  252. let mut permalinks = HashMap::new();
  253. permalinks.insert("pages/about.md".to_string(), "https://vincent.is/about".to_string());
  254. let tera_ctx = Tera::default();
  255. let config = Config::default();
  256. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks, InsertAnchor::None);
  257. let res = render_content(
  258. r#"[rel link](@/pages/about.md), [abs link](https://vincent.is/about)"#,
  259. &context,
  260. )
  261. .unwrap();
  262. assert!(
  263. res.body.contains(r#"<p><a href="https://vincent.is/about">rel link</a>, <a href="https://vincent.is/about">abs link</a></p>"#)
  264. );
  265. }
  266. #[test]
  267. fn can_make_relative_links_with_anchors() {
  268. let mut permalinks = HashMap::new();
  269. permalinks.insert("pages/about.md".to_string(), "https://vincent.is/about".to_string());
  270. let tera_ctx = Tera::default();
  271. let config = Config::default();
  272. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks, InsertAnchor::None);
  273. let res = render_content(r#"[rel link](@/pages/about.md#cv)"#, &context).unwrap();
  274. assert!(res.body.contains(r#"<p><a href="https://vincent.is/about#cv">rel link</a></p>"#));
  275. }
  276. #[test]
  277. fn errors_relative_link_inexistant() {
  278. let tera_ctx = Tera::default();
  279. let permalinks_ctx = HashMap::new();
  280. let config = Config::default();
  281. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  282. let res = render_content("[rel link](@/pages/about.md)", &context);
  283. assert!(res.is_err());
  284. }
  285. #[test]
  286. fn can_add_id_to_headings() {
  287. let tera_ctx = Tera::default();
  288. let permalinks_ctx = HashMap::new();
  289. let config = Config::default();
  290. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  291. let res = render_content(r#"# Hello"#, &context).unwrap();
  292. assert_eq!(res.body, "<h1 id=\"hello\">Hello</h1>\n");
  293. }
  294. #[test]
  295. fn can_add_id_to_headings_same_slug() {
  296. let tera_ctx = Tera::default();
  297. let permalinks_ctx = HashMap::new();
  298. let config = Config::default();
  299. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  300. let res = render_content("# Hello\n# Hello", &context).unwrap();
  301. assert_eq!(res.body, "<h1 id=\"hello\">Hello</h1>\n<h1 id=\"hello-1\">Hello</h1>\n");
  302. }
  303. #[test]
  304. fn can_add_non_slug_id_to_headings() {
  305. let tera_ctx = Tera::default();
  306. let permalinks_ctx = HashMap::new();
  307. let mut config = Config::default();
  308. config.slugify.anchors = SlugifyStrategy::Safe;
  309. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  310. let res = render_content(r#"# L'écologie et vous"#, &context).unwrap();
  311. assert_eq!(res.body, "<h1 id=\"L'écologie_et_vous\">L'écologie et vous</h1>\n");
  312. }
  313. #[test]
  314. fn can_handle_manual_ids_on_headings() {
  315. let tera_ctx = Tera::default();
  316. let permalinks_ctx = HashMap::new();
  317. let config = Config::default();
  318. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  319. // Tested things: manual IDs; whitespace flexibility; that automatic IDs avoid collision with
  320. // manual IDs; that duplicates are in fact permitted among manual IDs; that any non-plain-text
  321. // in the middle of `{#…}` will disrupt it from being acknowledged as a manual ID (that last
  322. // one could reasonably be considered a bug rather than a feature, but test it either way); one
  323. // workaround for the improbable case where you actually want `{#…}` at the end of a heading.
  324. let res = render_content(
  325. "\
  326. # Hello\n\
  327. # Hello{#hello}\n\
  328. # Hello {#hello}\n\
  329. # Hello {#Something_else} \n\
  330. # Workaround for literal {#…&#125;\n\
  331. # Hello\n\
  332. # Auto {#*matic*}",
  333. &context,
  334. )
  335. .unwrap();
  336. assert_eq!(
  337. res.body,
  338. "\
  339. <h1 id=\"hello-1\">Hello</h1>\n\
  340. <h1 id=\"hello\">Hello</h1>\n\
  341. <h1 id=\"hello\">Hello</h1>\n\
  342. <h1 id=\"Something_else\">Hello</h1>\n\
  343. <h1 id=\"workaround-for-literal\">Workaround for literal {#…}</h1>\n\
  344. <h1 id=\"hello-2\">Hello</h1>\n\
  345. <h1 id=\"auto-matic\">Auto {#<em>matic</em>}</h1>\n\
  346. "
  347. );
  348. }
  349. #[test]
  350. fn blank_headings() {
  351. let tera_ctx = Tera::default();
  352. let permalinks_ctx = HashMap::new();
  353. let config = Config::default();
  354. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  355. let res = render_content("# \n#\n# {#hmm} \n# {#}", &context).unwrap();
  356. assert_eq!(
  357. res.body,
  358. "<h1 id=\"-1\"></h1>\n<h1 id=\"-2\"></h1>\n<h1 id=\"hmm\"></h1>\n<h1 id=\"\"></h1>\n"
  359. );
  360. }
  361. #[test]
  362. fn can_insert_anchor_left() {
  363. let permalinks_ctx = HashMap::new();
  364. let config = Config::default();
  365. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Left);
  366. let res = render_content("# Hello", &context).unwrap();
  367. assert_eq!(
  368. res.body,
  369. "<h1 id=\"hello\"><a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>Hello</h1>\n"
  370. );
  371. }
  372. #[test]
  373. fn can_insert_anchor_right() {
  374. let permalinks_ctx = HashMap::new();
  375. let config = Config::default();
  376. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Right);
  377. let res = render_content("# Hello", &context).unwrap();
  378. assert_eq!(
  379. res.body,
  380. "<h1 id=\"hello\">Hello<a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a></h1>\n"
  381. );
  382. }
  383. #[test]
  384. fn can_insert_anchor_for_multi_heading() {
  385. let permalinks_ctx = HashMap::new();
  386. let config = Config::default();
  387. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Right);
  388. let res = render_content("# Hello\n# World", &context).unwrap();
  389. assert_eq!(
  390. res.body,
  391. "<h1 id=\"hello\">Hello<a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a></h1>\n\
  392. <h1 id=\"world\">World<a class=\"zola-anchor\" href=\"#world\" aria-label=\"Anchor link for: world\">🔗</a></h1>\n"
  393. );
  394. }
  395. // See https://github.com/Keats/gutenberg/issues/42
  396. #[test]
  397. fn can_insert_anchor_with_exclamation_mark() {
  398. let permalinks_ctx = HashMap::new();
  399. let config = Config::default();
  400. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Left);
  401. let res = render_content("# Hello!", &context).unwrap();
  402. assert_eq!(
  403. res.body,
  404. "<h1 id=\"hello\"><a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>Hello!</h1>\n"
  405. );
  406. }
  407. // See https://github.com/Keats/gutenberg/issues/53
  408. #[test]
  409. fn can_insert_anchor_with_link() {
  410. let permalinks_ctx = HashMap::new();
  411. let config = Config::default();
  412. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Left);
  413. let res = render_content("## [Rust](https://rust-lang.org)", &context).unwrap();
  414. assert_eq!(
  415. res.body,
  416. "<h2 id=\"rust\"><a class=\"zola-anchor\" href=\"#rust\" aria-label=\"Anchor link for: rust\">🔗</a><a href=\"https://rust-lang.org\">Rust</a></h2>\n"
  417. );
  418. }
  419. #[test]
  420. fn can_insert_anchor_with_other_special_chars() {
  421. let permalinks_ctx = HashMap::new();
  422. let config = Config::default();
  423. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Left);
  424. let res = render_content("# Hello*_()", &context).unwrap();
  425. assert_eq!(
  426. res.body,
  427. "<h1 id=\"hello\"><a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>Hello*_()</h1>\n"
  428. );
  429. }
  430. #[test]
  431. fn can_make_toc() {
  432. let permalinks_ctx = HashMap::new();
  433. let config = Config::default();
  434. let context = RenderContext::new(
  435. &ZOLA_TERA,
  436. &config,
  437. "https://mysite.com/something",
  438. &permalinks_ctx,
  439. InsertAnchor::Left,
  440. );
  441. let res = render_content(
  442. r#"
  443. # Heading 1
  444. ## Heading 2
  445. ## Another Heading 2
  446. ### Last one
  447. "#,
  448. &context,
  449. )
  450. .unwrap();
  451. let toc = res.toc;
  452. assert_eq!(toc.len(), 1);
  453. assert_eq!(toc[0].children.len(), 2);
  454. assert_eq!(toc[0].children[1].children.len(), 1);
  455. }
  456. #[test]
  457. fn can_ignore_tags_in_toc() {
  458. let permalinks_ctx = HashMap::new();
  459. let config = Config::default();
  460. let context = RenderContext::new(
  461. &ZOLA_TERA,
  462. &config,
  463. "https://mysite.com/something",
  464. &permalinks_ctx,
  465. InsertAnchor::Left,
  466. );
  467. let res = render_content(
  468. r#"
  469. ## heading with `code`
  470. ## [anchor](https://duckduckgo.com/) in heading
  471. ## **bold** and *italics*
  472. "#,
  473. &context,
  474. )
  475. .unwrap();
  476. let toc = res.toc;
  477. assert_eq!(toc[0].id, "heading-with-code");
  478. assert_eq!(toc[0].title, "heading with code");
  479. assert_eq!(toc[1].id, "anchor-in-heading");
  480. assert_eq!(toc[1].title, "anchor in heading");
  481. assert_eq!(toc[2].id, "bold-and-italics");
  482. assert_eq!(toc[2].title, "bold and italics");
  483. }
  484. #[test]
  485. fn can_understand_backtick_in_titles() {
  486. let permalinks_ctx = HashMap::new();
  487. let config = Config::default();
  488. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  489. let res = render_content("# `Hello`", &context).unwrap();
  490. assert_eq!(res.body, "<h1 id=\"hello\"><code>Hello</code></h1>\n");
  491. }
  492. #[test]
  493. fn can_understand_backtick_in_paragraphs() {
  494. let permalinks_ctx = HashMap::new();
  495. let config = Config::default();
  496. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  497. let res = render_content("Hello `world`", &context).unwrap();
  498. assert_eq!(res.body, "<p>Hello <code>world</code></p>\n");
  499. }
  500. // https://github.com/Keats/gutenberg/issues/297
  501. #[test]
  502. fn can_understand_links_in_heading() {
  503. let permalinks_ctx = HashMap::new();
  504. let config = Config::default();
  505. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  506. let res = render_content("# [Rust](https://rust-lang.org)", &context).unwrap();
  507. assert_eq!(res.body, "<h1 id=\"rust\"><a href=\"https://rust-lang.org\">Rust</a></h1>\n");
  508. }
  509. #[test]
  510. fn can_understand_link_with_title_in_heading() {
  511. let permalinks_ctx = HashMap::new();
  512. let config = Config::default();
  513. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  514. let res =
  515. render_content("# [Rust](https://rust-lang.org \"Rust homepage\")", &context).unwrap();
  516. assert_eq!(
  517. res.body,
  518. "<h1 id=\"rust\"><a href=\"https://rust-lang.org\" title=\"Rust homepage\">Rust</a></h1>\n"
  519. );
  520. }
  521. #[test]
  522. fn can_understand_emphasis_in_heading() {
  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("# *Emphasis* text", &context).unwrap();
  527. assert_eq!(res.body, "<h1 id=\"emphasis-text\"><em>Emphasis</em> text</h1>\n");
  528. }
  529. #[test]
  530. fn can_understand_strong_in_heading() {
  531. let permalinks_ctx = HashMap::new();
  532. let config = Config::default();
  533. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  534. let res = render_content("# **Strong** text", &context).unwrap();
  535. assert_eq!(res.body, "<h1 id=\"strong-text\"><strong>Strong</strong> text</h1>\n");
  536. }
  537. #[test]
  538. fn can_understand_code_in_heading() {
  539. let permalinks_ctx = HashMap::new();
  540. let config = Config::default();
  541. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  542. let res = render_content("# `Code` text", &context).unwrap();
  543. assert_eq!(res.body, "<h1 id=\"code-text\"><code>Code</code> text</h1>\n");
  544. }
  545. // See https://github.com/getzola/zola/issues/569
  546. #[test]
  547. fn can_understand_footnote_in_heading() {
  548. let permalinks_ctx = HashMap::new();
  549. let config = Config::default();
  550. let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
  551. let res = render_content("# text [^1] there\n[^1]: footnote", &context).unwrap();
  552. assert_eq!(
  553. res.body,
  554. r##"<h1 id="text-there">text <sup class="footnote-reference"><a href="#1">1</a></sup> there</h1>
  555. <div class="footnote-definition" id="1"><sup class="footnote-definition-label">1</sup>
  556. <p>footnote</p>
  557. </div>
  558. "##
  559. );
  560. }
  561. #[test]
  562. fn can_make_valid_relative_link_in_heading() {
  563. let mut permalinks = HashMap::new();
  564. permalinks.insert("pages/about.md".to_string(), "https://vincent.is/about/".to_string());
  565. let tera_ctx = Tera::default();
  566. let config = Config::default();
  567. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks, InsertAnchor::None);
  568. let res = render_content(r#" # [rel link](@/pages/about.md)"#, &context).unwrap();
  569. assert_eq!(
  570. res.body,
  571. "<h1 id=\"rel-link\"><a href=\"https://vincent.is/about/\">rel link</a></h1>\n"
  572. );
  573. }
  574. #[test]
  575. fn can_make_permalinks_with_colocated_assets_for_link() {
  576. let permalinks_ctx = HashMap::new();
  577. let config = Config::default();
  578. let context = RenderContext::new(
  579. &ZOLA_TERA,
  580. &config,
  581. "https://vincent.is/about/",
  582. &permalinks_ctx,
  583. InsertAnchor::None,
  584. );
  585. let res = render_content("[an image](image.jpg)", &context).unwrap();
  586. assert_eq!(res.body, "<p><a href=\"https://vincent.is/about/image.jpg\">an image</a></p>\n");
  587. }
  588. #[test]
  589. fn can_make_permalinks_with_colocated_assets_for_image() {
  590. let permalinks_ctx = HashMap::new();
  591. let config = Config::default();
  592. let context = RenderContext::new(
  593. &ZOLA_TERA,
  594. &config,
  595. "https://vincent.is/about/",
  596. &permalinks_ctx,
  597. InsertAnchor::None,
  598. );
  599. let res = render_content("![alt text](image.jpg)", &context).unwrap();
  600. assert_eq!(
  601. res.body,
  602. "<p><img src=\"https://vincent.is/about/image.jpg\" alt=\"alt text\" /></p>\n"
  603. );
  604. }
  605. #[test]
  606. fn markdown_doesnt_wrap_html_in_paragraph() {
  607. let permalinks_ctx = HashMap::new();
  608. let config = Config::default();
  609. let context = RenderContext::new(
  610. &ZOLA_TERA,
  611. &config,
  612. "https://vincent.is/about/",
  613. &permalinks_ctx,
  614. InsertAnchor::None,
  615. );
  616. let res = render_content(
  617. r#"
  618. Some text
  619. <h1>Helo</h1>
  620. <div>
  621. <a href="mobx-flow.png">
  622. <img src="mobx-flow.png" alt="MobX flow">
  623. </a>
  624. </div>
  625. "#,
  626. &context,
  627. )
  628. .unwrap();
  629. assert_eq!(
  630. res.body,
  631. "<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"
  632. );
  633. }
  634. #[test]
  635. fn correctly_captures_external_links() {
  636. let permalinks_ctx = HashMap::new();
  637. let config = Config::default();
  638. let context = RenderContext::new(
  639. &ZOLA_TERA,
  640. &config,
  641. "https://vincent.is/about/",
  642. &permalinks_ctx,
  643. InsertAnchor::None,
  644. );
  645. let content = "
  646. [a link](http://google.com)
  647. [a link](http://google.comy)
  648. Email: [foo@bar.baz](mailto:foo@bar.baz)
  649. Email: <foo@bar.baz>
  650. ";
  651. let res = render_content(content, &context).unwrap();
  652. assert_eq!(
  653. res.external_links,
  654. &["http://google.com".to_owned(), "http://google.comy".to_owned()]
  655. );
  656. }
  657. #[test]
  658. fn can_handle_summaries() {
  659. let tera_ctx = Tera::default();
  660. let permalinks_ctx = HashMap::new();
  661. let config = Config::default();
  662. let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
  663. let res = render_content(
  664. r#"
  665. Hello [My site][world]
  666. <!-- more -->
  667. Bla bla
  668. [world]: https://vincentprouillet.com
  669. "#,
  670. &context,
  671. )
  672. .unwrap();
  673. assert_eq!(
  674. res.body,
  675. "<p>Hello <a href=\"https://vincentprouillet.com\">My site</a></p>\n<span id=\"continue-reading\"></span>\n<p>Bla bla</p>\n"
  676. );
  677. assert_eq!(
  678. res.summary_len,
  679. Some("<p>Hello <a href=\"https://vincentprouillet.com/\">My site</a></p>".len())
  680. );
  681. }
  682. // https://github.com/Keats/gutenberg/issues/522
  683. #[test]
  684. fn doesnt_try_to_highlight_content_from_shortcode() {
  685. let permalinks_ctx = HashMap::new();
  686. let mut tera = Tera::default();
  687. tera.extend(&ZOLA_TERA).unwrap();
  688. let shortcode = r#"
  689. <figure>
  690. {% if width %}
  691. <img src="/images/{{ src }}" alt="{{ caption }}" width="{{ width }}" />
  692. {% else %}
  693. <img src="/images/{{ src }}" alt="{{ caption }}" />
  694. {% endif %}
  695. <figcaption>{{ caption }}</figcaption>
  696. </figure>"#;
  697. let markdown_string = r#"{{ figure(src="spherecluster.png", caption="Some spheres.") }}"#;
  698. let expected = r#"<figure>
  699. <img src="/images/spherecluster.png" alt="Some spheres." />
  700. <figcaption>Some spheres.</figcaption>
  701. </figure>"#;
  702. tera.add_raw_template(&format!("shortcodes/{}.html", "figure"), shortcode).unwrap();
  703. let config = Config::default();
  704. let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
  705. let res = render_content(markdown_string, &context).unwrap();
  706. assert_eq!(res.body, expected);
  707. }
  708. // TODO: re-enable once it's fixed in Tera
  709. // https://github.com/Keats/tera/issues/373
  710. //#[test]
  711. //fn can_split_lines_shortcode_body() {
  712. // let permalinks_ctx = HashMap::new();
  713. // let mut tera = Tera::default();
  714. // tera.extend(&ZOLA_TERA).unwrap();
  715. //
  716. // let shortcode = r#"{{ body | split(pat="\n") }}"#;
  717. //
  718. // let markdown_string = r#"
  719. //{% alert() %}
  720. //multi
  721. //ple
  722. //lines
  723. //{% end %}
  724. // "#;
  725. //
  726. // let expected = r#"<p>["multi", "ple", "lines"]</p>"#;
  727. //
  728. // tera.add_raw_template(&format!("shortcodes/{}.html", "alert"), shortcode).unwrap();
  729. // let config = Config::default();
  730. // let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
  731. //
  732. // let res = render_content(markdown_string, &context).unwrap();
  733. // assert_eq!(res.body, expected);
  734. //}
  735. // https://github.com/getzola/zola/issues/747
  736. // https://github.com/getzola/zola/issues/816
  737. #[test]
  738. fn leaves_custom_url_scheme_untouched() {
  739. let content = r#"[foo@bar.tld](xmpp:foo@bar.tld)
  740. [(123) 456-7890](tel:+11234567890)
  741. [blank page](about:blank)
  742. "#;
  743. let tera_ctx = Tera::default();
  744. let config = Config::default();
  745. let permalinks_ctx = HashMap::new();
  746. let context = RenderContext::new(
  747. &tera_ctx,
  748. &config,
  749. "https://vincent.is/",
  750. &permalinks_ctx,
  751. InsertAnchor::None,
  752. );
  753. let res = render_content(content, &context).unwrap();
  754. let expected = r#"<p><a href="xmpp:foo@bar.tld">foo@bar.tld</a></p>
  755. <p><a href="tel:+11234567890">(123) 456-7890</a></p>
  756. <p><a href="about:blank">blank page</a></p>
  757. "#;
  758. assert_eq!(res.body, expected);
  759. }
  760. #[test]
  761. fn stops_with_an_error_on_an_empty_link() {
  762. let content = r#"[some link]()"#;
  763. let tera_ctx = Tera::default();
  764. let config = Config::default();
  765. let permalinks_ctx = HashMap::new();
  766. let context = RenderContext::new(
  767. &tera_ctx,
  768. &config,
  769. "https://vincent.is/",
  770. &permalinks_ctx,
  771. InsertAnchor::None,
  772. );
  773. let res = render_content(content, &context);
  774. let expected = "There is a link that is missing a URL";
  775. assert!(res.is_err());
  776. assert_eq!(res.unwrap_err().to_string(), expected);
  777. }