Skip to content

Commit

Permalink
fix windows EOL in markdown files (#1911)
Browse files Browse the repository at this point in the history
* 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 pulldown-cmark/pulldown-cmark#457
  • Loading branch information
agrandville authored Jun 23, 2022
1 parent 4f6a1c6 commit 18e8246
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
16 changes: 10 additions & 6 deletions components/markdown/src/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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()
Expand All @@ -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("</code></pre>\n".into()));
Expand Down
10 changes: 10 additions & 0 deletions components/markdown/tests/codeblocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: components/markdown/tests/codeblocks.rs
assertion_line: 248
expression: body
---
<pre data-linenos style="background-color:#2b303b;color:#c0c5ce;"><code><table><tbody><tr><td>1</td><td><span>foo
</span></td></tr><tr><td>2</td><td><span>bar
</span></td></tr></tbody></table></code></pre>

0 comments on commit 18e8246

Please sign in to comment.