Skip to content

Commit

Permalink
treat newline as space at start and end of inline code
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeando committed May 2, 2022
1 parent e224b15 commit c3c6707
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/cm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,11 +548,14 @@ impl<'a, 'o> CommonMarkFormatter<'a, 'o> {
for _ in 0..numticks {
write!(self, "`").unwrap();
}

let all_space = literal.iter().all(|&c| c==b' ' || c==b'\r' || c==b'\n');
let has_edge_space = literal[0] == b' ' || literal[literal.len() - 1] == b' ';
let has_edge_backtick = literal[0] == b'`' || literal[literal.len() - 1] == b'`';

let pad = literal.is_empty()
|| literal[0] == b'`'
|| literal[literal.len() - 1] == b'`'
|| literal[0] == b' '
|| literal[literal.len() - 1] == b' ';
|| has_edge_backtick
|| (!all_space && has_edge_space);
if pad {
write!(self, " ").unwrap();
}
Expand Down
17 changes: 16 additions & 1 deletion src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub fn normalize_code(v: &[u8]) -> Vec<u8> {
}
c => r.push(c),
}
if v[i] != b' ' {
if v[i] != b' ' && v[i] != b'\r' && v[i] != b'\n' {
contains_nonspace = true;
}

Expand Down Expand Up @@ -274,3 +274,18 @@ pub fn extract_attributes_from_tag(html_tag: &str) -> HashMap<String, String> {

attributes
}

#[cfg(test)]
pub mod tests {
use super::normalize_code;

#[test]
pub fn normalize_code_handles_lone_newline() {
assert_eq!(normalize_code(&[b'\n']), vec![b' ']);
}

#[test]
pub fn normalize_code_handles_lone_space() {
assert_eq!(normalize_code(&[b' ']), vec![b' ']);
}
}
5 changes: 5 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,11 @@ fn backticks() {
);
}

#[test]
fn backticks_empty_with_newline_should_be_space() {
html("`\n`", "<p><code> </code></p>\n");
}

#[test]
fn backticks_num() {
let input = "Some `code1`. More ``` code2 ```.\n";
Expand Down

0 comments on commit c3c6707

Please sign in to comment.