= vec![];
let mut internal_links_with_anchors = Vec::new();
let mut external_links = Vec::new();
let mut opts = Options::empty();
let mut has_summary = false;
opts.insert(Options::ENABLE_TABLES);
opts.insert(Options::ENABLE_FOOTNOTES);
opts.insert(Options::ENABLE_STRIKETHROUGH);
{
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?"
);
}
} else {
highlighter.highlight(&text, &SYNTAX_SET)
};
//let highlighted = &highlighter.highlight(&text, ss);
let html = styled_line_to_highlighted_html(&highlighted, background);
return Event::Html(html.into());
}
// Business as usual
Event::Text(text)
}
Event::Start(Tag::CodeBlock(ref info)) => {
if !context.config.highlight_code {
return Event::Html("".into());
}
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(snippet.0.into())
}
Event::End(Tag::CodeBlock(_)) => {
if !context.config.highlight_code {
return Event::Html("
\n".into());
}
// reset highlight and close the code block
highlighter = None;
Event::Html("".into())
}
Event::Start(Tag::Image(link_type, src, title)) => {
if is_colocated_asset_link(&src) {
let link = format!("{}{}", context.current_page_permalink, &*src);
return Event::Start(Tag::Image(link_type, link.into(), title));
}
Event::Start(Tag::Image(link_type, src, title))
}
Event::Start(Tag::Link(link_type, link, title)) if link.is_empty() => {
error = Some(Error::msg("There is a link that is missing a URL"));
Event::Start(Tag::Link(link_type, "#".into(), title))
}
Event::Start(Tag::Link(link_type, link, title)) => {
let fixed_link = match fix_link(
link_type,
&link,
context,
&mut internal_links_with_anchors,
&mut external_links,
) {
Ok(fixed_link) => fixed_link,
Err(err) => {
error = Some(err);
return Event::Html("".into());
}
};
Event::Start(Tag::Link(link_type, fixed_link.into(), title))
}
Event::Html(ref markup) if markup.contains("") => {
has_summary = true;
Event::Html(CONTINUE_READING.into())
}
_ => event,
}
})
.collect::>(); // We need to collect the events to make a second pass
let mut heading_refs = get_heading_refs(&events);
let mut anchors_to_insert = vec![];
// First heading pass: look for a manually-specified IDs, e.g. `# Heading text {#hash}`
// (This is a separate first pass so that auto IDs can avoid collisions with manual IDs.)
for heading_ref in heading_refs.iter_mut() {
let end_idx = heading_ref.end_idx;
if let Event::Text(ref mut text) = events[end_idx - 1] {
if text.as_bytes().last() == Some(&b'}') {
if let Some(mut i) = text.find("{#") {
let id = text[i + 2..text.len() - 1].to_owned();
inserted_anchors.push(id.clone());
while i > 0 && text.as_bytes()[i - 1] == b' ' {
i -= 1;
}
heading_ref.id = Some(id);
*text = text[..i].to_owned().into();
}
}
}
}
// Second heading pass: auto-generate remaining IDs, and emit HTML
for heading_ref in heading_refs {
let start_idx = heading_ref.start_idx;
let end_idx = heading_ref.end_idx;
let title = get_text(&events[start_idx + 1..end_idx]);
let id = heading_ref.id.unwrap_or_else(|| {
find_anchor(
&inserted_anchors,
slugify_anchors(&title, context.config.slugify.anchors),
0,
)
});
inserted_anchors.push(id.clone());
// insert `id` to the tag
let html = format!("", lvl = heading_ref.level, id = id);
events[start_idx] = Event::Html(html.into());
// 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);
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))?;
anchors_to_insert.push((anchor_idx, Event::Html(anchor_link.into())));
}
// record heading to make table of contents
let permalink = format!("{}#{}", context.current_page_permalink, id);
let h =
Heading { level: heading_ref.level, id, permalink, title, children: Vec::new() };
headings.push(h);
}
if context.insert_anchor != InsertAnchor::None {
events.insert_many(anchors_to_insert);
}
cmark::html::push_html(&mut html, events.into_iter());
}
if let Some(e) = error {
Err(e)
} else {
Ok(Rendered {
summary_len: if has_summary { html.find(CONTINUE_READING) } else { None },
body: html,
toc: make_table_of_contents(headings),
internal_links_with_anchors,
external_links,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_starts_with_schema() {
// registered
assert!(starts_with_schema("https://example.com/"));
assert!(starts_with_schema("ftp://example.com/"));
assert!(starts_with_schema("mailto:user@example.com"));
assert!(starts_with_schema("xmpp:node@example.com"));
assert!(starts_with_schema("tel:18008675309"));
assert!(starts_with_schema("sms:18008675309"));
assert!(starts_with_schema("h323:user@example.com"));
// arbitrary
assert!(starts_with_schema("zola:post?content=hi"));
// case-insensitive
assert!(starts_with_schema("MailTo:user@example.com"));
assert!(starts_with_schema("MAILTO:user@example.com"));
}
#[test]
fn test_is_external_link() {
assert!(is_external_link("http://example.com/"));
assert!(is_external_link("https://example.com/"));
assert!(is_external_link("https://example.com/index.html#introduction"));
assert!(!is_external_link("mailto:user@example.com"));
assert!(!is_external_link("tel:18008675309"));
assert!(!is_external_link("#introduction"));
assert!(!is_external_link("http.jpg"))
}
}