Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix windows EOL in markdown files #1911

Merged
merged 2 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>