Merge pull request #380 from Freaky/rework-summary

Improve summary handling
This commit is contained in:
Vincent Prouillet 2018-08-25 11:19:40 +02:00 committed by GitHub
commit 0b874c9248
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 101 additions and 75 deletions

View file

@ -185,19 +185,12 @@ impl Page {
context.tera_context.add("page", self); context.tera_context.add("page", self);
let res = render_content( let res = render_content(&self.raw_content, &context)
&self.raw_content.replacen("<!-- more -->", "<a name=\"continue-reading\"></a>", 1), .chain_err(|| format!("Failed to render content of {}", self.file.path.display()))?;
&context,
).chain_err(|| format!("Failed to render content of {}", self.file.path.display()))?; self.summary = res.summary_len.map(|l| res.body[0..l].to_owned());
self.content = res.0; self.content = res.body;
self.toc = res.1; self.toc = res.toc;
if self.raw_content.contains("<!-- more -->") {
self.summary = Some({
let summary = self.raw_content.splitn(2, "<!-- more -->").collect::<Vec<&str>>()[0];
render_content(summary, &context)
.chain_err(|| format!("Failed to render content of {}", self.file.path.display()))?.0
})
}
Ok(()) Ok(())
} }

View file

@ -137,8 +137,8 @@ impl Section {
let res = render_content(&self.raw_content, &context) let res = render_content(&self.raw_content, &context)
.chain_err(|| format!("Failed to render content of {}", self.file.path.display()))?; .chain_err(|| format!("Failed to render content of {}", self.file.path.display()))?;
self.content = res.0; self.content = res.body;
self.toc = res.1; self.toc = res.toc;
Ok(()) Ok(())
} }

View file

@ -32,7 +32,7 @@ pub use table_of_contents::Header;
pub use shortcode::render_shortcodes; pub use shortcode::render_shortcodes;
pub use context::RenderContext; pub use context::RenderContext;
pub fn render_content(content: &str, context: &RenderContext) -> Result<(String, Vec<Header>)> { pub fn render_content(content: &str, context: &RenderContext) -> Result<markdown::Rendered> {
// Don't do anything if there is nothing like a shortcode in the content // Don't do anything if there is nothing like a shortcode in the content
if content.contains("{{") || content.contains("{%") { if content.contains("{{") || content.contains("{%") {
let rendered = render_shortcodes(content, context)?; let rendered = render_shortcodes(content, context)?;

View file

@ -1,4 +1,4 @@
use std::borrow::Cow::Owned; use std::borrow::Cow::{Owned, Borrowed};
use pulldown_cmark as cmark; use pulldown_cmark as cmark;
use self::cmark::{Parser, Event, Tag, Options, OPTION_ENABLE_TABLES, OPTION_ENABLE_FOOTNOTES}; use self::cmark::{Parser, Event, Tag, Options, OPTION_ENABLE_TABLES, OPTION_ENABLE_FOOTNOTES};
@ -14,6 +14,15 @@ use link_checker::check_url;
use table_of_contents::{TempHeader, Header, make_table_of_contents}; use table_of_contents::{TempHeader, Header, make_table_of_contents};
use context::RenderContext; use context::RenderContext;
const CONTINUE_READING: &str = "<p><a name=\"continue-reading\"></a></p>\n";
#[derive(Debug)]
pub struct Rendered {
pub body: String,
pub summary_len: Option<usize>,
pub toc: Vec<Header>
}
// We might have cases where the slug is already present in our list of anchor // We might have cases where the slug is already present in our list of anchor
// for example an article could have several titles named Example // for example an article could have several titles named Example
// We add a counter after the slug if the slug is already present, which // We add a counter after the slug if the slug is already present, which
@ -36,8 +45,7 @@ fn is_colocated_asset_link(link: &str) -> bool {
&& !link.starts_with("mailto:") && !link.starts_with("mailto:")
} }
pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<Rendered> {
pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<(String, Vec<Header>)> {
// the rendered html // the rendered html
let mut html = String::with_capacity(content.len()); let mut html = String::with_capacity(content.len());
// Set while parsing // Set while parsing
@ -57,6 +65,7 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<(Strin
let mut temp_header = TempHeader::default(); let mut temp_header = TempHeader::default();
let mut opts = Options::empty(); let mut opts = Options::empty();
let mut has_summary = false;
opts.insert(OPTION_ENABLE_TABLES); opts.insert(OPTION_ENABLE_TABLES);
opts.insert(OPTION_ENABLE_FOOTNOTES); opts.insert(OPTION_ENABLE_FOOTNOTES);
@ -68,7 +77,7 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<(Strin
if in_header { if in_header {
if header_created { if header_created {
temp_header.push(&text); temp_header.push(&text);
return Event::Html(Owned(String::new())); return Event::Html(Borrowed(""));
} }
let id = find_anchor(&anchors, slugify(&text), 0); let id = find_anchor(&anchors, slugify(&text), 0);
anchors.push(id.clone()); anchors.push(id.clone());
@ -78,7 +87,7 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<(Strin
// += as we might have some <code> or other things already there // += as we might have some <code> or other things already there
temp_header.title += &text; temp_header.title += &text;
header_created = true; header_created = true;
return Event::Html(Owned(String::new())); return Event::Html(Borrowed(""));
} }
// if we are in the middle of a code block // if we are in the middle of a code block
@ -93,7 +102,7 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<(Strin
} }
Event::Start(Tag::CodeBlock(ref info)) => { Event::Start(Tag::CodeBlock(ref info)) => {
if !context.config.highlight_code { if !context.config.highlight_code {
return Event::Html(Owned("<pre><code>".to_string())); return Event::Html(Borrowed("<pre><code>"));
} }
let theme = &THEME_SET.themes[&context.config.highlight_theme]; let theme = &THEME_SET.themes[&context.config.highlight_theme];
@ -101,7 +110,7 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<(Strin
Ok(h) => highlighter = Some(h), Ok(h) => highlighter = Some(h),
Err(err) => { Err(err) => {
error = Some(format!("Could not load syntax: {}", err).into()); error = Some(format!("Could not load syntax: {}", err).into());
return Event::Html(Owned(String::new())); return Event::Html(Borrowed(""));
} }
} }
let snippet = start_coloured_html_snippet(theme); let snippet = start_coloured_html_snippet(theme);
@ -109,11 +118,11 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<(Strin
} }
Event::End(Tag::CodeBlock(_)) => { Event::End(Tag::CodeBlock(_)) => {
if !context.config.highlight_code { if !context.config.highlight_code {
return Event::Html(Owned("</code></pre>\n".to_string())); return Event::Html(Borrowed("</code></pre>\n"));
} }
// reset highlight and close the code block // reset highlight and close the code block
highlighter = None; highlighter = None;
Event::Html(Owned("</pre>".to_string())) Event::Html(Borrowed("</pre>"))
} }
Event::Start(Tag::Image(src, title)) => { Event::Start(Tag::Image(src, title)) => {
if is_colocated_asset_link(&src) { if is_colocated_asset_link(&src) {
@ -139,7 +148,7 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<(Strin
Ok(url) => url, Ok(url) => url,
Err(_) => { Err(_) => {
error = Some(format!("Relative link {} not found.", link).into()); error = Some(format!("Relative link {} not found.", link).into());
return Event::Html(Owned(String::new())); return Event::Html(Borrowed(""));
} }
} }
} else if is_colocated_asset_link(&link) { } else if is_colocated_asset_link(&link) {
@ -167,7 +176,7 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<(Strin
format!("<a href=\"{}\" title=\"{}\">", fixed_link, title) format!("<a href=\"{}\" title=\"{}\">", fixed_link, title)
}; };
temp_header.push(&html); temp_header.push(&html);
return Event::Html(Owned(String::new())); return Event::Html(Borrowed(""));
} }
Event::Start(Tag::Link(Owned(fixed_link), title)) Event::Start(Tag::Link(Owned(fixed_link), title))
@ -175,28 +184,28 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<(Strin
Event::End(Tag::Link(_, _)) => { Event::End(Tag::Link(_, _)) => {
if in_header { if in_header {
temp_header.push("</a>"); temp_header.push("</a>");
return Event::Html(Owned(String::new())); return Event::Html(Borrowed(""));
} }
event event
} }
Event::Start(Tag::Code) => { Event::Start(Tag::Code) => {
if in_header { if in_header {
temp_header.push("<code>"); temp_header.push("<code>");
return Event::Html(Owned(String::new())); return Event::Html(Borrowed(""));
} }
event event
} }
Event::End(Tag::Code) => { Event::End(Tag::Code) => {
if in_header { if in_header {
temp_header.push("</code>"); temp_header.push("</code>");
return Event::Html(Owned(String::new())); return Event::Html(Borrowed(""));
} }
event event
} }
Event::Start(Tag::Header(num)) => { Event::Start(Tag::Header(num)) => {
in_header = true; in_header = true;
temp_header = TempHeader::new(num); temp_header = TempHeader::new(num);
Event::Html(Owned(String::new())) Event::Html(Borrowed(""))
} }
Event::End(Tag::Header(_)) => { Event::End(Tag::Header(_)) => {
// End of a header, reset all the things and return the stringified // End of a header, reset all the things and return the stringified
@ -208,6 +217,10 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<(Strin
temp_header = TempHeader::default(); temp_header = TempHeader::default();
Event::Html(Owned(val)) Event::Html(Owned(val))
} }
Event::Html(ref markup) if markup.contains("<!-- more -->") => {
has_summary = true;
Event::Html(Borrowed(CONTINUE_READING))
}
_ => event, _ => event,
} }
}); });
@ -215,11 +228,14 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<(Strin
cmark::html::push_html(&mut html, parser); cmark::html::push_html(&mut html, parser);
} }
match error { if let Some(e) = error {
Some(e) => Err(e), return Err(e)
None => Ok(( } else {
html.replace("<p></p>", "").replace("</p></p>", "</p>"), html = html.replace("<p></p>", "").replace("</p></p>", "</p>");
make_table_of_contents(&headers) Ok(Rendered {
)), summary_len: if has_summary { html.find(CONTINUE_READING) } else { None },
body: html,
toc: make_table_of_contents(&headers)
})
} }
} }

View file

@ -22,7 +22,7 @@ fn can_do_render_content_simple() {
let config = Config::default(); let config = Config::default();
let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None); let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
let res = render_content("hello", &context).unwrap(); let res = render_content("hello", &context).unwrap();
assert_eq!(res.0, "<p>hello</p>\n"); assert_eq!(res.body, "<p>hello</p>\n");
} }
#[test] #[test]
@ -34,7 +34,7 @@ fn doesnt_highlight_code_block_with_highlighting_off() {
let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None); let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
let res = render_content("```\n$ gutenberg server\n```", &context).unwrap(); let res = render_content("```\n$ gutenberg server\n```", &context).unwrap();
assert_eq!( assert_eq!(
res.0, res.body,
"<pre><code>$ gutenberg server\n</code></pre>\n" "<pre><code>$ gutenberg server\n</code></pre>\n"
); );
} }
@ -47,7 +47,7 @@ fn can_highlight_code_block_no_lang() {
let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None); let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
let res = render_content("```\n$ gutenberg server\n$ ping\n```", &context).unwrap(); let res = render_content("```\n$ gutenberg server\n$ ping\n```", &context).unwrap();
assert_eq!( assert_eq!(
res.0, res.body,
"<pre style=\"background-color:#2b303b\">\n<span style=\"background-color:#2b303b;color:#c0c5ce;\">$ gutenberg server\n</span><span style=\"background-color:#2b303b;color:#c0c5ce;\">$ ping\n</span></pre>" "<pre style=\"background-color:#2b303b\">\n<span style=\"background-color:#2b303b;color:#c0c5ce;\">$ gutenberg server\n</span><span style=\"background-color:#2b303b;color:#c0c5ce;\">$ ping\n</span></pre>"
); );
} }
@ -60,7 +60,7 @@ fn can_highlight_code_block_with_lang() {
let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None); let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
let res = render_content("```python\nlist.append(1)\n```", &context).unwrap(); let res = render_content("```python\nlist.append(1)\n```", &context).unwrap();
assert_eq!( assert_eq!(
res.0, res.body,
"<pre style=\"background-color:#2b303b\">\n<span style=\"background-color:#2b303b;color:#c0c5ce;\">list.</span><span style=\"background-color:#2b303b;color:#bf616a;\">append</span><span style=\"background-color:#2b303b;color:#c0c5ce;\">(</span><span style=\"background-color:#2b303b;color:#d08770;\">1</span><span style=\"background-color:#2b303b;color:#c0c5ce;\">)\n</span></pre>" "<pre style=\"background-color:#2b303b\">\n<span style=\"background-color:#2b303b;color:#c0c5ce;\">list.</span><span style=\"background-color:#2b303b;color:#bf616a;\">append</span><span style=\"background-color:#2b303b;color:#c0c5ce;\">(</span><span style=\"background-color:#2b303b;color:#d08770;\">1</span><span style=\"background-color:#2b303b;color:#c0c5ce;\">)\n</span></pre>"
); );
} }
@ -74,7 +74,7 @@ fn can_higlight_code_block_with_unknown_lang() {
let res = render_content("```yolo\nlist.append(1)\n```", &context).unwrap(); let res = render_content("```yolo\nlist.append(1)\n```", &context).unwrap();
// defaults to plain text // defaults to plain text
assert_eq!( assert_eq!(
res.0, res.body,
"<pre style=\"background-color:#2b303b\">\n<span style=\"background-color:#2b303b;color:#c0c5ce;\">list.append(1)\n</span></pre>" "<pre style=\"background-color:#2b303b\">\n<span style=\"background-color:#2b303b;color:#c0c5ce;\">list.append(1)\n</span></pre>"
); );
} }
@ -89,8 +89,8 @@ Hello
{{ youtube(id="ub36ffWAqgQ") }} {{ youtube(id="ub36ffWAqgQ") }}
"#, &context).unwrap(); "#, &context).unwrap();
assert!(res.0.contains("<p>Hello</p>\n<div >")); assert!(res.body.contains("<p>Hello</p>\n<div >"));
assert!(res.0.contains(r#"<iframe src="https://www.youtube.com/embed/ub36ffWAqgQ""#)); assert!(res.body.contains(r#"<iframe src="https://www.youtube.com/embed/ub36ffWAqgQ""#));
} }
#[test] #[test]
@ -106,7 +106,7 @@ fn can_render_shortcode_with_markdown_char_in_args_name() {
]; ];
for i in input { for i in input {
let res = render_content(&format!("{{{{ youtube(id=\"hey\", {}=1) }}}}", i), &context).unwrap(); let res = render_content(&format!("{{{{ youtube(id=\"hey\", {}=1) }}}}", i), &context).unwrap();
assert!(res.0.contains(r#"<iframe src="https://www.youtube.com/embed/hey""#)); assert!(res.body.contains(r#"<iframe src="https://www.youtube.com/embed/hey""#));
} }
} }
@ -124,7 +124,7 @@ fn can_render_shortcode_with_markdown_char_in_args_value() {
]; ];
for i in input { for i in input {
let res = render_content(&format!("{{{{ youtube(id=\"{}\") }}}}", i), &context).unwrap(); let res = render_content(&format!("{{{{ youtube(id=\"{}\") }}}}", i), &context).unwrap();
assert!(res.0.contains(&format!(r#"<iframe src="https://www.youtube.com/embed/{}""#, i))); assert!(res.body.contains(&format!(r#"<iframe src="https://www.youtube.com/embed/{}""#, i)));
} }
} }
@ -145,7 +145,7 @@ fn can_render_body_shortcode_with_markdown_char_in_name() {
let res = render_content(&format!("{{% {}(author=\"Bob\") %}}\nhey\n{{% end %}}", i), &context).unwrap(); let res = render_content(&format!("{{% {}(author=\"Bob\") %}}\nhey\n{{% end %}}", i), &context).unwrap();
println!("{:?}", res); println!("{:?}", res);
assert!(res.0.contains("<blockquote>hey - Bob</blockquote>")); assert!(res.body.contains("<blockquote>hey - Bob</blockquote>"));
} }
} }
@ -174,7 +174,7 @@ Here is another paragraph.
let res = render_content(markdown_string, &context).unwrap(); let res = render_content(markdown_string, &context).unwrap();
println!("{:?}", res); println!("{:?}", res);
assert_eq!(res.0, expected); assert_eq!(res.body, expected);
} }
#[test] #[test]
@ -207,7 +207,7 @@ Here is another paragraph.
let res = render_content(markdown_string, &context).unwrap(); let res = render_content(markdown_string, &context).unwrap();
println!("{:?}", res); println!("{:?}", res);
assert_eq!(res.0, expected); assert_eq!(res.body, expected);
} }
#[test] #[test]
@ -229,11 +229,11 @@ Hello
{{ gist(url="https://gist.github.com/Keats/32d26f699dcc13ebd41b") }} {{ gist(url="https://gist.github.com/Keats/32d26f699dcc13ebd41b") }}
"#, &context).unwrap(); "#, &context).unwrap();
assert!(res.0.contains("<p>Hello</p>\n<div >")); assert!(res.body.contains("<p>Hello</p>\n<div >"));
assert!(res.0.contains(r#"<iframe src="https://www.youtube.com/embed/ub36ffWAqgQ""#)); assert!(res.body.contains(r#"<iframe src="https://www.youtube.com/embed/ub36ffWAqgQ""#));
assert!(res.0.contains(r#"<iframe src="https://www.youtube.com/embed/ub36ffWAqgQ?autoplay=1""#)); assert!(res.body.contains(r#"<iframe src="https://www.youtube.com/embed/ub36ffWAqgQ?autoplay=1""#));
assert!(res.0.contains(r#"<iframe src="https://www.streamable.com/e/c0ic""#)); assert!(res.body.contains(r#"<iframe src="https://www.streamable.com/e/c0ic""#));
assert!(res.0.contains(r#"//player.vimeo.com/video/210073083""#)); assert!(res.body.contains(r#"//player.vimeo.com/video/210073083""#));
} }
#[test] #[test]
@ -243,7 +243,7 @@ fn doesnt_render_ignored_shortcodes() {
config.highlight_code = false; config.highlight_code = false;
let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None); let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
let res = render_content(r#"```{{/* youtube(id="w7Ft2ymGmfc") */}}```"#, &context).unwrap(); let res = render_content(r#"```{{/* youtube(id="w7Ft2ymGmfc") */}}```"#, &context).unwrap();
assert_eq!(res.0, "<p><code>{{ youtube(id=&quot;w7Ft2ymGmfc&quot;) }}</code></p>\n"); assert_eq!(res.body, "<p><code>{{ youtube(id=&quot;w7Ft2ymGmfc&quot;) }}</code></p>\n");
} }
#[test] #[test]
@ -261,7 +261,7 @@ Hello
A quote A quote
{% end %} {% end %}
"#, &context).unwrap(); "#, &context).unwrap();
assert_eq!(res.0, "<p>Hello</p>\n<blockquote>A quote - Keats</blockquote>\n"); assert_eq!(res.body, "<p>Hello</p>\n<blockquote>A quote - Keats</blockquote>\n");
} }
#[test] #[test]
@ -287,7 +287,7 @@ fn can_make_valid_relative_link() {
).unwrap(); ).unwrap();
assert!( assert!(
res.0.contains(r#"<p><a href="https://vincent.is/about">rel link</a>, <a href="https://vincent.is/about">abs link</a></p>"#) res.body.contains(r#"<p><a href="https://vincent.is/about">rel link</a>, <a href="https://vincent.is/about">abs link</a></p>"#)
); );
} }
@ -301,7 +301,7 @@ fn can_make_relative_links_with_anchors() {
let res = render_content(r#"[rel link](./pages/about.md#cv)"#, &context).unwrap(); let res = render_content(r#"[rel link](./pages/about.md#cv)"#, &context).unwrap();
assert!( assert!(
res.0.contains(r#"<p><a href="https://vincent.is/about#cv">rel link</a></p>"#) res.body.contains(r#"<p><a href="https://vincent.is/about#cv">rel link</a></p>"#)
); );
} }
@ -322,7 +322,7 @@ fn can_add_id_to_headers() {
let config = Config::default(); let config = Config::default();
let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None); let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
let res = render_content(r#"# Hello"#, &context).unwrap(); let res = render_content(r#"# Hello"#, &context).unwrap();
assert_eq!(res.0, "<h1 id=\"hello\">Hello</h1>\n"); assert_eq!(res.body, "<h1 id=\"hello\">Hello</h1>\n");
} }
#[test] #[test]
@ -332,7 +332,7 @@ fn can_add_id_to_headers_same_slug() {
let config = Config::default(); let config = Config::default();
let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None); let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
let res = render_content("# Hello\n# Hello", &context).unwrap(); let res = render_content("# Hello\n# Hello", &context).unwrap();
assert_eq!(res.0, "<h1 id=\"hello\">Hello</h1>\n<h1 id=\"hello-1\">Hello</h1>\n"); assert_eq!(res.body, "<h1 id=\"hello\">Hello</h1>\n<h1 id=\"hello-1\">Hello</h1>\n");
} }
#[test] #[test]
@ -342,7 +342,7 @@ fn can_insert_anchor_left() {
let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::Left); let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::Left);
let res = render_content("# Hello", &context).unwrap(); let res = render_content("# Hello", &context).unwrap();
assert_eq!( assert_eq!(
res.0, res.body,
"<h1 id=\"hello\"><a class=\"gutenberg-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>\nHello</h1>\n" "<h1 id=\"hello\"><a class=\"gutenberg-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>\nHello</h1>\n"
); );
} }
@ -354,7 +354,7 @@ fn can_insert_anchor_right() {
let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::Right); let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::Right);
let res = render_content("# Hello", &context).unwrap(); let res = render_content("# Hello", &context).unwrap();
assert_eq!( assert_eq!(
res.0, res.body,
"<h1 id=\"hello\">Hello<a class=\"gutenberg-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>\n</h1>\n" "<h1 id=\"hello\">Hello<a class=\"gutenberg-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>\n</h1>\n"
); );
} }
@ -367,7 +367,7 @@ fn can_insert_anchor_with_exclamation_mark() {
let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::Left); let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::Left);
let res = render_content("# Hello!", &context).unwrap(); let res = render_content("# Hello!", &context).unwrap();
assert_eq!( assert_eq!(
res.0, res.body,
"<h1 id=\"hello\"><a class=\"gutenberg-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>\nHello!</h1>\n" "<h1 id=\"hello\"><a class=\"gutenberg-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>\nHello!</h1>\n"
); );
} }
@ -380,7 +380,7 @@ fn can_insert_anchor_with_link() {
let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::Left); let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::Left);
let res = render_content("## [Rust](https://rust-lang.org)", &context).unwrap(); let res = render_content("## [Rust](https://rust-lang.org)", &context).unwrap();
assert_eq!( assert_eq!(
res.0, res.body,
"<h2 id=\"rust\"><a class=\"gutenberg-anchor\" href=\"#rust\" aria-label=\"Anchor link for: rust\">🔗</a>\n<a href=\"https://rust-lang.org\">Rust</a></h2>\n" "<h2 id=\"rust\"><a class=\"gutenberg-anchor\" href=\"#rust\" aria-label=\"Anchor link for: rust\">🔗</a>\n<a href=\"https://rust-lang.org\">Rust</a></h2>\n"
); );
} }
@ -392,7 +392,7 @@ fn can_insert_anchor_with_other_special_chars() {
let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::Left); let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::Left);
let res = render_content("# Hello*_()", &context).unwrap(); let res = render_content("# Hello*_()", &context).unwrap();
assert_eq!( assert_eq!(
res.0, res.body,
"<h1 id=\"hello\"><a class=\"gutenberg-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>\nHello*_()</h1>\n" "<h1 id=\"hello\"><a class=\"gutenberg-anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">🔗</a>\nHello*_()</h1>\n"
); );
} }
@ -420,7 +420,7 @@ fn can_make_toc() {
### Last one ### Last one
"#, &context).unwrap(); "#, &context).unwrap();
let toc = res.1; let toc = res.toc;
assert_eq!(toc.len(), 1); assert_eq!(toc.len(), 1);
assert_eq!(toc[0].children.len(), 2); assert_eq!(toc[0].children.len(), 2);
assert_eq!(toc[0].children[1].children.len(), 1); assert_eq!(toc[0].children[1].children.len(), 1);
@ -433,7 +433,7 @@ fn can_understand_backtick_in_titles() {
let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None); let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
let res = render_content("# `Hello`", &context).unwrap(); let res = render_content("# `Hello`", &context).unwrap();
assert_eq!( assert_eq!(
res.0, res.body,
"<h1 id=\"hello\"><code>Hello</code></h1>\n" "<h1 id=\"hello\"><code>Hello</code></h1>\n"
); );
} }
@ -445,7 +445,7 @@ fn can_understand_backtick_in_paragraphs() {
let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None); let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
let res = render_content("Hello `world`", &context).unwrap(); let res = render_content("Hello `world`", &context).unwrap();
assert_eq!( assert_eq!(
res.0, res.body,
"<p>Hello <code>world</code></p>\n" "<p>Hello <code>world</code></p>\n"
); );
} }
@ -458,7 +458,7 @@ fn can_understand_links_in_header() {
let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None); let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
let res = render_content("# [Rust](https://rust-lang.org)", &context).unwrap(); let res = render_content("# [Rust](https://rust-lang.org)", &context).unwrap();
assert_eq!( assert_eq!(
res.0, res.body,
"<h1 id=\"rust\"><a href=\"https://rust-lang.org\">Rust</a></h1>\n" "<h1 id=\"rust\"><a href=\"https://rust-lang.org\">Rust</a></h1>\n"
); );
} }
@ -470,7 +470,7 @@ fn can_understand_link_with_title_in_header() {
let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None); let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
let res = render_content("# [Rust](https://rust-lang.org \"Rust homepage\")", &context).unwrap(); let res = render_content("# [Rust](https://rust-lang.org \"Rust homepage\")", &context).unwrap();
assert_eq!( assert_eq!(
res.0, res.body,
"<h1 id=\"rust\"><a href=\"https://rust-lang.org\" title=\"Rust homepage\">Rust</a></h1>\n" "<h1 id=\"rust\"><a href=\"https://rust-lang.org\" title=\"Rust homepage\">Rust</a></h1>\n"
); );
} }
@ -488,7 +488,7 @@ fn can_make_valid_relative_link_in_header() {
).unwrap(); ).unwrap();
assert_eq!( assert_eq!(
res.0, res.body,
"<h1 id=\"rel-link\"><a href=\"https://vincent.is/about/\">rel link</a></h1>\n" "<h1 id=\"rel-link\"><a href=\"https://vincent.is/about/\">rel link</a></h1>\n"
); );
} }
@ -500,7 +500,7 @@ fn can_make_permalinks_with_colocated_assets_for_link() {
let context = RenderContext::new(&GUTENBERG_TERA, &config, "https://vincent.is/about/", &permalinks_ctx, Path::new("something"), InsertAnchor::None); let context = RenderContext::new(&GUTENBERG_TERA, &config, "https://vincent.is/about/", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
let res = render_content("[an image](image.jpg)", &context).unwrap(); let res = render_content("[an image](image.jpg)", &context).unwrap();
assert_eq!( assert_eq!(
res.0, res.body,
"<p><a href=\"https://vincent.is/about/image.jpg\">an image</a></p>\n" "<p><a href=\"https://vincent.is/about/image.jpg\">an image</a></p>\n"
); );
} }
@ -512,7 +512,7 @@ fn can_make_permalinks_with_colocated_assets_for_image() {
let context = RenderContext::new(&GUTENBERG_TERA, &config, "https://vincent.is/about/", &permalinks_ctx, Path::new("something"), InsertAnchor::None); let context = RenderContext::new(&GUTENBERG_TERA, &config, "https://vincent.is/about/", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
let res = render_content("![alt text](image.jpg)", &context).unwrap(); let res = render_content("![alt text](image.jpg)", &context).unwrap();
assert_eq!( assert_eq!(
res.0, res.body,
"<p><img src=\"https://vincent.is/about/image.jpg\" alt=\"alt text\" /></p>\n" "<p><img src=\"https://vincent.is/about/image.jpg\" alt=\"alt text\" /></p>\n"
); );
} }
@ -534,7 +534,7 @@ Some text
</div> </div>
"#, &context).unwrap(); "#, &context).unwrap();
assert_eq!( assert_eq!(
res.0, 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" "<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"
); );
} }
@ -547,7 +547,7 @@ fn can_validate_valid_external_links() {
let context = RenderContext::new(&GUTENBERG_TERA, &config, "https://vincent.is/about/", &permalinks_ctx, Path::new("something"), InsertAnchor::None); let context = RenderContext::new(&GUTENBERG_TERA, &config, "https://vincent.is/about/", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
let res = render_content("[a link](http://google.com)", &context).unwrap(); let res = render_content("[a link](http://google.com)", &context).unwrap();
assert_eq!( assert_eq!(
res.0, res.body,
"<p><a href=\"http://google.com\">a link</a></p>\n" "<p><a href=\"http://google.com\">a link</a></p>\n"
); );
} }
@ -563,3 +563,20 @@ fn can_show_error_message_for_invalid_external_links() {
let err = res.unwrap_err(); let err = res.unwrap_err();
assert!(err.description().contains("Link http://google.comy is not valid")); assert!(err.description().contains("Link http://google.comy is not valid"));
} }
#[test]
fn can_handle_summaries() {
let tera_ctx = Tera::default();
let permalinks_ctx = HashMap::new();
let config = Config::default();
let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, Path::new("something"), InsertAnchor::None);
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><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())
);
}