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
This commit is contained in:
Arnaud Grandville 2022-06-23 19:36:33 +02:00 committed by GitHub
parent 4f6a1c6bcc
commit 18e8246fbc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 6 deletions

View file

@ -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("</code></pre>\n".into()));

View file

@ -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(

View file

@ -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>