diff --git a/src/wrap/tokenize.rs b/src/wrap/tokenize.rs index a839ca2e..7214630e 100644 --- a/src/wrap/tokenize.rs +++ b/src/wrap/tokenize.rs @@ -123,21 +123,20 @@ fn next_token(s: &str) -> Option<(Token<'_>, usize)> { if s.is_empty() { return None; } - if let Some(pos) = s.find('`') { - if pos > 0 { + let delim_len = s.chars().take_while(|&c| c == '`').count(); + if delim_len == 0 { + if let Some(pos) = s.find('`') { return Some((Token::Text(&s[..pos]), pos)); } - let delim_len = s.chars().take_while(|&c| c == '`').count(); - if delim_len == 0 { - return Some((Token::Text(s), s.len())); - } - let closing = &s[..delim_len]; - if let Some(end) = s[delim_len..].find(closing) { - let code = &s[delim_len..delim_len + end]; - return Some((Token::Code(code), delim_len + end + delim_len)); - } + return Some((Token::Text(s), s.len())); + } + + let closing = &s[..delim_len]; + if let Some(end) = s[delim_len..].find(closing) { + let code = &s[delim_len..delim_len + end]; + return Some((Token::Code(code), delim_len + end + delim_len)); } - Some((Token::Text(s), s.len())) + Some((Token::Text(closing), delim_len)) } fn tokenize_inline<'a, F>(mut rest: &'a str, mut emit: F) diff --git a/tests/wrap/tokenize_markdown.rs b/tests/wrap/tokenize_markdown.rs index 33f67f0b..4e41a826 100644 --- a/tests/wrap/tokenize_markdown.rs +++ b/tests/wrap/tokenize_markdown.rs @@ -50,3 +50,30 @@ fn incorrect_fence_length_is_text() { ] ); } +#[test] +fn unmatched_inline_code_is_text() { + let source = "bad `code span"; + let tokens = wrap::tokenize_markdown(source); + assert_eq!( + tokens, + vec![ + Token::Text("bad "), + Token::Text("`"), + Token::Text("code span"), + ] + ); +} + +#[test] +fn multiple_unmatched_backticks_are_text() { + let source = "``bad code"; + let tokens = wrap::tokenize_markdown(source); + assert_eq!( + tokens, + vec![ + Token::Text("``"), + Token::Text("bad code"), + ] + ); +} +