zola/components/rendering/tests/markdown.rs

805 lines
26 KiB
Rust
Raw Normal View History

2018-10-31 07:18:57 +00:00
extern crate config;
2017-07-04 12:27:32 +00:00
extern crate front_matter;
extern crate rendering;
2018-10-31 07:18:57 +00:00
extern crate templates;
extern crate tera;
2017-07-04 12:27:32 +00:00
use std::collections::HashMap;
use tera::Tera;
2018-05-06 20:58:39 +00:00
use config::Config;
2017-07-04 12:27:32 +00:00
use front_matter::InsertAnchor;
2018-10-31 07:18:57 +00:00
use rendering::{render_content, RenderContext};
use templates::ZOLA_TERA;
2017-07-04 12:27:32 +00:00
#[test]
2018-05-06 20:58:39 +00:00
fn can_do_render_content_simple() {
2017-07-04 12:27:32 +00:00
let tera_ctx = Tera::default();
let permalinks_ctx = HashMap::new();
2018-05-06 20:58:39 +00:00
let config = Config::default();
2018-10-09 12:33:43 +00:00
let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
2018-05-06 20:58:39 +00:00
let res = render_content("hello", &context).unwrap();
2018-08-24 21:37:55 +00:00
assert_eq!(res.body, "<p>hello</p>\n");
2017-07-04 12:27:32 +00:00
}
#[test]
fn doesnt_highlight_code_block_with_highlighting_off() {
let tera_ctx = Tera::default();
let permalinks_ctx = HashMap::new();
2018-05-06 20:58:39 +00:00
let mut config = Config::default();
config.highlight_code = false;
2018-10-09 12:33:43 +00:00
let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
2018-05-06 20:58:39 +00:00
let res = render_content("```\n$ gutenberg server\n```", &context).unwrap();
2018-10-31 07:18:57 +00:00
assert_eq!(res.body, "<pre><code>$ gutenberg server\n</code></pre>\n");
2017-07-04 12:27:32 +00:00
}
#[test]
fn can_highlight_code_block_no_lang() {
let tera_ctx = Tera::default();
let permalinks_ctx = HashMap::new();
let mut config = Config::default();
config.highlight_code = true;
2018-10-09 12:33:43 +00:00
let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
2018-05-06 20:58:39 +00:00
let res = render_content("```\n$ gutenberg server\n$ ping\n```", &context).unwrap();
2017-07-04 12:27:32 +00:00
assert_eq!(
2018-08-24 21:37:55 +00:00
res.body,
2018-10-09 12:33:43 +00:00
"<pre style=\"background-color:#2b303b;\">\n<span style=\"color:#c0c5ce;\">$ gutenberg server\n</span><span style=\"color:#c0c5ce;\">$ ping\n</span></pre>"
2017-07-04 12:27:32 +00:00
);
}
#[test]
fn can_highlight_code_block_with_lang() {
let tera_ctx = Tera::default();
let permalinks_ctx = HashMap::new();
let mut config = Config::default();
config.highlight_code = true;
2018-10-09 12:33:43 +00:00
let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
2018-05-06 20:58:39 +00:00
let res = render_content("```python\nlist.append(1)\n```", &context).unwrap();
2017-07-04 12:27:32 +00:00
assert_eq!(
2018-08-24 21:37:55 +00:00
res.body,
2018-10-09 12:33:43 +00:00
"<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>"
2017-07-04 12:27:32 +00:00
);
}
#[test]
fn can_higlight_code_block_with_unknown_lang() {
let tera_ctx = Tera::default();
let permalinks_ctx = HashMap::new();
let mut config = Config::default();
config.highlight_code = true;
2018-10-09 12:33:43 +00:00
let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
2018-05-06 20:58:39 +00:00
let res = render_content("```yolo\nlist.append(1)\n```", &context).unwrap();
2017-07-04 12:27:32 +00:00
// defaults to plain text
assert_eq!(
2018-08-24 21:37:55 +00:00
res.body,
2018-10-09 12:33:43 +00:00
"<pre style=\"background-color:#2b303b;\">\n<span style=\"color:#c0c5ce;\">list.append(1)\n</span></pre>"
2017-07-04 12:27:32 +00:00
);
}
#[test]
fn can_render_shortcode() {
let permalinks_ctx = HashMap::new();
2018-05-06 20:58:39 +00:00
let config = Config::default();
let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
2018-10-31 07:18:57 +00:00
let res = render_content(
r#"
2017-07-04 12:27:32 +00:00
Hello
{{ youtube(id="ub36ffWAqgQ") }}
2018-10-31 07:18:57 +00:00
"#,
&context,
)
.unwrap();
2018-08-24 21:37:55 +00:00
assert!(res.body.contains("<p>Hello</p>\n<div >"));
assert!(res.body.contains(r#"<iframe src="https://www.youtube.com/embed/ub36ffWAqgQ""#));
2017-07-04 12:27:32 +00:00
}
#[test]
fn can_render_shortcode_with_markdown_char_in_args_name() {
let permalinks_ctx = HashMap::new();
2018-05-06 20:58:39 +00:00
let config = Config::default();
let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
2018-10-31 07:18:57 +00:00
let input = vec!["name", "na_me", "n_a_me", "n1"];
for i in input {
2018-10-31 07:18:57 +00:00
let res =
render_content(&format!("{{{{ youtube(id=\"hey\", {}=1) }}}}", i), &context).unwrap();
2018-08-24 21:37:55 +00:00
assert!(res.body.contains(r#"<iframe src="https://www.youtube.com/embed/hey""#));
}
}
#[test]
fn can_render_shortcode_with_markdown_char_in_args_value() {
let permalinks_ctx = HashMap::new();
2018-05-06 20:58:39 +00:00
let config = Config::default();
let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
let input = vec![
"ub36ffWAqgQ-hey",
"ub36ffWAqgQ_hey",
"ub36ffWAqgQ_he_y",
"ub36ffWAqgQ*hey",
"ub36ffWAqgQ#hey",
];
for i in input {
2018-05-06 20:58:39 +00:00
let res = render_content(&format!("{{{{ youtube(id=\"{}\") }}}}", i), &context).unwrap();
2018-11-14 16:34:21 +00:00
assert!(res
.body
.contains(&format!(r#"<iframe src="https://www.youtube.com/embed/{}""#, i)));
}
}
#[test]
fn can_render_body_shortcode_with_markdown_char_in_name() {
let permalinks_ctx = HashMap::new();
let mut tera = Tera::default();
tera.extend(&ZOLA_TERA).unwrap();
2018-10-31 07:18:57 +00:00
let input = vec!["quo_te", "qu_o_te"];
2018-05-06 20:58:39 +00:00
let config = Config::default();
for i in input {
2018-10-31 07:18:57 +00:00
tera.add_raw_template(
&format!("shortcodes/{}.html", i),
"<blockquote>{{ body }} - {{ author}}</blockquote>",
)
.unwrap();
2018-10-09 12:33:43 +00:00
let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
2018-10-31 07:18:57 +00:00
let res =
render_content(&format!("{{% {}(author=\"Bob\") %}}\nhey\n{{% end %}}", i), &context)
.unwrap();
println!("{:?}", res);
2018-08-24 21:37:55 +00:00
assert!(res.body.contains("<blockquote>hey - Bob</blockquote>"));
}
}
#[test]
fn can_render_body_shortcode_and_paragraph_after() {
let permalinks_ctx = HashMap::new();
let mut tera = Tera::default();
tera.extend(&ZOLA_TERA).unwrap();
let shortcode = "<p>{{ body }}</p>";
let markdown_string = r#"
{% figure() %}
This is a figure caption.
{% end %}
Here is another paragraph.
"#;
let expected = "<p>This is a figure caption.</p>
<p>Here is another paragraph.</p>
";
tera.add_raw_template(&format!("shortcodes/{}.html", "figure"), shortcode).unwrap();
2018-05-06 20:58:39 +00:00
let config = Config::default();
2018-10-09 12:33:43 +00:00
let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
2018-05-06 20:58:39 +00:00
let res = render_content(markdown_string, &context).unwrap();
println!("{:?}", res);
2018-08-24 21:37:55 +00:00
assert_eq!(res.body, expected);
}
#[test]
fn can_render_two_body_shortcode_and_paragraph_after_with_line_break_between() {
let permalinks_ctx = HashMap::new();
let mut tera = Tera::default();
tera.extend(&ZOLA_TERA).unwrap();
let shortcode = "<p>{{ body }}</p>";
let markdown_string = r#"
{% figure() %}
This is a figure caption.
{% end %}
{% figure() %}
This is a figure caption.
{% end %}
Here is another paragraph.
"#;
let expected = "<p>This is a figure caption.</p>
<p>This is a figure caption.</p>
<p>Here is another paragraph.</p>
";
tera.add_raw_template(&format!("shortcodes/{}.html", "figure"), shortcode).unwrap();
2018-05-06 20:58:39 +00:00
let config = Config::default();
2018-10-09 12:33:43 +00:00
let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
2018-05-06 20:58:39 +00:00
let res = render_content(markdown_string, &context).unwrap();
println!("{:?}", res);
2018-08-24 21:37:55 +00:00
assert_eq!(res.body, expected);
}
2017-07-04 12:27:32 +00:00
#[test]
fn can_render_several_shortcode_in_row() {
let permalinks_ctx = HashMap::new();
2018-05-06 20:58:39 +00:00
let config = Config::default();
let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
2018-10-31 07:18:57 +00:00
let res = render_content(
r#"
2017-07-04 12:27:32 +00:00
Hello
{{ youtube(id="ub36ffWAqgQ") }}
{{ youtube(id="ub36ffWAqgQ", autoplay=true) }}
{{ vimeo(id="210073083") }}
{{ streamable(id="c0ic") }}
{{ gist(url="https://gist.github.com/Keats/32d26f699dcc13ebd41b") }}
2018-10-31 07:18:57 +00:00
"#,
&context,
)
.unwrap();
2018-08-24 21:37:55 +00:00
assert!(res.body.contains("<p>Hello</p>\n<div >"));
assert!(res.body.contains(r#"<iframe src="https://www.youtube.com/embed/ub36ffWAqgQ""#));
2018-11-14 16:34:21 +00:00
assert!(res
.body
.contains(r#"<iframe src="https://www.youtube.com/embed/ub36ffWAqgQ?autoplay=1""#));
2018-08-24 21:37:55 +00:00
assert!(res.body.contains(r#"<iframe src="https://www.streamable.com/e/c0ic""#));
assert!(res.body.contains(r#"//player.vimeo.com/video/210073083""#));
2017-07-04 12:27:32 +00:00
}
2017-10-23 13:54:32 +00:00
#[test]
2018-05-06 20:58:39 +00:00
fn doesnt_render_ignored_shortcodes() {
2017-10-23 13:54:32 +00:00
let permalinks_ctx = HashMap::new();
2018-05-06 20:58:39 +00:00
let mut config = Config::default();
config.highlight_code = false;
let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
2018-05-06 20:58:39 +00:00
let res = render_content(r#"```{{/* youtube(id="w7Ft2ymGmfc") */}}```"#, &context).unwrap();
2018-08-24 21:37:55 +00:00
assert_eq!(res.body, "<p><code>{{ youtube(id=&quot;w7Ft2ymGmfc&quot;) }}</code></p>\n");
2017-07-04 12:27:32 +00:00
}
#[test]
fn can_render_shortcode_with_body() {
let mut tera = Tera::default();
tera.extend(&ZOLA_TERA).unwrap();
2018-10-31 07:18:57 +00:00
tera.add_raw_template(
"shortcodes/quote.html",
"<blockquote>{{ body }} - {{ author }}</blockquote>",
)
.unwrap();
2017-07-04 12:27:32 +00:00
let permalinks_ctx = HashMap::new();
2018-05-06 20:58:39 +00:00
let config = Config::default();
2018-10-09 12:33:43 +00:00
let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
2017-07-04 12:27:32 +00:00
2018-10-31 07:18:57 +00:00
let res = render_content(
r#"
2017-07-04 12:27:32 +00:00
Hello
{% quote(author="Keats") %}
A quote
{% end %}
2018-10-31 07:18:57 +00:00
"#,
&context,
)
.unwrap();
2018-08-24 21:37:55 +00:00
assert_eq!(res.body, "<p>Hello</p>\n<blockquote>A quote - Keats</blockquote>\n");
2017-07-04 12:27:32 +00:00
}
#[test]
fn errors_rendering_unknown_shortcode() {
let tera_ctx = Tera::default();
let permalinks_ctx = HashMap::new();
2018-05-06 20:58:39 +00:00
let config = Config::default();
2018-10-09 12:33:43 +00:00
let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
2018-05-06 20:58:39 +00:00
let res = render_content("{{ hello(flash=true) }}", &context);
2017-07-04 12:27:32 +00:00
assert!(res.is_err());
}
#[test]
fn can_make_valid_relative_link() {
let mut permalinks = HashMap::new();
permalinks.insert("pages/about.md".to_string(), "https://vincent.is/about".to_string());
let tera_ctx = Tera::default();
2018-05-06 20:58:39 +00:00
let config = Config::default();
2018-10-09 12:33:43 +00:00
let context = RenderContext::new(&tera_ctx, &config, "", &permalinks, InsertAnchor::None);
2018-05-06 20:58:39 +00:00
let res = render_content(
2017-07-04 12:27:32 +00:00
r#"[rel link](./pages/about.md), [abs link](https://vincent.is/about)"#,
&context,
2018-10-31 07:18:57 +00:00
)
.unwrap();
2017-07-04 12:27:32 +00:00
assert!(
2018-08-24 21:37:55 +00:00
res.body.contains(r#"<p><a href="https://vincent.is/about">rel link</a>, <a href="https://vincent.is/about">abs link</a></p>"#)
2017-07-04 12:27:32 +00:00
);
}
#[test]
fn can_make_relative_links_with_anchors() {
let mut permalinks = HashMap::new();
permalinks.insert("pages/about.md".to_string(), "https://vincent.is/about".to_string());
let tera_ctx = Tera::default();
2018-05-06 20:58:39 +00:00
let config = Config::default();
2018-10-09 12:33:43 +00:00
let context = RenderContext::new(&tera_ctx, &config, "", &permalinks, InsertAnchor::None);
2018-05-06 20:58:39 +00:00
let res = render_content(r#"[rel link](./pages/about.md#cv)"#, &context).unwrap();
2017-07-04 12:27:32 +00:00
2018-10-31 07:18:57 +00:00
assert!(res.body.contains(r#"<p><a href="https://vincent.is/about#cv">rel link</a></p>"#));
2017-07-04 12:27:32 +00:00
}
#[test]
fn errors_relative_link_inexistant() {
let tera_ctx = Tera::default();
let permalinks_ctx = HashMap::new();
2018-05-06 20:58:39 +00:00
let config = Config::default();
2018-10-09 12:33:43 +00:00
let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
2018-05-06 20:58:39 +00:00
let res = render_content("[rel link](./pages/about.md)", &context);
2017-07-04 12:27:32 +00:00
assert!(res.is_err());
}
#[test]
fn can_add_id_to_headers() {
let tera_ctx = Tera::default();
let permalinks_ctx = HashMap::new();
2018-05-06 20:58:39 +00:00
let config = Config::default();
2018-10-09 12:33:43 +00:00
let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
2018-05-06 20:58:39 +00:00
let res = render_content(r#"# Hello"#, &context).unwrap();
2018-08-24 21:37:55 +00:00
assert_eq!(res.body, "<h1 id=\"hello\">Hello</h1>\n");
2017-07-04 12:27:32 +00:00
}
#[test]
fn can_add_id_to_headers_same_slug() {
let tera_ctx = Tera::default();
let permalinks_ctx = HashMap::new();
2018-05-06 20:58:39 +00:00
let config = Config::default();
2018-10-09 12:33:43 +00:00
let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
2018-05-06 20:58:39 +00:00
let res = render_content("# Hello\n# Hello", &context).unwrap();
2018-08-24 21:37:55 +00:00
assert_eq!(res.body, "<h1 id=\"hello\">Hello</h1>\n<h1 id=\"hello-1\">Hello</h1>\n");
2017-07-04 12:27:32 +00:00
}
#[test]
fn can_insert_anchor_left() {
let permalinks_ctx = HashMap::new();
2018-05-06 20:58:39 +00:00
let config = Config::default();
let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Left);
2018-05-06 20:58:39 +00:00
let res = render_content("# Hello", &context).unwrap();
2017-07-04 12:27:32 +00:00
assert_eq!(
2018-08-24 21:37:55 +00:00
res.body,
"<h1 id=\"hello\"><a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>\nHello</h1>\n"
2017-07-04 12:27:32 +00:00
);
}
#[test]
fn can_insert_anchor_right() {
let permalinks_ctx = HashMap::new();
2018-05-06 20:58:39 +00:00
let config = Config::default();
let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Right);
2018-05-06 20:58:39 +00:00
let res = render_content("# Hello", &context).unwrap();
2017-07-04 12:27:32 +00:00
assert_eq!(
2018-08-24 21:37:55 +00:00
res.body,
"<h1 id=\"hello\">Hello<a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>\n</h1>\n"
2017-07-04 12:27:32 +00:00
);
}
#[test]
fn can_insert_anchor_for_multi_header() {
let permalinks_ctx = HashMap::new();
let config = Config::default();
let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Right);
let res = render_content("# Hello\n# World", &context).unwrap();
assert_eq!(
res.body,
"<h1 id=\"hello\">Hello<a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>\n</h1>\n\
<h1 id=\"world\">World<a class=\"zola-anchor\" href=\"#world\" aria-label=\"Anchor link for: world\">🔗</a>\n</h1>\n"
);
}
2017-07-04 12:27:32 +00:00
// See https://github.com/Keats/gutenberg/issues/42
#[test]
fn can_insert_anchor_with_exclamation_mark() {
let permalinks_ctx = HashMap::new();
2018-05-06 20:58:39 +00:00
let config = Config::default();
let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Left);
2018-05-06 20:58:39 +00:00
let res = render_content("# Hello!", &context).unwrap();
2017-07-04 12:27:32 +00:00
assert_eq!(
2018-08-24 21:37:55 +00:00
res.body,
"<h1 id=\"hello\"><a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>\nHello!</h1>\n"
2017-07-04 12:27:32 +00:00
);
}
// See https://github.com/Keats/gutenberg/issues/53
#[test]
fn can_insert_anchor_with_link() {
let permalinks_ctx = HashMap::new();
2018-05-06 20:58:39 +00:00
let config = Config::default();
let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Left);
2018-05-07 16:38:04 +00:00
let res = render_content("## [Rust](https://rust-lang.org)", &context).unwrap();
2017-07-04 12:27:32 +00:00
assert_eq!(
2018-08-24 21:37:55 +00:00
res.body,
"<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"
2017-07-04 12:27:32 +00:00
);
}
#[test]
fn can_insert_anchor_with_other_special_chars() {
let permalinks_ctx = HashMap::new();
2018-05-06 20:58:39 +00:00
let config = Config::default();
let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::Left);
2018-05-06 20:58:39 +00:00
let res = render_content("# Hello*_()", &context).unwrap();
2017-07-04 12:27:32 +00:00
assert_eq!(
2018-08-24 21:37:55 +00:00
res.body,
"<h1 id=\"hello\"><a class=\"zola-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>\nHello*_()</h1>\n"
2017-07-04 12:27:32 +00:00
);
}
#[test]
fn can_make_toc() {
let permalinks_ctx = HashMap::new();
2018-05-06 20:58:39 +00:00
let config = Config::default();
let context = RenderContext::new(
&ZOLA_TERA,
2018-05-06 20:58:39 +00:00
&config,
2017-07-04 12:27:32 +00:00
"https://mysite.com/something",
&permalinks_ctx,
InsertAnchor::Left,
2017-07-04 12:27:32 +00:00
);
2018-10-31 07:18:57 +00:00
let res = render_content(
r#"
2017-07-04 12:27:32 +00:00
# Header 1
## Header 2
## Another Header 2
### Last one
2018-10-31 07:18:57 +00:00
"#,
&context,
)
.unwrap();
2017-07-04 12:27:32 +00:00
2018-08-24 21:37:55 +00:00
let toc = res.toc;
2017-07-04 12:27:32 +00:00
assert_eq!(toc.len(), 1);
assert_eq!(toc[0].children.len(), 2);
assert_eq!(toc[0].children[1].children.len(), 1);
}
2017-09-27 14:09:13 +00:00
#[test]
fn can_ignore_tags_in_toc() {
let permalinks_ctx = HashMap::new();
let config = Config::default();
let context = RenderContext::new(
&ZOLA_TERA,
&config,
"https://mysite.com/something",
&permalinks_ctx,
InsertAnchor::Left,
);
2018-10-31 07:18:57 +00:00
let res = render_content(
r#"
## header with `code`
## [anchor](https://duckduckgo.com/) in header
## **bold** and *italics*
2018-10-31 07:18:57 +00:00
"#,
&context,
)
.unwrap();
let toc = res.toc;
assert_eq!(toc[0].id, "header-with-code");
assert_eq!(toc[0].title, "header with code");
assert_eq!(toc[1].id, "anchor-in-header");
assert_eq!(toc[1].title, "anchor in header");
assert_eq!(toc[2].id, "bold-and-italics");
assert_eq!(toc[2].title, "bold and italics");
}
2017-09-27 14:09:13 +00:00
#[test]
fn can_understand_backtick_in_titles() {
let permalinks_ctx = HashMap::new();
2018-05-06 20:58:39 +00:00
let config = Config::default();
let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
2018-05-06 20:58:39 +00:00
let res = render_content("# `Hello`", &context).unwrap();
2018-10-31 07:18:57 +00:00
assert_eq!(res.body, "<h1 id=\"hello\"><code>Hello</code></h1>\n");
2017-09-27 14:09:13 +00:00
}
#[test]
fn can_understand_backtick_in_paragraphs() {
let permalinks_ctx = HashMap::new();
2018-05-06 20:58:39 +00:00
let config = Config::default();
let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
2018-05-06 20:58:39 +00:00
let res = render_content("Hello `world`", &context).unwrap();
2018-10-31 07:18:57 +00:00
assert_eq!(res.body, "<p>Hello <code>world</code></p>\n");
2017-09-27 14:09:13 +00:00
}
2018-05-07 16:38:04 +00:00
// https://github.com/Keats/gutenberg/issues/297
#[test]
fn can_understand_links_in_header() {
let permalinks_ctx = HashMap::new();
let config = Config::default();
let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
2018-05-07 16:38:04 +00:00
let res = render_content("# [Rust](https://rust-lang.org)", &context).unwrap();
2018-10-31 07:18:57 +00:00
assert_eq!(res.body, "<h1 id=\"rust\"><a href=\"https://rust-lang.org\">Rust</a></h1>\n");
2018-05-07 16:38:04 +00:00
}
#[test]
fn can_understand_link_with_title_in_header() {
let permalinks_ctx = HashMap::new();
let config = Config::default();
let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
2018-10-31 07:18:57 +00:00
let res =
render_content("# [Rust](https://rust-lang.org \"Rust homepage\")", &context).unwrap();
2018-05-07 16:38:04 +00:00
assert_eq!(
2018-08-24 21:37:55 +00:00
res.body,
2018-05-07 16:38:04 +00:00
"<h1 id=\"rust\"><a href=\"https://rust-lang.org\" title=\"Rust homepage\">Rust</a></h1>\n"
);
}
2018-05-07 19:03:51 +00:00
#[test]
fn can_understand_emphasis_in_header() {
let permalinks_ctx = HashMap::new();
let config = Config::default();
let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
let res = render_content("# *Emphasis* text", &context).unwrap();
assert_eq!(res.body, "<h1 id=\"emphasis-text\"><em>Emphasis</em> text</h1>\n");
}
#[test]
fn can_understand_strong_in_header() {
let permalinks_ctx = HashMap::new();
let config = Config::default();
let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
let res = render_content("# **Strong** text", &context).unwrap();
assert_eq!(res.body, "<h1 id=\"strong-text\"><strong>Strong</strong> text</h1>\n");
}
#[test]
fn can_understand_code_in_header() {
let permalinks_ctx = HashMap::new();
let config = Config::default();
let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
let res = render_content("# `Code` text", &context).unwrap();
assert_eq!(res.body, "<h1 id=\"code-text\"><code>Code</code> text</h1>\n");
}
// See https://github.com/getzola/zola/issues/569
#[test]
fn can_understand_footnote_in_header() {
let permalinks_ctx = HashMap::new();
let config = Config::default();
let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
let res = render_content("# text [^1] there\n[^1]: footnote", &context).unwrap();
assert_eq!(res.body, r##"<h1 id="text-there">text <sup class="footnote-reference"><a href="#1">1</a></sup> there</h1>
<div class="footnote-definition" id="1"><sup class="footnote-definition-label">1</sup>
<p>footnote</p>
</div>
"##);
}
2018-05-07 19:03:51 +00:00
#[test]
fn can_make_valid_relative_link_in_header() {
let mut permalinks = HashMap::new();
permalinks.insert("pages/about.md".to_string(), "https://vincent.is/about/".to_string());
let tera_ctx = Tera::default();
let config = Config::default();
2018-10-09 12:33:43 +00:00
let context = RenderContext::new(&tera_ctx, &config, "", &permalinks, InsertAnchor::None);
2018-10-31 07:18:57 +00:00
let res = render_content(r#" # [rel link](./pages/about.md)"#, &context).unwrap();
2018-05-07 19:03:51 +00:00
assert_eq!(
2018-08-24 21:37:55 +00:00
res.body,
2018-05-07 19:03:51 +00:00
"<h1 id=\"rel-link\"><a href=\"https://vincent.is/about/\">rel link</a></h1>\n"
);
}
#[test]
fn can_make_permalinks_with_colocated_assets_for_link() {
2018-05-07 19:03:51 +00:00
let permalinks_ctx = HashMap::new();
let config = Config::default();
2018-10-31 07:18:57 +00:00
let context = RenderContext::new(
&ZOLA_TERA,
&config,
"https://vincent.is/about/",
&permalinks_ctx,
InsertAnchor::None,
2018-05-07 19:03:51 +00:00
);
2018-10-31 07:18:57 +00:00
let res = render_content("[an image](image.jpg)", &context).unwrap();
assert_eq!(res.body, "<p><a href=\"https://vincent.is/about/image.jpg\">an image</a></p>\n");
2018-05-07 19:03:51 +00:00
}
#[test]
fn can_make_permalinks_with_colocated_assets_for_image() {
let permalinks_ctx = HashMap::new();
let config = Config::default();
2018-10-31 07:18:57 +00:00
let context = RenderContext::new(
&ZOLA_TERA,
&config,
"https://vincent.is/about/",
&permalinks_ctx,
InsertAnchor::None,
);
let res = render_content("![alt text](image.jpg)", &context).unwrap();
assert_eq!(
2018-08-24 21:37:55 +00:00
res.body,
"<p><img src=\"https://vincent.is/about/image.jpg\" alt=\"alt text\" /></p>\n"
);
}
#[test]
fn markdown_doesnt_wrap_html_in_paragraph() {
let permalinks_ctx = HashMap::new();
let config = Config::default();
2018-10-31 07:18:57 +00:00
let context = RenderContext::new(
&ZOLA_TERA,
&config,
"https://vincent.is/about/",
&permalinks_ctx,
InsertAnchor::None,
);
let res = render_content(
r#"
Some text
<h1>Helo</h1>
<div>
<a href="mobx-flow.png">
<img src="mobx-flow.png" alt="MobX flow">
</a>
</div>
2018-10-31 07:18:57 +00:00
"#,
&context,
)
.unwrap();
assert_eq!(
2018-08-24 21:37:55 +00:00
res.body,
"<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"
);
}
#[test]
fn can_validate_valid_external_links() {
let permalinks_ctx = HashMap::new();
let mut config = Config::default();
config.check_external_links = true;
2018-10-31 07:18:57 +00:00
let context = RenderContext::new(
&ZOLA_TERA,
&config,
"https://vincent.is/about/",
&permalinks_ctx,
InsertAnchor::None,
);
2018-10-31 07:18:57 +00:00
let res = render_content("[a link](http://google.com)", &context).unwrap();
assert_eq!(res.body, "<p><a href=\"http://google.com\">a link</a></p>\n");
}
#[test]
fn can_show_error_message_for_invalid_external_links() {
let permalinks_ctx = HashMap::new();
let mut config = Config::default();
config.check_external_links = true;
2018-10-31 07:18:57 +00:00
let context = RenderContext::new(
&ZOLA_TERA,
&config,
"https://vincent.is/about/",
&permalinks_ctx,
InsertAnchor::None,
);
let res = render_content("[a link](http://google.comy)", &context);
assert!(res.is_err());
let err = res.unwrap_err();
2019-01-11 19:29:46 +00:00
assert!(format!("{}", err).contains("Link http://google.comy is not valid"));
}
#[test]
fn doesnt_try_to_validate_email_links_mailto() {
let permalinks_ctx = HashMap::new();
let mut config = Config::default();
config.check_external_links = true;
2018-10-31 07:18:57 +00:00
let context = RenderContext::new(
&ZOLA_TERA,
&config,
"https://vincent.is/about/",
&permalinks_ctx,
InsertAnchor::None,
);
2018-10-31 07:18:57 +00:00
let res = render_content("Email: [foo@bar.baz](mailto:foo@bar.baz)", &context).unwrap();
assert_eq!(res.body, "<p>Email: <a href=\"mailto:foo@bar.baz\">foo@bar.baz</a></p>\n");
}
#[test]
fn doesnt_try_to_validate_email_links_angled_brackets() {
let permalinks_ctx = HashMap::new();
let mut config = Config::default();
config.check_external_links = true;
2018-10-31 07:18:57 +00:00
let context = RenderContext::new(
&ZOLA_TERA,
&config,
"https://vincent.is/about/",
&permalinks_ctx,
InsertAnchor::None,
);
2018-10-31 07:18:57 +00:00
let res = render_content("Email: <foo@bar.baz>", &context).unwrap();
assert_eq!(res.body, "<p>Email: <a href=\"mailto:foo@bar.baz\">foo@bar.baz</a></p>\n");
}
#[test]
fn can_handle_summaries() {
let tera_ctx = Tera::default();
let permalinks_ctx = HashMap::new();
let config = Config::default();
2018-10-09 12:33:43 +00:00
let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
2018-10-31 07:18:57 +00:00
let res = render_content(
"Hello [world]\n\n<!-- more -->\n\nBla bla\n\n[world]: https://vincent.is/about/",
&context,
)
.unwrap();
assert_eq!(
res.body,
"<p>Hello <a href=\"https://vincent.is/about/\">world</a></p>\n<p id=\"zola-continue-reading\"><a name=\"continue-reading\"></a></p>\n<p>Bla bla</p>\n"
);
assert_eq!(
res.summary_len,
Some("<p>Hello <a href=\"https://vincent.is/about/\">world</a></p>\n".len())
);
}
// https://github.com/Keats/gutenberg/issues/522
#[test]
fn doesnt_try_to_highlight_content_from_shortcode() {
let permalinks_ctx = HashMap::new();
let mut tera = Tera::default();
tera.extend(&ZOLA_TERA).unwrap();
let shortcode = r#"
<figure>
{% if width %}
<img src="/images/{{ src }}" alt="{{ caption }}" width="{{ width }}" />
{% else %}
<img src="/images/{{ src }}" alt="{{ caption }}" />
{% endif %}
<figcaption>{{ caption }}</figcaption>
</figure>"#;
let markdown_string = r#"{{ figure(src="spherecluster.png", caption="Some spheres.") }}"#;
let expected = r#"<figure>
<img src="/images/spherecluster.png" alt="Some spheres." />
<figcaption>Some spheres.</figcaption>
</figure>"#;
tera.add_raw_template(&format!("shortcodes/{}.html", "figure"), shortcode).unwrap();
let config = Config::default();
let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
let res = render_content(markdown_string, &context).unwrap();
assert_eq!(res.body, expected);
}
2019-01-10 16:17:07 +00:00
// https://github.com/Keats/tera/issues/373
#[test]
fn can_split_lines_shortcode_body() {
let permalinks_ctx = HashMap::new();
let mut tera = Tera::default();
tera.extend(&ZOLA_TERA).unwrap();
let shortcode = r#"{{ body | split(pat="\n") }}"#;
let markdown_string = r#"
{% alert() %}
multi
ple
lines
{% end %}
"#;
let expected = r#"<p>["multi", "ple", "lines"]</p>"#;
tera.add_raw_template(&format!("shortcodes/{}.html", "alert"), shortcode).unwrap();
let config = Config::default();
let context = RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
let res = render_content(markdown_string, &context).unwrap();
assert_eq!(res.body, expected);
}