Browse Source

Move highlight decision into the markdown_to_html fn

index-subcmd
Vincent Prouillet 7 years ago
parent
commit
58208df35a
2 changed files with 15 additions and 14 deletions
  1. +13
    -1
      src/markdown.rs
  2. +2
    -13
      src/page.rs

+ 13
- 1
src/markdown.rs View File

@@ -94,8 +94,20 @@ impl<'a> Iterator for CodeHighlightingParser<'a> {
}

pub fn markdown_to_html(content: &str, highlight_code: bool, highlight_theme: &str) -> String {
// We try to be smart about highlighting code as it can be time-consuming
// If the global config disables it, then we do nothing. However,
// if we see a code block in the content, we assume that this page needs
// to be highlighted. It could potentially have false positive if the content
// has ``` in it but that seems kind of unlikely
let should_highlight = if highlight_code {
content.contains("```")
} else {
false
};


let mut html = String::new();
if highlight_code {
if should_highlight {
let parser = CodeHighlightingParser::new(Parser::new(content), highlight_theme);
cmark::html::push_html(&mut html, parser);
} else {


+ 2
- 13
src/page.rs View File

@@ -131,24 +131,13 @@ impl Page {
page.parent_path = page.file_path.parent().unwrap().to_path_buf();
page.raw_content = content;

// We try to be smart about highlighting code as it can be time-consuming
// If the global config disables it, then we do nothing. However,
// if we see a code block in the content, we assume that this page needs
// to be highlighted. It could potentially have false positive if the content
// has ``` in it but that seems kind of unlikely
let should_highlight = if config.highlight_code.unwrap() {
page.raw_content.contains("```")
} else {
false
};

let highlight_theme = config.highlight_theme.clone().unwrap();
page.content = markdown_to_html(&page.raw_content, should_highlight, &highlight_theme);
page.content = markdown_to_html(&page.raw_content, config.highlight_code.unwrap(), &highlight_theme);

if page.raw_content.contains("<!-- more -->") {
page.summary = {
let summary = page.raw_content.splitn(2, "<!-- more -->").collect::<Vec<&str>>()[0];
markdown_to_html(summary, should_highlight, &highlight_theme)
markdown_to_html(summary, config.highlight_code.unwrap(), &highlight_theme)
}
}



Loading…
Cancel
Save