Skip to content

Commit 584b8b0

Browse files
committed
fix(formatter_markdown): keep a shape line after a multi-line inline node or link title (#27000)
Preserve lines whose meaning changes when they are merged into a single line; a few had been overlooked.
1 parent 342527d commit 584b8b0

3 files changed

Lines changed: 134 additions & 15 deletions

File tree

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

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,17 @@ pub fn collect_inlines<'a>(
450450
mark_delimiter(open, parts, false);
451451
parts.push_str(style);
452452
}
453-
Inline::Link(l) => link::collect_link(l, parts, f),
453+
Inline::Link(l) => {
454+
link::collect_link(l, parts, f);
455+
// A title keeps its newlines (a label's are collapsed): as after an opaque node
456+
if let LinkKind::Inline { title: Some(title), .. } = &l.kind
457+
&& let (Some(first), Some(last)) = (title.first(), title.last())
458+
&& let Some(risky) =
459+
tail_line_risky(children, first.span.start, last.span.end, None, f)
460+
{
461+
f.context().raw_text().set(raw_for(risky));
462+
}
463+
}
454464
// Opaque to delimiter pairing (an image's alt text too: it is printed as written)
455465
_ => {
456466
let start = parts.len();
@@ -495,29 +505,41 @@ pub fn collect_inlines<'a>(
495505
_ => parts.push_str(raw),
496506
}
497507
parts.mark(Mark::Opaque { start, end: parts.len() });
498-
// A newline inside the node starts a source line with the node's tail; what follows
499-
// on that line may be cut by wrapping (`$$\n$$ text`: `$$` alone opens math),
500-
// so such a line stays as written
501-
if parent.paragraph
502-
&& !code_span_joined
503-
&& let Some(newline) = raw.rfind('\n')
504-
{
505-
let line_start = child.span().start + u32::try_from(newline).unwrap_or(0) + 1;
506-
let line = line_from(
508+
// A newline inside the node starts a source line with the node's tail;
509+
// what follows on that line may be cut by wrapping (`$$\n$$ text`: `$$` alone opens math),
510+
// so such a line stays as written.
511+
if !code_span_joined
512+
&& let Some(risky) = tail_line_risky(
507513
children,
508-
line_start,
509-
code_span_tail.map(|tail| (tail, child.span().end)),
514+
child.span().start,
515+
child.span().end,
516+
code_span_tail,
510517
f,
511-
);
512-
let risky = is_line_shape_start(&line.text)
513-
|| line_or_prefix_opens_block(&line, true, f);
518+
)
519+
{
514520
f.context().raw_text().set(raw_for(risky));
515521
}
516522
}
517523
}
518524
}
519525
}
520526

527+
/// After a newline inside `start..end` (a node printed with its line breaks),
528+
/// whether the source line it starts is a shape or may open a block; `None` without a newline.
529+
/// `tail`: the printed form of that line up to `end`, when it differs from the source.
530+
fn tail_line_risky<'a>(
531+
children: &[Inline<'_>],
532+
start: u32,
533+
end: u32,
534+
tail: Option<&'a str>,
535+
f: &MarkdownFormatter<'_, 'a>,
536+
) -> Option<bool> {
537+
let newline = f.context().source_text().slice_range(start, end).rfind('\n')?;
538+
let line_start = start + u32::try_from(newline).unwrap_or(0) + 1;
539+
let line = line_from(children, line_start, tail.map(|tail| (tail, end)), f);
540+
Some(is_line_shape_start(&line.text) || line_or_prefix_opens_block(&line, true, f))
541+
}
542+
521543
/// Records a delimiter node whose opening marker was pushed at `open` and whose closing marker
522544
/// is about to be pushed.
523545
fn mark_delimiter(open: u32, parts: &mut Parts<'_>, strong: bool) {

‎crates/oxc_formatter_markdown/tests/fixtures/markdown/prose-wrap/line-shapes.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,15 @@ Click <Badge type="tip" text="new" /> to see it, a sentence long enough to wrap
1818
<span>plain html</span> keeps wrapping with the prose around it as usual.
1919
:::)
2020
plain text after an emoticon line that is long enough to wrap when prose wrap is always on.
21+
22+
A line starting inside a multi-line tag is a shape line, <span
23+
:::> at the top level
24+
next line
25+
26+
_A line starting inside a multi-line tag is a shape line, <span
27+
:::> inside emphasis
28+
next line_
29+
30+
A line starting inside a multi-line link title is a shape line, [text](url "title
31+
:::") too
32+
next line

‎crates/oxc_formatter_markdown/tests/fixtures/markdown/prose-wrap/line-shapes.md.snap‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ Click <Badge type="tip" text="new" /> to see it, a sentence long enough to wrap
2323
:::)
2424
plain text after an emoticon line that is long enough to wrap when prose wrap is always on.
2525

26+
A line starting inside a multi-line tag is a shape line, <span
27+
:::> at the top level
28+
next line
29+
30+
_A line starting inside a multi-line tag is a shape line, <span
31+
:::> inside emphasis
32+
next line_
33+
34+
A line starting inside a multi-line link title is a shape line, [text](url "title
35+
:::") too
36+
next line
37+
2638
==================== Output ====================
2739
---------------------------------------
2840
{ printWidth: 80, proseWrap: "always" }
@@ -52,6 +64,19 @@ around the tag.
5264
plain text after an emoticon line that is long enough to wrap when prose wrap is
5365
always on.
5466

67+
A line starting inside a multi-line tag is a shape line, <span
68+
:::> at the top level
69+
next line
70+
71+
_A line starting inside a multi-line tag is a shape line, <span
72+
:::> inside emphasis
73+
next line_
74+
75+
A line starting inside a multi-line link title is a shape line,
76+
[text](url "title
77+
:::") too
78+
next line
79+
5580
----------------------------------------
5681
{ printWidth: 100, proseWrap: "always" }
5782
----------------------------------------
@@ -76,6 +101,18 @@ Click <Badge type="tip" text="new" /> to see it, a sentence long enough to wrap
76101
:::)
77102
plain text after an emoticon line that is long enough to wrap when prose wrap is always on.
78103

104+
A line starting inside a multi-line tag is a shape line, <span
105+
:::> at the top level
106+
next line
107+
108+
_A line starting inside a multi-line tag is a shape line, <span
109+
:::> inside emphasis
110+
next line_
111+
112+
A line starting inside a multi-line link title is a shape line, [text](url "title
113+
:::") too
114+
next line
115+
79116
--------------------------------------
80117
{ printWidth: 80, proseWrap: "never" }
81118
--------------------------------------
@@ -100,6 +137,18 @@ Click <Badge type="tip" text="new" /> to see it, a sentence long enough to wrap
100137
:::)
101138
plain text after an emoticon line that is long enough to wrap when prose wrap is always on.
102139

140+
A line starting inside a multi-line tag is a shape line, <span
141+
:::> at the top level
142+
next line
143+
144+
_A line starting inside a multi-line tag is a shape line, <span
145+
:::> inside emphasis
146+
next line_
147+
148+
A line starting inside a multi-line link title is a shape line, [text](url "title
149+
:::") too
150+
next line
151+
103152
---------------------------------------
104153
{ printWidth: 100, proseWrap: "never" }
105154
---------------------------------------
@@ -124,6 +173,18 @@ Click <Badge type="tip" text="new" /> to see it, a sentence long enough to wrap
124173
:::)
125174
plain text after an emoticon line that is long enough to wrap when prose wrap is always on.
126175

176+
A line starting inside a multi-line tag is a shape line, <span
177+
:::> at the top level
178+
next line
179+
180+
_A line starting inside a multi-line tag is a shape line, <span
181+
:::> inside emphasis
182+
next line_
183+
184+
A line starting inside a multi-line link title is a shape line, [text](url "title
185+
:::") too
186+
next line
187+
127188
-----------------------------------------
128189
{ printWidth: 80, proseWrap: "preserve" }
129190
-----------------------------------------
@@ -148,6 +209,18 @@ Click <Badge type="tip" text="new" /> to see it, a sentence long enough to wrap
148209
:::)
149210
plain text after an emoticon line that is long enough to wrap when prose wrap is always on.
150211

212+
A line starting inside a multi-line tag is a shape line, <span
213+
:::> at the top level
214+
next line
215+
216+
_A line starting inside a multi-line tag is a shape line, <span
217+
:::> inside emphasis
218+
next line_
219+
220+
A line starting inside a multi-line link title is a shape line, [text](url "title
221+
:::") too
222+
next line
223+
151224
------------------------------------------
152225
{ printWidth: 100, proseWrap: "preserve" }
153226
------------------------------------------
@@ -172,4 +245,16 @@ Click <Badge type="tip" text="new" /> to see it, a sentence long enough to wrap
172245
:::)
173246
plain text after an emoticon line that is long enough to wrap when prose wrap is always on.
174247

248+
A line starting inside a multi-line tag is a shape line, <span
249+
:::> at the top level
250+
next line
251+
252+
_A line starting inside a multi-line tag is a shape line, <span
253+
:::> inside emphasis
254+
next line_
255+
256+
A line starting inside a multi-line link title is a shape line, [text](url "title
257+
:::") too
258+
next line
259+
175260
===================== End =====================

0 commit comments

Comments
 (0)