From 18e8246fbc25bcb01677bd94909c46eb66bd0390 Mon Sep 17 00:00:00 2001 From: Arnaud Grandville Date: Thu, 23 Jun 2022 19:36:33 +0200 Subject: [PATCH] fix windows EOL in markdown files (#1911) * add a test * CodeBlock text may be split on multiple parts The CodeBlock text events are now concatenated and processed in a single stream at the end. cf https://github.com/raphlinus/pulldown-cmark/issues/457 --- components/markdown/src/markdown.rs | 16 ++++++++++------ components/markdown/tests/codeblocks.rs | 10 ++++++++++ ...blocks__can_add_line_numbers_windows_eol.snap | 9 +++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 components/markdown/tests/snapshots/codeblocks__can_add_line_numbers_windows_eol.snap diff --git a/components/markdown/src/markdown.rs b/components/markdown/src/markdown.rs index 4b1c9bd7..efbac362 100644 --- a/components/markdown/src/markdown.rs +++ b/components/markdown/src/markdown.rs @@ -317,13 +317,12 @@ pub fn markdown_to_html( }; } + let mut accumulated_block = String::new(); for (event, mut range) in Parser::new_ext(content, opts).into_offset_iter() { match event { Event::Text(text) => { - if let Some(ref mut code_block) = code_block { - let html; + if let Some(ref mut _code_block) = code_block { if contains_shortcode(text.as_ref()) { - let mut accumulated_block = String::new(); // mark the start of the code block events let stack_start = events.len(); render_shortcodes!(true, text, range); @@ -341,13 +340,12 @@ pub fn markdown_to_html( } } } - html = code_block.highlight(&accumulated_block); + // remove all the original events from shortcode rendering events.truncate(stack_start); } else { - html = code_block.highlight(&text); + accumulated_block += &text; } - events.push(Event::Html(html.into())); } else { let text = if context.config.markdown.render_emoji { EMOJI_REPLACER.replace_all(&text).to_string().into() @@ -373,6 +371,12 @@ pub fn markdown_to_html( events.push(Event::Html(begin.into())); } Event::End(Tag::CodeBlock(_)) => { + if let Some(ref mut code_block) = code_block { + let html = code_block.highlight(&accumulated_block); + events.push(Event::Html(html.into())); + accumulated_block.clear(); + } + // reset highlight and close the code block code_block = None; events.push(Event::Html("\n".into())); diff --git a/components/markdown/tests/codeblocks.rs b/components/markdown/tests/codeblocks.rs index 6aa9dc74..d12e0f32 100644 --- a/components/markdown/tests/codeblocks.rs +++ b/components/markdown/tests/codeblocks.rs @@ -239,6 +239,16 @@ bar insta::assert_snapshot!(body); } +#[test] +fn can_add_line_numbers_windows_eol() { + let body = render_codeblock( + "```linenos\r\nfoo\r\nbar\r\n```\r\n", + true, + ); + insta::assert_snapshot!(body); +} + + #[test] fn can_add_line_numbers_with_lineno_start() { let body = render_codeblock( diff --git a/components/markdown/tests/snapshots/codeblocks__can_add_line_numbers_windows_eol.snap b/components/markdown/tests/snapshots/codeblocks__can_add_line_numbers_windows_eol.snap new file mode 100644 index 00000000..1e79755a --- /dev/null +++ b/components/markdown/tests/snapshots/codeblocks__can_add_line_numbers_windows_eol.snap @@ -0,0 +1,9 @@ +--- +source: components/markdown/tests/codeblocks.rs +assertion_line: 248 +expression: body +--- +
1foo +
2bar +
+