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.

248 lines
10KB

  1. use std::borrow::Cow::{Owned, Borrowed};
  2. use pulldown_cmark as cmark;
  3. use self::cmark::{Parser, Event, Tag, Options, OPTION_ENABLE_TABLES, OPTION_ENABLE_FOOTNOTES};
  4. use slug::slugify;
  5. use syntect::easy::HighlightLines;
  6. use syntect::html::{start_highlighted_html_snippet, styled_line_to_highlighted_html, IncludeBackground};
  7. use errors::Result;
  8. use utils::site::resolve_internal_link;
  9. use config::highlighting::{get_highlighter, THEME_SET, SYNTAX_SET};
  10. use link_checker::check_url;
  11. use table_of_contents::{TempHeader, Header, make_table_of_contents};
  12. use context::RenderContext;
  13. const CONTINUE_READING: &str = "<p><a name=\"continue-reading\"></a></p>\n";
  14. #[derive(Debug)]
  15. pub struct Rendered {
  16. pub body: String,
  17. pub summary_len: Option<usize>,
  18. pub toc: Vec<Header>,
  19. }
  20. // We might have cases where the slug is already present in our list of anchor
  21. // for example an article could have several titles named Example
  22. // We add a counter after the slug if the slug is already present, which
  23. // means we will have example, example-1, example-2 etc
  24. fn find_anchor(anchors: &[String], name: String, level: u8) -> String {
  25. if level == 0 && !anchors.contains(&name) {
  26. return name.to_string();
  27. }
  28. let new_anchor = format!("{}-{}", name, level + 1);
  29. if !anchors.contains(&new_anchor) {
  30. return new_anchor;
  31. }
  32. find_anchor(anchors, name, level + 1)
  33. }
  34. fn is_colocated_asset_link(link: &str) -> bool {
  35. !link.contains('/') // http://, ftp://, ../ etc
  36. && !link.starts_with("mailto:")
  37. }
  38. pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<Rendered> {
  39. // the rendered html
  40. let mut html = String::with_capacity(content.len());
  41. // Set while parsing
  42. let mut error = None;
  43. let mut background = IncludeBackground::Yes;
  44. let mut highlighter: Option<(HighlightLines, bool)> = None;
  45. // If we get text in header, we need to insert the id and a anchor
  46. let mut in_header = false;
  47. // pulldown_cmark can send several text events for a title if there are markdown
  48. // specific characters like `!` in them. We only want to insert the anchor the first time
  49. let mut header_created = false;
  50. let mut anchors: Vec<String> = vec![];
  51. let mut headers = vec![];
  52. // Defaults to a 0 level so not a real header
  53. // It should be an Option ideally but not worth the hassle to update
  54. let mut temp_header = TempHeader::default();
  55. let mut opts = Options::empty();
  56. let mut has_summary = false;
  57. opts.insert(OPTION_ENABLE_TABLES);
  58. opts.insert(OPTION_ENABLE_FOOTNOTES);
  59. {
  60. let parser = Parser::new_ext(content, opts).map(|event| {
  61. match event {
  62. Event::Text(text) => {
  63. // Header first
  64. if in_header {
  65. if header_created {
  66. temp_header.add_text(&text);
  67. return Event::Html(Borrowed(""));
  68. }
  69. // += as we might have some <code> or other things already there
  70. temp_header.add_text(&text);
  71. header_created = true;
  72. return Event::Html(Borrowed(""));
  73. }
  74. // if we are in the middle of a code block
  75. if let Some((ref mut highlighter, in_extra)) = highlighter {
  76. let highlighted = if in_extra {
  77. if let Some(ref extra) = context.config.extra_syntax_set {
  78. highlighter.highlight(&text, &extra)
  79. } else {
  80. unreachable!("Got a highlighter from extra syntaxes but no extra?");
  81. }
  82. } else {
  83. highlighter.highlight(&text, &SYNTAX_SET)
  84. };
  85. //let highlighted = &highlighter.highlight(&text, ss);
  86. let html = styled_line_to_highlighted_html(&highlighted, background);
  87. return Event::Html(Owned(html));
  88. }
  89. // Business as usual
  90. Event::Text(text)
  91. }
  92. Event::Start(Tag::CodeBlock(ref info)) => {
  93. if !context.config.highlight_code {
  94. return Event::Html(Borrowed("<pre><code>"));
  95. }
  96. let theme = &THEME_SET.themes[&context.config.highlight_theme];
  97. highlighter = Some(get_highlighter(info, &context.config));
  98. // This selects the background color the same way that start_coloured_html_snippet does
  99. let color = theme.settings.background.unwrap_or(::syntect::highlighting::Color::WHITE);
  100. background = IncludeBackground::IfDifferent(color);
  101. let snippet = start_highlighted_html_snippet(theme);
  102. Event::Html(Owned(snippet.0))
  103. }
  104. Event::End(Tag::CodeBlock(_)) => {
  105. if !context.config.highlight_code {
  106. return Event::Html(Borrowed("</code></pre>\n"));
  107. }
  108. // reset highlight and close the code block
  109. highlighter = None;
  110. Event::Html(Borrowed("</pre>"))
  111. }
  112. Event::Start(Tag::Image(src, title)) => {
  113. if is_colocated_asset_link(&src) {
  114. return Event::Start(
  115. Tag::Image(
  116. Owned(format!("{}{}", context.current_page_permalink, src)),
  117. title,
  118. )
  119. );
  120. }
  121. Event::Start(Tag::Image(src, title))
  122. }
  123. Event::Start(Tag::Link(link, title)) => {
  124. // A few situations here:
  125. // - it could be a relative link (starting with `./`)
  126. // - it could be a link to a co-located asset
  127. // - it could be a normal link
  128. // - any of those can be in a header or not: if it's in a header
  129. // we need to append to a string
  130. let fixed_link = if link.starts_with("./") {
  131. match resolve_internal_link(&link, context.permalinks) {
  132. Ok(url) => url,
  133. Err(_) => {
  134. error = Some(format!("Relative link {} not found.", link).into());
  135. return Event::Html(Borrowed(""));
  136. }
  137. }
  138. } else if is_colocated_asset_link(&link) {
  139. format!("{}{}", context.current_page_permalink, link)
  140. } else if context.config.check_external_links
  141. && !link.starts_with('#')
  142. && !link.starts_with("mailto:") {
  143. let res = check_url(&link);
  144. if res.is_valid() {
  145. link.to_string()
  146. } else {
  147. error = Some(
  148. format!("Link {} is not valid: {}", link, res.message()).into()
  149. );
  150. String::new()
  151. }
  152. } else {
  153. link.to_string()
  154. };
  155. if in_header {
  156. let html = if title.is_empty() {
  157. format!("<a href=\"{}\">", fixed_link)
  158. } else {
  159. format!("<a href=\"{}\" title=\"{}\">", fixed_link, title)
  160. };
  161. temp_header.add_html(&html);
  162. return Event::Html(Borrowed(""));
  163. }
  164. Event::Start(Tag::Link(Owned(fixed_link), title))
  165. }
  166. Event::End(Tag::Link(_, _)) => {
  167. if in_header {
  168. temp_header.add_html("</a>");
  169. return Event::Html(Borrowed(""));
  170. }
  171. event
  172. }
  173. Event::Start(Tag::Code) => {
  174. if in_header {
  175. temp_header.add_html("<code>");
  176. return Event::Html(Borrowed(""));
  177. }
  178. event
  179. }
  180. Event::End(Tag::Code) => {
  181. if in_header {
  182. temp_header.add_html("</code>");
  183. return Event::Html(Borrowed(""));
  184. }
  185. event
  186. }
  187. Event::Start(Tag::Header(num)) => {
  188. in_header = true;
  189. temp_header = TempHeader::new(num);
  190. Event::Html(Borrowed(""))
  191. }
  192. Event::End(Tag::Header(_)) => {
  193. // End of a header, reset all the things and return the header string
  194. let id = find_anchor(&anchors, slugify(&temp_header.title), 0);
  195. anchors.push(id.clone());
  196. temp_header.permalink = format!("{}#{}", context.current_page_permalink, id);
  197. temp_header.id = id;
  198. in_header = false;
  199. header_created = false;
  200. let val = temp_header.to_string(context.tera, context.insert_anchor);
  201. headers.push(temp_header.clone());
  202. temp_header = TempHeader::default();
  203. Event::Html(Owned(val))
  204. }
  205. Event::Html(ref markup) if markup.contains("<!-- more -->") => {
  206. has_summary = true;
  207. Event::Html(Borrowed(CONTINUE_READING))
  208. }
  209. _ => event,
  210. }
  211. });
  212. cmark::html::push_html(&mut html, parser);
  213. }
  214. if let Some(e) = error {
  215. return Err(e);
  216. } else {
  217. Ok(Rendered {
  218. summary_len: if has_summary { html.find(CONTINUE_READING) } else { None },
  219. body: html,
  220. toc: make_table_of_contents(&headers),
  221. })
  222. }
  223. }