2017-03-07 12:34:31 +00:00
|
|
|
use std::borrow::Cow::Owned;
|
|
|
|
|
|
|
|
use pulldown_cmark as cmark;
|
2017-04-19 13:16:21 +00:00
|
|
|
use self::cmark::{Parser, Event, Tag, Options, OPTION_ENABLE_TABLES, OPTION_ENABLE_FOOTNOTES};
|
2017-04-06 07:07:18 +00:00
|
|
|
use slug::slugify;
|
2017-03-07 12:34:31 +00:00
|
|
|
use syntect::easy::HighlightLines;
|
|
|
|
use syntect::html::{start_coloured_html_snippet, styles_to_coloured_html, IncludeBackground};
|
2017-03-27 14:17:33 +00:00
|
|
|
|
2017-07-01 07:47:41 +00:00
|
|
|
use errors::Result;
|
|
|
|
use utils::site::resolve_internal_link;
|
2018-05-06 20:58:39 +00:00
|
|
|
use highlighting::{get_highlighter, THEME_SET};
|
2018-07-16 19:13:00 +00:00
|
|
|
use link_checker::check_url;
|
2018-05-06 20:58:39 +00:00
|
|
|
|
2017-07-01 07:47:41 +00:00
|
|
|
use table_of_contents::{TempHeader, Header, make_table_of_contents};
|
2018-05-06 20:58:39 +00:00
|
|
|
use context::RenderContext;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// We add a counter after the slug if the slug is already present, which
|
|
|
|
// means we will have example, example-1, example-2 etc
|
|
|
|
fn find_anchor(anchors: &[String], name: String, level: u8) -> String {
|
|
|
|
if level == 0 && !anchors.contains(&name) {
|
|
|
|
return name.to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
let new_anchor = format!("{}-{}", name, level + 1);
|
|
|
|
if !anchors.contains(&new_anchor) {
|
|
|
|
return new_anchor;
|
|
|
|
}
|
|
|
|
|
|
|
|
find_anchor(anchors, name, level + 1)
|
|
|
|
}
|
2017-03-07 12:34:31 +00:00
|
|
|
|
2018-05-07 19:03:51 +00:00
|
|
|
fn is_colocated_asset_link(link: &str) -> bool {
|
|
|
|
!link.contains("/") // http://, ftp://, ../ etc
|
|
|
|
&& !link.starts_with("mailto:")
|
|
|
|
}
|
|
|
|
|
2017-05-22 11:28:43 +00:00
|
|
|
|
2018-05-06 20:58:39 +00:00
|
|
|
pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<(String, Vec<Header>)> {
|
|
|
|
// the rendered html
|
2018-05-07 19:03:51 +00:00
|
|
|
let mut html = String::with_capacity(content.len());
|
2017-03-27 14:17:33 +00:00
|
|
|
// Set while parsing
|
|
|
|
let mut error = None;
|
2018-05-06 20:58:39 +00:00
|
|
|
|
2017-03-27 14:17:33 +00:00
|
|
|
let mut highlighter: Option<HighlightLines> = None;
|
2017-04-06 07:07:18 +00:00
|
|
|
// If we get text in header, we need to insert the id and a anchor
|
|
|
|
let mut in_header = false;
|
2017-04-22 02:40:11 +00:00
|
|
|
// pulldown_cmark can send several text events for a title if there are markdown
|
|
|
|
// specific characters like `!` in them. We only want to insert the anchor the first time
|
2017-09-27 14:09:13 +00:00
|
|
|
let mut header_created = false;
|
2017-06-16 04:00:48 +00:00
|
|
|
let mut anchors: Vec<String> = vec![];
|
|
|
|
|
|
|
|
let mut headers = vec![];
|
|
|
|
// Defaults to a 0 level so not a real header
|
|
|
|
// It should be an Option ideally but not worth the hassle to update
|
|
|
|
let mut temp_header = TempHeader::default();
|
|
|
|
|
2017-04-19 13:16:21 +00:00
|
|
|
let mut opts = Options::empty();
|
|
|
|
opts.insert(OPTION_ENABLE_TABLES);
|
|
|
|
opts.insert(OPTION_ENABLE_FOOTNOTES);
|
|
|
|
|
2017-03-27 14:17:33 +00:00
|
|
|
{
|
2017-10-29 19:55:36 +00:00
|
|
|
let parser = Parser::new_ext(content, opts).map(|event| {
|
|
|
|
match event {
|
2018-05-06 20:58:39 +00:00
|
|
|
Event::Text(text) => {
|
|
|
|
// Header first
|
|
|
|
if in_header {
|
|
|
|
if header_created {
|
|
|
|
temp_header.push(&text);
|
|
|
|
return Event::Html(Owned(String::new()));
|
|
|
|
}
|
|
|
|
let id = find_anchor(&anchors, slugify(&text), 0);
|
|
|
|
anchors.push(id.clone());
|
|
|
|
// update the header and add it to the list
|
2018-05-07 16:38:04 +00:00
|
|
|
temp_header.permalink = format!("{}#{}", context.current_page_permalink, id);
|
|
|
|
temp_header.id = id;
|
2018-05-06 20:58:39 +00:00
|
|
|
// += as we might have some <code> or other things already there
|
|
|
|
temp_header.title += &text;
|
|
|
|
header_created = true;
|
2017-09-27 14:09:13 +00:00
|
|
|
return Event::Html(Owned(String::new()));
|
|
|
|
}
|
|
|
|
|
2018-05-06 20:58:39 +00:00
|
|
|
// if we are in the middle of a code block
|
|
|
|
if let Some(ref mut highlighter) = highlighter {
|
|
|
|
let highlighted = &highlighter.highlight(&text);
|
|
|
|
let html = styles_to_coloured_html(highlighted, IncludeBackground::Yes);
|
|
|
|
return Event::Html(Owned(html));
|
2017-10-23 12:18:05 +00:00
|
|
|
}
|
|
|
|
|
2018-05-06 20:58:39 +00:00
|
|
|
// Business as usual
|
|
|
|
Event::Text(text)
|
|
|
|
},
|
|
|
|
Event::Start(Tag::CodeBlock(ref info)) => {
|
|
|
|
if !context.config.highlight_code {
|
2018-05-07 16:38:04 +00:00
|
|
|
return Event::Html(Owned("<pre><code>".to_string()));
|
2017-03-27 14:17:33 +00:00
|
|
|
}
|
2018-05-07 16:38:04 +00:00
|
|
|
|
2018-05-06 20:58:39 +00:00
|
|
|
let theme = &THEME_SET.themes[&context.config.highlight_theme];
|
|
|
|
highlighter = Some(get_highlighter(&theme, info));
|
|
|
|
let snippet = start_coloured_html_snippet(theme);
|
|
|
|
Event::Html(Owned(snippet))
|
|
|
|
},
|
|
|
|
Event::End(Tag::CodeBlock(_)) => {
|
|
|
|
if !context.config.highlight_code {
|
2018-05-07 16:38:04 +00:00
|
|
|
return Event::Html(Owned("</code></pre>\n".to_string()))
|
2017-03-27 14:17:33 +00:00
|
|
|
}
|
2018-05-06 20:58:39 +00:00
|
|
|
// reset highlight and close the code block
|
|
|
|
highlighter = None;
|
2018-05-07 16:38:04 +00:00
|
|
|
Event::Html(Owned("</pre>".to_string()))
|
2018-05-06 20:58:39 +00:00
|
|
|
},
|
2018-05-07 19:23:27 +00:00
|
|
|
Event::Start(Tag::Image(src, title)) => {
|
|
|
|
if is_colocated_asset_link(&src) {
|
|
|
|
return Event::Start(
|
2018-06-25 17:13:21 +00:00
|
|
|
Tag::Image(
|
|
|
|
Owned(format!("{}{}", context.current_page_permalink, src)),
|
|
|
|
title,
|
|
|
|
)
|
2018-05-07 19:23:27 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-05-18 12:51:44 +00:00
|
|
|
Event::Start(Tag::Image(src, title))
|
2018-05-07 19:23:27 +00:00
|
|
|
},
|
2018-05-07 19:03:51 +00:00
|
|
|
Event::Start(Tag::Link(link, title)) => {
|
|
|
|
// A few situations here:
|
|
|
|
// - it could be a relative link (starting with `./`)
|
|
|
|
// - it could be a link to a co-located asset
|
|
|
|
// - it could be a normal link
|
2018-06-25 17:13:21 +00:00
|
|
|
// - any of those can be in a header or not: if it's in a header
|
|
|
|
// we need to append to a string
|
2018-05-07 19:03:51 +00:00
|
|
|
let fixed_link = if link.starts_with("./") {
|
|
|
|
match resolve_internal_link(&link, context.permalinks) {
|
|
|
|
Ok(url) => url,
|
2018-05-06 20:58:39 +00:00
|
|
|
Err(_) => {
|
|
|
|
error = Some(format!("Relative link {} not found.", link).into());
|
2018-05-07 16:38:04 +00:00
|
|
|
return Event::Html(Owned(String::new()));
|
2017-03-27 14:17:33 +00:00
|
|
|
}
|
2018-05-07 19:03:51 +00:00
|
|
|
}
|
|
|
|
} else if is_colocated_asset_link(&link) {
|
|
|
|
format!("{}{}", context.current_page_permalink, link)
|
|
|
|
} else {
|
2018-07-16 19:13:00 +00:00
|
|
|
if context.config.check_external_links && !link.starts_with('#') {
|
|
|
|
println!("Checking Link {}", link);
|
|
|
|
let res = check_url(&link);
|
|
|
|
if res.is_valid() {
|
|
|
|
link.to_string()
|
|
|
|
} else {
|
|
|
|
error = Some(
|
|
|
|
format!("Link {} is not valid: {}", link, res.message()).into()
|
|
|
|
);
|
|
|
|
String::new()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
link.to_string()
|
|
|
|
}
|
2018-05-07 19:03:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if in_header {
|
|
|
|
let html = if title.is_empty() {
|
|
|
|
format!("<a href=\"{}\">", fixed_link)
|
|
|
|
} else {
|
|
|
|
format!("<a href=\"{}\" title=\"{}\">", fixed_link, title)
|
2018-05-06 20:58:39 +00:00
|
|
|
};
|
2018-05-07 19:03:51 +00:00
|
|
|
temp_header.push(&html);
|
|
|
|
return Event::Html(Owned(String::new()));
|
2017-03-27 14:17:33 +00:00
|
|
|
}
|
2017-03-30 05:48:07 +00:00
|
|
|
|
2018-05-07 19:03:51 +00:00
|
|
|
Event::Start(Tag::Link(Owned(fixed_link), title))
|
2018-05-06 20:58:39 +00:00
|
|
|
},
|
|
|
|
Event::End(Tag::Link(_, _)) => {
|
|
|
|
if in_header {
|
2018-05-07 16:38:04 +00:00
|
|
|
temp_header.push("</a>");
|
|
|
|
return Event::Html(Owned(String::new()));
|
2018-05-06 20:58:39 +00:00
|
|
|
}
|
|
|
|
event
|
2018-05-07 16:38:04 +00:00
|
|
|
},
|
2018-05-06 20:58:39 +00:00
|
|
|
Event::Start(Tag::Code) => {
|
|
|
|
if in_header {
|
|
|
|
temp_header.push("<code>");
|
|
|
|
return Event::Html(Owned(String::new()));
|
|
|
|
}
|
|
|
|
event
|
|
|
|
},
|
|
|
|
Event::End(Tag::Code) => {
|
|
|
|
if in_header {
|
|
|
|
temp_header.push("</code>");
|
|
|
|
return Event::Html(Owned(String::new()));
|
|
|
|
}
|
|
|
|
event
|
|
|
|
},
|
|
|
|
Event::Start(Tag::Header(num)) => {
|
|
|
|
in_header = true;
|
|
|
|
temp_header = TempHeader::new(num);
|
|
|
|
Event::Html(Owned(String::new()))
|
|
|
|
},
|
|
|
|
Event::End(Tag::Header(_)) => {
|
2018-06-25 17:13:21 +00:00
|
|
|
// End of a header, reset all the things and return the stringified
|
|
|
|
// version of the header
|
2018-05-06 20:58:39 +00:00
|
|
|
in_header = false;
|
|
|
|
header_created = false;
|
|
|
|
let val = temp_header.to_string(context.tera, context.insert_anchor);
|
|
|
|
headers.push(temp_header.clone());
|
|
|
|
temp_header = TempHeader::default();
|
|
|
|
Event::Html(Owned(val))
|
|
|
|
},
|
|
|
|
_ => event,
|
2017-05-01 07:04:41 +00:00
|
|
|
}
|
2018-05-06 20:58:39 +00:00
|
|
|
});
|
2017-03-07 12:34:31 +00:00
|
|
|
|
2017-03-27 14:17:33 +00:00
|
|
|
cmark::html::push_html(&mut html, parser);
|
2017-03-07 12:34:31 +00:00
|
|
|
}
|
2017-03-23 05:11:24 +00:00
|
|
|
|
2017-03-27 14:17:33 +00:00
|
|
|
match error {
|
|
|
|
Some(e) => Err(e),
|
2018-05-06 20:58:39 +00:00
|
|
|
None => Ok((
|
|
|
|
html.replace("<p></p>", "").replace("</p></p>", "</p>"),
|
|
|
|
make_table_of_contents(&headers)
|
|
|
|
)),
|
2017-03-27 14:17:33 +00:00
|
|
|
}
|
2017-03-07 12:34:31 +00:00
|
|
|
}
|