From 58208df35acea8c103ede52c2b500733034725ec Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Thu, 23 Mar 2017 14:11:24 +0900 Subject: [PATCH] Move highlight decision into the markdown_to_html fn --- src/markdown.rs | 14 +++++++++++++- src/page.rs | 15 ++------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/markdown.rs b/src/markdown.rs index 2c3d3f4..0c17a86 100644 --- a/src/markdown.rs +++ b/src/markdown.rs @@ -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 { diff --git a/src/page.rs b/src/page.rs index 721e8f1..71c1281 100644 --- a/src/page.rs +++ b/src/page.rs @@ -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("") { page.summary = { let summary = page.raw_content.splitn(2, "").collect::>()[0]; - markdown_to_html(summary, should_highlight, &highlight_theme) + markdown_to_html(summary, config.highlight_code.unwrap(), &highlight_theme) } }