2018-10-31 07:18:57 +00:00
|
|
|
use std::borrow::Cow::{Borrowed, Owned};
|
2017-03-07 12:34:31 +00:00
|
|
|
|
|
|
|
use pulldown_cmark as cmark;
|
2017-04-06 07:07:18 +00:00
|
|
|
use slug::slugify;
|
2017-03-07 12:34:31 +00:00
|
|
|
use syntect::easy::HighlightLines;
|
2018-10-31 07:18:57 +00:00
|
|
|
use syntect::html::{
|
2019-02-09 18:54:46 +00:00
|
|
|
start_highlighted_html_snippet, styled_line_to_highlighted_html, IncludeBackground,
|
2018-10-31 07:18:57 +00:00
|
|
|
};
|
2017-03-27 14:17:33 +00:00
|
|
|
|
2018-10-31 07:18:57 +00:00
|
|
|
use config::highlighting::{get_highlighter, SYNTAX_SET, THEME_SET};
|
2019-01-12 08:55:52 +00:00
|
|
|
use context::RenderContext;
|
2019-01-23 18:20:02 +00:00
|
|
|
use errors::{Error, Result};
|
2019-01-12 08:55:52 +00:00
|
|
|
use front_matter::InsertAnchor;
|
2018-07-16 19:13:00 +00:00
|
|
|
use link_checker::check_url;
|
2019-02-09 18:54:46 +00:00
|
|
|
use table_of_contents::{make_table_of_contents, Header};
|
2018-10-31 07:18:57 +00:00
|
|
|
use utils::site::resolve_internal_link;
|
2019-01-12 08:55:52 +00:00
|
|
|
use utils::vec::InsertMany;
|
2018-05-06 20:58:39 +00:00
|
|
|
|
2019-01-12 08:55:52 +00:00
|
|
|
use self::cmark::{Event, Options, Parser, Tag};
|
2018-05-06 20:58:39 +00:00
|
|
|
|
2019-02-09 18:54:46 +00:00
|
|
|
const CONTINUE_READING: &str =
|
|
|
|
"<p id=\"zola-continue-reading\"><a name=\"continue-reading\"></a></p>\n";
|
2019-01-12 08:55:52 +00:00
|
|
|
const ANCHOR_LINK_TEMPLATE: &str = "anchor-link.html";
|
2018-08-22 16:34:32 +00:00
|
|
|
|
2018-08-24 21:37:39 +00:00
|
|
|
#[derive(Debug)]
|
2018-08-22 16:34:32 +00:00
|
|
|
pub struct Rendered {
|
|
|
|
pub body: String,
|
|
|
|
pub summary_len: Option<usize>,
|
2018-09-30 19:15:09 +00:00
|
|
|
pub toc: Vec<Header>,
|
2018-08-22 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 14:46:18 +00:00
|
|
|
// tracks a header in a slice of pulldown-cmark events
|
2019-01-16 09:09:23 +00:00
|
|
|
#[derive(Debug)]
|
2019-01-18 14:46:18 +00:00
|
|
|
struct HeaderRef {
|
|
|
|
start_idx: usize,
|
|
|
|
end_idx: usize,
|
2019-01-12 08:55:52 +00:00
|
|
|
level: i32,
|
|
|
|
}
|
|
|
|
|
2019-01-18 14:46:18 +00:00
|
|
|
impl HeaderRef {
|
|
|
|
fn new(start: usize, level: i32) -> HeaderRef {
|
|
|
|
HeaderRef { start_idx: start, end_idx: 0, level }
|
2019-01-12 08:55:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-06 20:58:39 +00:00
|
|
|
// 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) {
|
2018-11-19 14:04:22 +00:00
|
|
|
return name;
|
2018-05-06 20:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-11-16 17:19:38 +00:00
|
|
|
// Colocated asset links refers to the files in the same directory,
|
|
|
|
// there it should be a filename only
|
2018-05-07 19:03:51 +00:00
|
|
|
fn is_colocated_asset_link(link: &str) -> bool {
|
2018-09-30 19:15:09 +00:00
|
|
|
!link.contains('/') // http://, ftp://, ../ etc
|
2018-05-07 19:03:51 +00:00
|
|
|
&& !link.starts_with("mailto:")
|
|
|
|
}
|
|
|
|
|
2019-01-05 14:37:24 +00:00
|
|
|
fn fix_link(link: &str, context: &RenderContext) -> Result<String> {
|
|
|
|
// 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
|
|
|
|
let result = if link.starts_with("./") {
|
|
|
|
match resolve_internal_link(&link, context.permalinks) {
|
|
|
|
Ok(url) => url,
|
|
|
|
Err(_) => {
|
|
|
|
return Err(format!("Relative link {} not found.", link).into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if is_colocated_asset_link(&link) {
|
|
|
|
format!("{}{}", context.current_page_permalink, link)
|
|
|
|
} else if context.config.check_external_links
|
|
|
|
&& !link.starts_with('#')
|
2019-01-12 08:55:52 +00:00
|
|
|
&& !link.starts_with("mailto:")
|
|
|
|
{
|
2019-01-05 14:37:24 +00:00
|
|
|
let res = check_url(&link);
|
|
|
|
if res.is_valid() {
|
|
|
|
link.to_string()
|
|
|
|
} else {
|
2019-02-09 18:54:46 +00:00
|
|
|
return Err(format!("Link {} is not valid: {}", link, res.message()).into());
|
2019-01-05 14:37:24 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
link.to_string()
|
|
|
|
};
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
2019-01-12 08:55:52 +00:00
|
|
|
/// get only text in a slice of events
|
|
|
|
fn get_text(parser_slice: &[Event]) -> String {
|
|
|
|
let mut title = String::new();
|
2019-01-05 15:30:53 +00:00
|
|
|
|
2019-01-12 08:55:52 +00:00
|
|
|
for event in parser_slice.iter() {
|
|
|
|
if let Event::Text(text) = event {
|
|
|
|
title += text;
|
|
|
|
}
|
2019-01-05 15:30:53 +00:00
|
|
|
}
|
2019-01-12 08:55:52 +00:00
|
|
|
|
|
|
|
title
|
2019-01-05 15:30:53 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 14:46:18 +00:00
|
|
|
fn get_header_refs(events: &[Event]) -> Vec<HeaderRef> {
|
|
|
|
let mut header_refs = vec![];
|
2019-01-12 08:55:52 +00:00
|
|
|
|
|
|
|
for (i, event) in events.iter().enumerate() {
|
|
|
|
match event {
|
|
|
|
Event::Start(Tag::Header(level)) => {
|
2019-01-18 14:46:18 +00:00
|
|
|
header_refs.push(HeaderRef::new(i, *level));
|
2019-01-12 08:55:52 +00:00
|
|
|
}
|
|
|
|
Event::End(Tag::Header(_)) => {
|
|
|
|
let msg = "Header end before start?";
|
2019-01-18 14:46:18 +00:00
|
|
|
header_refs.last_mut().expect(msg).end_idx = i;
|
2019-01-12 08:55:52 +00:00
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
2019-01-05 14:37:24 +00:00
|
|
|
}
|
2019-01-12 08:55:52 +00:00
|
|
|
|
2019-01-18 14:46:18 +00:00
|
|
|
header_refs
|
2019-01-05 14:37:24 +00:00
|
|
|
}
|
|
|
|
|
2018-08-22 16:34:32 +00:00
|
|
|
pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<Rendered> {
|
2018-05-06 20:58:39 +00:00
|
|
|
// 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
|
|
|
|
2018-09-09 20:02:38 +00:00
|
|
|
let mut background = IncludeBackground::Yes;
|
2018-10-09 12:33:43 +00:00
|
|
|
let mut highlighter: Option<(HighlightLines, bool)> = None;
|
2019-01-12 08:55:52 +00:00
|
|
|
|
|
|
|
let mut inserted_anchors: Vec<String> = vec![];
|
2019-01-27 23:34:18 +00:00
|
|
|
let mut headers: Vec<Header> = vec![];
|
2017-06-16 04:00:48 +00:00
|
|
|
|
2017-04-19 13:16:21 +00:00
|
|
|
let mut opts = Options::empty();
|
2018-08-22 16:34:32 +00:00
|
|
|
let mut has_summary = false;
|
2018-11-07 18:42:15 +00:00
|
|
|
opts.insert(Options::ENABLE_TABLES);
|
|
|
|
opts.insert(Options::ENABLE_FOOTNOTES);
|
2017-04-19 13:16:21 +00:00
|
|
|
|
2017-03-27 14:17:33 +00:00
|
|
|
{
|
2019-02-09 18:54:46 +00:00
|
|
|
let mut events = Parser::new_ext(content, opts)
|
|
|
|
.map(|event| {
|
|
|
|
match event {
|
|
|
|
Event::Text(text) => {
|
|
|
|
// if we are in the middle of a code block
|
|
|
|
if let Some((ref mut highlighter, in_extra)) = highlighter {
|
|
|
|
let highlighted = if in_extra {
|
|
|
|
if let Some(ref extra) = context.config.extra_syntax_set {
|
|
|
|
highlighter.highlight(&text, &extra)
|
|
|
|
} else {
|
|
|
|
unreachable!(
|
|
|
|
"Got a highlighter from extra syntaxes but no extra?"
|
|
|
|
);
|
|
|
|
}
|
2018-10-09 12:33:43 +00:00
|
|
|
} else {
|
2019-02-09 18:54:46 +00:00
|
|
|
highlighter.highlight(&text, &SYNTAX_SET)
|
|
|
|
};
|
|
|
|
//let highlighted = &highlighter.highlight(&text, ss);
|
|
|
|
let html = styled_line_to_highlighted_html(&highlighted, background);
|
|
|
|
return Event::Html(Owned(html));
|
|
|
|
}
|
2017-10-23 12:18:05 +00:00
|
|
|
|
2019-02-09 18:54:46 +00:00
|
|
|
// Business as usual
|
|
|
|
Event::Text(text)
|
2017-03-27 14:17:33 +00:00
|
|
|
}
|
2019-02-09 18:54:46 +00:00
|
|
|
Event::Start(Tag::CodeBlock(ref info)) => {
|
|
|
|
if !context.config.highlight_code {
|
|
|
|
return Event::Html(Borrowed("<pre><code>"));
|
|
|
|
}
|
2018-05-07 16:38:04 +00:00
|
|
|
|
2019-02-09 18:54:46 +00:00
|
|
|
let theme = &THEME_SET.themes[&context.config.highlight_theme];
|
|
|
|
highlighter = Some(get_highlighter(info, &context.config));
|
|
|
|
// This selects the background color the same way that start_coloured_html_snippet does
|
|
|
|
let color = theme
|
|
|
|
.settings
|
|
|
|
.background
|
|
|
|
.unwrap_or(::syntect::highlighting::Color::WHITE);
|
|
|
|
background = IncludeBackground::IfDifferent(color);
|
|
|
|
let snippet = start_highlighted_html_snippet(theme);
|
|
|
|
Event::Html(Owned(snippet.0))
|
2017-03-27 14:17:33 +00:00
|
|
|
}
|
2019-02-09 18:54:46 +00:00
|
|
|
Event::End(Tag::CodeBlock(_)) => {
|
|
|
|
if !context.config.highlight_code {
|
|
|
|
return Event::Html(Borrowed("</code></pre>\n"));
|
|
|
|
}
|
|
|
|
// reset highlight and close the code block
|
|
|
|
highlighter = None;
|
|
|
|
Event::Html(Borrowed("</pre>"))
|
2018-05-07 19:23:27 +00:00
|
|
|
}
|
2019-02-09 18:54:46 +00:00
|
|
|
Event::Start(Tag::Image(src, title)) => {
|
|
|
|
if is_colocated_asset_link(&src) {
|
|
|
|
return Event::Start(Tag::Image(
|
|
|
|
Owned(format!("{}{}", context.current_page_permalink, src)),
|
|
|
|
title,
|
|
|
|
));
|
2018-07-16 19:13:00 +00:00
|
|
|
}
|
2018-05-07 19:03:51 +00:00
|
|
|
|
2019-02-09 18:54:46 +00:00
|
|
|
Event::Start(Tag::Image(src, title))
|
|
|
|
}
|
|
|
|
Event::Start(Tag::Link(link, title)) => {
|
|
|
|
let fixed_link = match fix_link(&link, context) {
|
|
|
|
Ok(fixed_link) => fixed_link,
|
|
|
|
Err(err) => {
|
|
|
|
error = Some(err);
|
|
|
|
return Event::Html(Borrowed(""));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Event::Start(Tag::Link(Owned(fixed_link), title))
|
|
|
|
}
|
|
|
|
Event::Html(ref markup) if markup.contains("<!-- more -->") => {
|
|
|
|
has_summary = true;
|
|
|
|
Event::Html(Borrowed(CONTINUE_READING))
|
|
|
|
}
|
|
|
|
_ => event,
|
2018-08-22 16:34:32 +00:00
|
|
|
}
|
2019-02-09 18:54:46 +00:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>(); // We need to collect the events to make a second pass
|
2019-01-12 08:55:52 +00:00
|
|
|
|
2019-01-18 14:46:18 +00:00
|
|
|
let header_refs = get_header_refs(&events);
|
2019-01-12 08:55:52 +00:00
|
|
|
|
|
|
|
let mut anchors_to_insert = vec![];
|
|
|
|
|
2019-01-18 14:46:18 +00:00
|
|
|
for header_ref in header_refs {
|
|
|
|
let start_idx = header_ref.start_idx;
|
|
|
|
let end_idx = header_ref.end_idx;
|
2019-02-09 18:54:46 +00:00
|
|
|
let title = get_text(&events[start_idx + 1..end_idx]);
|
2019-01-12 08:55:52 +00:00
|
|
|
let id = find_anchor(&inserted_anchors, slugify(&title), 0);
|
|
|
|
inserted_anchors.push(id.clone());
|
|
|
|
|
|
|
|
// insert `id` to the tag
|
2019-01-18 14:46:18 +00:00
|
|
|
let html = format!("<h{lvl} id=\"{id}\">", lvl = header_ref.level, id = id);
|
2019-01-12 08:55:52 +00:00
|
|
|
events[start_idx] = Event::Html(Owned(html));
|
|
|
|
|
|
|
|
// generate anchors and places to insert them
|
|
|
|
if context.insert_anchor != InsertAnchor::None {
|
|
|
|
let anchor_idx = match context.insert_anchor {
|
|
|
|
InsertAnchor::Left => start_idx + 1,
|
|
|
|
InsertAnchor::Right => end_idx,
|
|
|
|
InsertAnchor::None => 0, // Not important
|
|
|
|
};
|
|
|
|
let mut c = tera::Context::new();
|
|
|
|
c.insert("id", &id);
|
2019-01-23 18:20:02 +00:00
|
|
|
|
2019-02-09 18:54:46 +00:00
|
|
|
let anchor_link = utils::templates::render_template(
|
|
|
|
&ANCHOR_LINK_TEMPLATE,
|
|
|
|
context.tera,
|
|
|
|
c,
|
|
|
|
&None,
|
|
|
|
)
|
|
|
|
.map_err(|e| Error::chain("Failed to render anchor link template", e))?;
|
2019-01-12 08:55:52 +00:00
|
|
|
anchors_to_insert.push((anchor_idx, Event::Html(Owned(anchor_link))));
|
|
|
|
}
|
|
|
|
|
|
|
|
// record header to make table of contents
|
|
|
|
let permalink = format!("{}#{}", context.current_page_permalink, id);
|
2019-01-27 23:34:18 +00:00
|
|
|
let h = Header { level: header_ref.level, id, permalink, title, children: Vec::new() };
|
|
|
|
headers.push(h);
|
2019-01-12 08:55:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if context.insert_anchor != InsertAnchor::None {
|
|
|
|
events.insert_many(anchors_to_insert);
|
|
|
|
}
|
2017-03-07 12:34:31 +00:00
|
|
|
|
2019-01-12 08:55:52 +00:00
|
|
|
cmark::html::push_html(&mut html, events.into_iter());
|
2017-03-07 12:34:31 +00:00
|
|
|
}
|
2017-03-23 05:11:24 +00:00
|
|
|
|
2018-08-22 16:34:32 +00:00
|
|
|
if let Some(e) = error {
|
2018-09-30 19:15:09 +00:00
|
|
|
return Err(e);
|
2018-08-22 16:34:32 +00:00
|
|
|
} else {
|
|
|
|
Ok(Rendered {
|
|
|
|
summary_len: if has_summary { html.find(CONTINUE_READING) } else { None },
|
|
|
|
body: html,
|
2019-01-27 23:34:18 +00:00
|
|
|
toc: make_table_of_contents(headers),
|
2018-08-22 16:34:32 +00:00
|
|
|
})
|
2017-03-27 14:17:33 +00:00
|
|
|
}
|
2017-03-07 12:34:31 +00:00
|
|
|
}
|