Move highlight decision into the markdown_to_html fn

This commit is contained in:
Vincent Prouillet 2017-03-23 14:11:24 +09:00
parent 3cc5e0cf28
commit 58208df35a
2 changed files with 15 additions and 14 deletions

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 { 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(); let mut html = String::new();
if highlight_code { if should_highlight {
let parser = CodeHighlightingParser::new(Parser::new(content), highlight_theme); let parser = CodeHighlightingParser::new(Parser::new(content), highlight_theme);
cmark::html::push_html(&mut html, parser); cmark::html::push_html(&mut html, parser);
} else { } else {

View file

@ -131,24 +131,13 @@ impl Page {
page.parent_path = page.file_path.parent().unwrap().to_path_buf(); page.parent_path = page.file_path.parent().unwrap().to_path_buf();
page.raw_content = content; 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(); 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 -->") { if page.raw_content.contains("<!-- more -->") {
page.summary = { page.summary = {
let summary = page.raw_content.splitn(2, "<!-- more -->").collect::<Vec<&str>>()[0]; 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)
} }
} }