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.

226 lines
9.4KB

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