You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

304 lines
11KB

  1. use pulldown_cmark as cmark;
  2. use slug::slugify;
  3. use syntect::easy::HighlightLines;
  4. use syntect::html::{
  5. start_highlighted_html_snippet, styled_line_to_highlighted_html, IncludeBackground,
  6. };
  7. use config::highlighting::{get_highlighter, SYNTAX_SET, THEME_SET};
  8. use context::RenderContext;
  9. use errors::{Error, Result};
  10. use front_matter::InsertAnchor;
  11. use table_of_contents::{make_table_of_contents, Header};
  12. use utils::site::resolve_internal_link;
  13. use utils::vec::InsertMany;
  14. use self::cmark::{Event, LinkType, Options, Parser, Tag};
  15. const CONTINUE_READING: &str =
  16. "<p id=\"zola-continue-reading\"><a name=\"continue-reading\"></a></p>\n";
  17. const ANCHOR_LINK_TEMPLATE: &str = "anchor-link.html";
  18. #[derive(Debug)]
  19. pub struct Rendered {
  20. pub body: String,
  21. pub summary_len: Option<usize>,
  22. pub toc: Vec<Header>,
  23. pub external_links: Vec<String>,
  24. }
  25. // tracks a header in a slice of pulldown-cmark events
  26. #[derive(Debug)]
  27. struct HeaderRef {
  28. start_idx: usize,
  29. end_idx: usize,
  30. level: i32,
  31. id: Option<String>,
  32. }
  33. impl HeaderRef {
  34. fn new(start: usize, level: i32) -> HeaderRef {
  35. HeaderRef { start_idx: start, end_idx: 0, level, id: None }
  36. }
  37. }
  38. // We might have cases where the slug is already present in our list of anchor
  39. // for example an article could have several titles named Example
  40. // We add a counter after the slug if the slug is already present, which
  41. // means we will have example, example-1, example-2 etc
  42. fn find_anchor(anchors: &[String], name: String, level: u8) -> String {
  43. if level == 0 && !anchors.contains(&name) {
  44. return name;
  45. }
  46. let new_anchor = format!("{}-{}", name, level + 1);
  47. if !anchors.contains(&new_anchor) {
  48. return new_anchor;
  49. }
  50. find_anchor(anchors, name, level + 1)
  51. }
  52. // Colocated asset links refers to the files in the same directory,
  53. // there it should be a filename only
  54. fn is_colocated_asset_link(link: &str) -> bool {
  55. !link.contains('/') // http://, ftp://, ../ etc
  56. && !link.starts_with("mailto:")
  57. }
  58. fn fix_link(link_type: LinkType, link: &str, context: &RenderContext, external_links: &mut Vec<String>) -> Result<String> {
  59. if link_type == LinkType::Email {
  60. return Ok(link.to_string());
  61. }
  62. // A few situations here:
  63. // - it could be a relative link (starting with `@/`)
  64. // - it could be a link to a co-located asset
  65. // - it could be a normal link
  66. let result = if link.starts_with("@/") {
  67. match resolve_internal_link(&link, context.permalinks) {
  68. Ok(url) => url,
  69. Err(_) => {
  70. return Err(format!("Relative link {} not found.", link).into());
  71. }
  72. }
  73. } else if is_colocated_asset_link(&link) {
  74. format!("{}{}", context.current_page_permalink, link)
  75. } else {
  76. if !link.starts_with('#') && !link.starts_with("mailto:") {
  77. external_links.push(link.to_owned());
  78. }
  79. link.to_string()
  80. };
  81. Ok(result)
  82. }
  83. /// get only text in a slice of events
  84. fn get_text(parser_slice: &[Event]) -> String {
  85. let mut title = String::new();
  86. for event in parser_slice.iter() {
  87. match event {
  88. Event::Text(text) | Event::Code(text) => title += text,
  89. _ => continue
  90. }
  91. }
  92. title
  93. }
  94. fn get_header_refs(events: &[Event]) -> Vec<HeaderRef> {
  95. let mut header_refs = vec![];
  96. for (i, event) in events.iter().enumerate() {
  97. match event {
  98. Event::Start(Tag::Header(level)) => {
  99. header_refs.push(HeaderRef::new(i, *level));
  100. }
  101. Event::End(Tag::Header(_)) => {
  102. let msg = "Header end before start?";
  103. header_refs.last_mut().expect(msg).end_idx = i;
  104. }
  105. _ => (),
  106. }
  107. }
  108. header_refs
  109. }
  110. pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<Rendered> {
  111. // the rendered html
  112. let mut html = String::with_capacity(content.len());
  113. // Set while parsing
  114. let mut error = None;
  115. let mut background = IncludeBackground::Yes;
  116. let mut highlighter: Option<(HighlightLines, bool)> = None;
  117. let mut inserted_anchors: Vec<String> = vec![];
  118. let mut headers: Vec<Header> = vec![];
  119. let mut external_links = Vec::new();
  120. let mut opts = Options::empty();
  121. let mut has_summary = false;
  122. opts.insert(Options::ENABLE_TABLES);
  123. opts.insert(Options::ENABLE_FOOTNOTES);
  124. {
  125. let mut events = Parser::new_ext(content, opts)
  126. .map(|event| {
  127. match event {
  128. Event::Text(text) => {
  129. // if we are in the middle of a code block
  130. if let Some((ref mut highlighter, in_extra)) = highlighter {
  131. let highlighted = if in_extra {
  132. if let Some(ref extra) = context.config.extra_syntax_set {
  133. highlighter.highlight(&text, &extra)
  134. } else {
  135. unreachable!(
  136. "Got a highlighter from extra syntaxes but no extra?"
  137. );
  138. }
  139. } else {
  140. highlighter.highlight(&text, &SYNTAX_SET)
  141. };
  142. //let highlighted = &highlighter.highlight(&text, ss);
  143. let html = styled_line_to_highlighted_html(&highlighted, background);
  144. return Event::Html(html.into());
  145. }
  146. // Business as usual
  147. Event::Text(text)
  148. }
  149. Event::Start(Tag::CodeBlock(ref info)) => {
  150. if !context.config.highlight_code {
  151. return Event::Html("<pre><code>".into());
  152. }
  153. let theme = &THEME_SET.themes[&context.config.highlight_theme];
  154. highlighter = Some(get_highlighter(info, &context.config));
  155. // This selects the background color the same way that start_coloured_html_snippet does
  156. let color = theme
  157. .settings
  158. .background
  159. .unwrap_or(::syntect::highlighting::Color::WHITE);
  160. background = IncludeBackground::IfDifferent(color);
  161. let snippet = start_highlighted_html_snippet(theme);
  162. Event::Html(snippet.0.into())
  163. }
  164. Event::End(Tag::CodeBlock(_)) => {
  165. if !context.config.highlight_code {
  166. return Event::Html("</code></pre>\n".into());
  167. }
  168. // reset highlight and close the code block
  169. highlighter = None;
  170. Event::Html("</pre>".into())
  171. }
  172. Event::Start(Tag::Image(link_type, src, title)) => {
  173. if is_colocated_asset_link(&src) {
  174. let link = format!("{}{}", context.current_page_permalink, &*src);
  175. return Event::Start(Tag::Image(link_type, link.into(), title));
  176. }
  177. Event::Start(Tag::Image(link_type, src, title))
  178. }
  179. Event::Start(Tag::Link(link_type, link, title)) => {
  180. let fixed_link = match fix_link(link_type, &link, context, &mut external_links) {
  181. Ok(fixed_link) => fixed_link,
  182. Err(err) => {
  183. error = Some(err);
  184. return Event::Html("".into());
  185. }
  186. };
  187. Event::Start(Tag::Link(link_type, fixed_link.into(), title))
  188. }
  189. Event::Html(ref markup) if markup.contains("<!-- more -->") => {
  190. has_summary = true;
  191. Event::Html(CONTINUE_READING.into())
  192. }
  193. _ => event,
  194. }
  195. })
  196. .collect::<Vec<_>>(); // We need to collect the events to make a second pass
  197. let mut header_refs = get_header_refs(&events);
  198. let mut anchors_to_insert = vec![];
  199. // First header pass: look for a manually-specified IDs, e.g. `# Heading text {#hash}`
  200. // (This is a separate first pass so that auto IDs can avoid collisions with manual IDs.)
  201. for header_ref in header_refs.iter_mut() {
  202. let end_idx = header_ref.end_idx;
  203. if let Event::Text(ref mut text) = events[end_idx - 1] {
  204. if text.as_bytes().last() == Some(&b'}') {
  205. if let Some(mut i) = text.find("{#") {
  206. let id = text[i + 2..text.len() - 1].to_owned();
  207. inserted_anchors.push(id.clone());
  208. while i > 0 && text.as_bytes()[i - 1] == b' ' {
  209. i -= 1;
  210. }
  211. header_ref.id = Some(id);
  212. *text = text[..i].to_owned().into();
  213. }
  214. }
  215. }
  216. }
  217. // Second header pass: auto-generate remaining IDs, and emit HTML
  218. for header_ref in header_refs {
  219. let start_idx = header_ref.start_idx;
  220. let end_idx = header_ref.end_idx;
  221. let title = get_text(&events[start_idx + 1..end_idx]);
  222. let id = header_ref.id.unwrap_or_else(
  223. || find_anchor(&inserted_anchors, slugify(&title), 0));
  224. inserted_anchors.push(id.clone());
  225. // insert `id` to the tag
  226. let html = format!("<h{lvl} id=\"{id}\">", lvl = header_ref.level, id = id);
  227. events[start_idx] = Event::Html(html.into());
  228. // generate anchors and places to insert them
  229. if context.insert_anchor != InsertAnchor::None {
  230. let anchor_idx = match context.insert_anchor {
  231. InsertAnchor::Left => start_idx + 1,
  232. InsertAnchor::Right => end_idx,
  233. InsertAnchor::None => 0, // Not important
  234. };
  235. let mut c = tera::Context::new();
  236. c.insert("id", &id);
  237. let anchor_link = utils::templates::render_template(
  238. &ANCHOR_LINK_TEMPLATE,
  239. context.tera,
  240. c,
  241. &None,
  242. )
  243. .map_err(|e| Error::chain("Failed to render anchor link template", e))?;
  244. anchors_to_insert.push((anchor_idx, Event::Html(anchor_link.into())));
  245. }
  246. // record header to make table of contents
  247. let permalink = format!("{}#{}", context.current_page_permalink, id);
  248. let h = Header { level: header_ref.level, id, permalink, title, children: Vec::new() };
  249. headers.push(h);
  250. }
  251. if context.insert_anchor != InsertAnchor::None {
  252. events.insert_many(anchors_to_insert);
  253. }
  254. cmark::html::push_html(&mut html, events.into_iter());
  255. }
  256. if let Some(e) = error {
  257. return Err(e);
  258. } else {
  259. Ok(Rendered {
  260. summary_len: if has_summary { html.find(CONTINUE_READING) } else { None },
  261. body: html,
  262. toc: make_table_of_contents(headers),
  263. external_links,
  264. })
  265. }
  266. }