Skip to content

Commit 4e77d59

Browse files
committed
fix(formatter_markdown): keep a math span after a kept line break from opening a block (#27001)
Math `$$` should not move to opening position.
1 parent 584b8b0 commit 4e77d59

4 files changed

Lines changed: 51 additions & 1 deletion

File tree

‎crates/oxc_formatter_markdown/src/print/inline.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,10 @@ pub fn collect_inlines<'a>(
287287
// After a kept line break, a line that would open a block at column 0 stays paragraph text
288288
// behind four spaces (indented code cannot interrupt a paragraph)
289289
if after_kept_break
290-
&& matches!(child, Inline::Text(_) | Inline::HtmlInline(_) | Inline::Liquid(_))
290+
&& matches!(
291+
child,
292+
Inline::Text(_) | Inline::HtmlInline(_) | Inline::Liquid(_) | Inline::MathSpan(_)
293+
)
291294
&& printed_line_opens_block(children, i, &break_kept, f)
292295
{
293296
parts.push_str(" ");

‎crates/oxc_formatter_markdown/src/print/line_shape.rs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,21 @@ pub fn printed_line_opens_block<'a>(
217217
_ => None,
218218
})
219219
.unwrap_or_else(|| children[children.len() - 1].span().end);
220+
// A verbatim node keeps its line breaks: the line ends at its first one
221+
let end = children[i..]
222+
.iter()
223+
.filter(|c| {
224+
matches!(
225+
c,
226+
Inline::HtmlInline(_) | Inline::Liquid(_) | Inline::MathSpan(_) | Inline::Image(_)
227+
)
228+
})
229+
.take_while(|c| c.span().start < end)
230+
.find_map(|c| {
231+
let newline = f.context().slice(c.span()).find('\n')?;
232+
Some(c.span().start + u32::try_from(newline).unwrap_or(0))
233+
})
234+
.map_or(end, |newline| newline.min(end));
220235
// A leading code span prints joined too (`print_code_span`);
221236
// the rest is the source, lines joined.
222237
let (head, rest_start) = match &children[i] {

‎crates/oxc_formatter_markdown/tests/fixtures/markdown/prose-wrap/wrapped-block-starts.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ padding words to push the risky token over the edge padding words to push the >
3030

3131
text then
3232
- item
33+
34+
A math span starting a line after a hard break stays behind four spaces
35+
$$\
36+
b $$ c

‎crates/oxc_formatter_markdown/tests/fixtures/markdown/prose-wrap/wrapped-block-starts.md.snap‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ padding words to push the risky token over the edge padding words to push the >
3535
text then
3636
- item
3737
38+
A math span starting a line after a hard break stays behind four spaces
39+
$$\
40+
b $$ c
41+
3842
==================== Output ====================
3943
---------------------------------------
4044
{ printWidth: 80, proseWrap: "always" }
@@ -81,6 +85,10 @@ not a quote tail words.
8185

8286
text then - item
8387

88+
A math span starting a line after a hard break stays behind four spaces
89+
$$\
90+
b $$ c
91+
8492
----------------------------------------
8593
{ printWidth: 100, proseWrap: "always" }
8694
----------------------------------------
@@ -117,6 +125,10 @@ words.
117125
118126
text then - item
119127
128+
A math span starting a line after a hard break stays behind four spaces
129+
$$\
130+
b $$ c
131+
120132
--------------------------------------
121133
{ printWidth: 80, proseWrap: "never" }
122134
--------------------------------------
@@ -151,6 +163,10 @@ padding words to push the risky token over the edge padding words to push the >
151163

152164
text then - item
153165

166+
A math span starting a line after a hard break stays behind four spaces
167+
$$\
168+
b $$ c
169+
154170
---------------------------------------
155171
{ printWidth: 100, proseWrap: "never" }
156172
---------------------------------------
@@ -185,6 +201,10 @@ padding words to push the risky token over the edge padding words to push the >
185201
186202
text then - item
187203
204+
A math span starting a line after a hard break stays behind four spaces
205+
$$\
206+
b $$ c
207+
188208
-----------------------------------------
189209
{ printWidth: 80, proseWrap: "preserve" }
190210
-----------------------------------------
@@ -219,6 +239,10 @@ padding words to push the risky token over the edge padding words to push the >
219239

220240
text then - item
221241

242+
A math span starting a line after a hard break stays behind four spaces
243+
$$\
244+
b $$ c
245+
222246
------------------------------------------
223247
{ printWidth: 100, proseWrap: "preserve" }
224248
------------------------------------------
@@ -253,4 +277,8 @@ padding words to push the risky token over the edge padding words to push the >
253277
254278
text then - item
255279
280+
A math span starting a line after a hard break stays behind four spaces
281+
$$\
282+
b $$ c
283+
256284
===================== End =====================

0 commit comments

Comments
 (0)