From 31983cae6c8e646152c75a86b3682ab63a7b0897 Mon Sep 17 00:00:00 2001 From: Michal Budzynski Date: Fri, 11 Aug 2017 11:29:14 +0200 Subject: [PATCH] fixed missing playpen css class when codeblock properties had whitespace --- src/utils/mod.rs | 88 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 2dac509e..aa2c3f89 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -17,7 +17,8 @@ pub fn render_markdown(text: &str, curly_quotes: bool) -> String { let p = Parser::new_ext(text, opts); let mut converter = EventQuoteConverter::new(curly_quotes); - let events = p.map(|event| converter.convert(event)); + let events = p.map(clean_codeblock_headers).map(|event| converter.convert(event)); + html::push_html(&mut s, events); s } @@ -54,6 +55,21 @@ impl EventQuoteConverter { } } +fn clean_codeblock_headers(event: Event) -> Event { + match event { + Event::Start(Tag::CodeBlock(ref info)) => { + let info: String = info + .chars() + .filter(|ch| !ch.is_whitespace()) + .collect(); + + Event::Start(Tag::CodeBlock(Cow::from(info))) + }, + _ => event, + } +} + + fn convert_quotes_to_curly(original_text: &str) -> String { // We'll consider the start to be "whitespace". let mut preceded_by_whitespace = true; @@ -99,6 +115,76 @@ mod tests { "#; assert_eq!(render_markdown(input, true), expected); } + + #[test] + fn whitespace_outside_of_codeblock_header_is_preserved() { + let input = r#" +some text with spaces +```rust +fn main() { +// code inside is unchanged +} +``` +more text with spaces +"#; + + let expected = r#"

some text with spaces

+
fn main() {
+// code inside is unchanged
+}
+
+

more text with spaces

+"#; + assert_eq!(render_markdown(input, false), expected); + assert_eq!(render_markdown(input, true), expected); + } + + #[test] + fn rust_code_block_properties_are_passed_as_space_delimited_class() { + let input = r#" +```rust,no_run,should_panic,property_3 +``` +"#; + + let expected = r#"
+"#; + assert_eq!(render_markdown(input, false), expected); + assert_eq!(render_markdown(input, true), expected); + } + + #[test] + fn rust_code_block_properties_with_whitespace_are_passed_as_space_delimited_class() { + let input = r#" +```rust, no_run,,,should_panic , ,property_3 +``` +"#; + + let expected = r#"
+"#; + assert_eq!(render_markdown(input, false), expected); + assert_eq!(render_markdown(input, true), expected); + } + + #[test] + fn rust_code_block_without_properties_has_proper_html_class() { + let input = r#" +```rust +``` +"#; + + let expected = r#"
+"#; + assert_eq!(render_markdown(input, false), expected); + assert_eq!(render_markdown(input, true), expected); + + let input = r#" +```rust +``` +"#; + assert_eq!(render_markdown(input, false), expected); + assert_eq!(render_markdown(input, true), expected); + + } } mod convert_quotes_to_curly {