Skip to content
Merged
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
24 changes: 20 additions & 4 deletions src/utils/mrkdwn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,10 @@ fn convert_inline(s: &str) -> String {
i = close + 2;
continue;
}
out.push('_');
i += 1;
// Unmatched `__`: emit both underscores literally so the
// second one isn't re-parsed as an italic opener.
out.push_str("__");
i += 2;
} else {
// _italic_ is already mrkdwn; copy through the closing `_`.
if let Some(close) = find_byte(b, i + 1, b'_') {
Expand All @@ -267,8 +269,10 @@ fn convert_inline(s: &str) -> String {
i = close + 2;
continue;
}
out.push('~');
i += 1;
// Unmatched `~~`: emit both tildes literally so the second
// one isn't re-parsed as a strikethrough opener.
out.push_str("~~");
i += 2;
} else if let Some(close) = find_byte(b, i + 1, b'~') {
out.push_str(&s[i..=close]);
i = close + 1;
Expand Down Expand Up @@ -622,6 +626,18 @@ mod tests {
);
}

#[test]
fn unmatched_double_underscore_and_tilde_stay_literal() {
// `__foo_bar`: the unmatched `__` must not leave its second `_` to
// pair with the one in `foo_bar` and create a spurious italic span.
assert_eq!(markdown_to_mrkdwn("__foo_bar"), "__foo_bar");
// `~~a~b`: likewise for strikethrough.
assert_eq!(markdown_to_mrkdwn("~~a~b"), "~~a~b");
// Matched pairs still convert.
assert_eq!(markdown_to_mrkdwn("__bold__"), "*bold*");
assert_eq!(markdown_to_mrkdwn("~~strike~~"), "~strike~");
}

#[test]
fn unmatched_double_asterisk_does_not_italicize() {
// `**foo* bar`: the unmatched `**` stays literal; the lone `*` after
Expand Down
Loading