Skip to content

Commit c1d1073

Browse files
committed
fix(MD013): keep emphasized step labels within their sentence
1 parent d736ced commit c1d1073

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

‎src/utils/text_reflow.rs‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,22 @@ fn sentence_boundary(
991991
let c = chars[pos];
992992
let next_char = chars[pos + 1];
993993

994+
// A number immediately inside an opening emphasis delimiter is a step
995+
// label, not a sentence: **1. Verify** must never split after the 1.
996+
if c == '.' && next_char == ' ' {
997+
let mut number_start = pos;
998+
while number_start > 0 && chars[number_start - 1].is_ascii_digit() {
999+
number_start -= 1;
1000+
}
1001+
if number_start < pos
1002+
&& number_start > 0
1003+
&& st.in_span_delimiter(number_start - 1)
1004+
&& st.span_closer_end(number_start - 1).is_none()
1005+
{
1006+
return None;
1007+
}
1008+
}
1009+
9941010
// Check for CJK sentence-ending punctuation (。, !, ?)
9951011
// CJK punctuation doesn't require space or uppercase after it
9961012
if is_cjk_sentence_ending(c) {
@@ -1462,7 +1478,13 @@ fn sentence_structure(text: &str, defined_references: Option<&HashSet<String>>)
14621478
// neither and skips the parse entirely.
14631479
let holds_construct = text.contains(['`', '[', '<', '$']);
14641480
let holds_cjk_emphasis = text.contains(['*', '_', '~']) && text.contains(['。', '!', '?']);
1465-
if !holds_construct && !holds_cjk_emphasis {
1481+
// ASCII step labels also need the parse to distinguish an opening span
1482+
// delimiter from literal marker text before their leading number.
1483+
let holds_emphasized_number = text
1484+
.as_bytes()
1485+
.windows(2)
1486+
.any(|pair| matches!(pair[0], b'*' | b'_' | b'~') && pair[1].is_ascii_digit());
1487+
if !holds_construct && !holds_cjk_emphasis && !holds_emphasized_number {
14661488
return NestedStructure {
14671489
atomic: Vec::new(),
14681490
markers: Vec::new(),

‎tests/rules/md013_reflow_regressions_test.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,17 @@ fn parenthesis_list_items_stay_one_per_line() {
13801380
}
13811381
}
13821382

1383+
#[test]
1384+
fn emphasized_step_labels_do_not_end_sentences() {
1385+
for marker in ["*", "**", "__", "***", "~~"] {
1386+
let input =
1387+
format!("{marker}1. Verify the user name first.{marker} Then sign in.\n\n{marker}2. Sign in{marker}\n");
1388+
let expected =
1389+
format!("{marker}1. Verify the user name first.{marker}\nThen sign in.\n\n{marker}2. Sign in{marker}\n");
1390+
assert_reflows_to(&input, 0, &[Mode::SentencePerLine], &expected);
1391+
}
1392+
}
1393+
13831394
/// Reflow rewrites only the item's paragraph, so a later line of the quote
13841395
/// keeps its indentation. Narrowing the marker's padding would move the
13851396
/// item's content column onto that line and nest it in the item.

0 commit comments

Comments
 (0)
⌂ 换网址