Skip to content

Commit cd54ccc

Browse files
anderskfisker
andauthored
Avoid corrupting empty Markdown link with title (#19487)
Co-authored-by: fisker <[email protected]> Signed-off-by: Anders Kaseorg <[email protected]>
1 parent 2bb67ce commit cd54ccc

7 files changed

Lines changed: 112 additions & 15 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#### Avoid corrupting empty link with title (#19487 by @andersk)
2+
3+
Do not remove `<>` from an inline link or image with an empty URL and a title, as this removal would change its interpretation.
4+
5+
<!-- prettier-ignore -->
6+
```md
7+
<!-- Input -->
8+
[link](<> "title")
9+
10+
<!-- Prettier stable -->
11+
[link]( "title")
12+
13+
<!-- Prettier main -->
14+
[link](<> "title")
15+
```

‎src/language-markdown/print/mdast.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,9 @@ function printMdast(path, options, print) {
224224
"[",
225225
printChildren(path, options, print),
226226
"](",
227-
printUrl(node.url, ")"),
227+
options.parser !== "mdx" && node.url === ""
228+
? "<>"
229+
: printUrl(node.url, ")"),
228230
printTitle(node.title, options),
229231
")",
230232
];
@@ -239,7 +241,9 @@ function printMdast(path, options, print) {
239241
"![",
240242
printImageAlt(node, options),
241243
"](",
242-
printUrl(node.url, ")"),
244+
options.parser !== "mdx" && node.url === ""
245+
? "<>"
246+
: printUrl(node.url, ")"),
243247
printTitle(node.title, options),
244248
")",
245249
];

‎tests/format/markdown/blockquote/__snapshots__/format.test.js.snap‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -879,8 +879,9 @@ proseWrap: "always"
879879
> This is a quote with an italic _across multuple lines which should just work_.
880880
> So make sure there is no > if we set proseWrap to \`never\`
881881
882-
> This is a quote with a link [across multuple lines which should just work]().
883-
> So make sure there is no > if we set proseWrap to \`never\`
882+
> This is a quote with a link
883+
> [across multuple lines which should just work](<>). So make sure there is no >
884+
> if we set proseWrap to \`never\`
884885
885886
================================================================================
886887
`;
@@ -926,7 +927,7 @@ proseWrap: "never"
926927
927928
> This is a quote with an italic _across multuple lines which should just work_. So make sure there is no > if we set proseWrap to \`never\`
928929
929-
> This is a quote with a link [across multuple lines which should just work](). So make sure there is no > if we set proseWrap to \`never\`
930+
> This is a quote with a link [across multuple lines which should just work](<>). So make sure there is no > if we set proseWrap to \`never\`
930931
931932
================================================================================
932933
`;
@@ -977,7 +978,7 @@ proseWrap: "preserve"
977978
> proseWrap to \`never\`
978979
979980
> This is a quote with a link [across multuple lines
980-
> which should just work](). So make sure there is no > if we set
981+
> which should just work](<>). So make sure there is no > if we set
981982
> proseWrap to \`never\`
982983
983984
================================================================================

‎tests/format/markdown/commonmark-test-suite/__snapshots__/format.test.js.snap‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8195,7 +8195,7 @@ proseWrap: "always"
81958195
[link]()
81968196
81978197
=====================================output=====================================
8198-
[link]()
8198+
[link](<>)
81998199
82008200
================================================================================
82018201
`;
@@ -8209,7 +8209,7 @@ proseWrap: "always"
82098209
[link](<>)
82108210
82118211
=====================================output=====================================
8212-
[link]()
8212+
[link](<>)
82138213
82148214
================================================================================
82158215
`;
@@ -8223,7 +8223,7 @@ proseWrap: "always"
82238223
[]()
82248224
82258225
=====================================output=====================================
8226-
[]()
8226+
[](<>)
82278227
82288228
================================================================================
82298229
`;
@@ -9531,7 +9531,7 @@ proseWrap: "always"
95319531
[foo]: /url1
95329532
95339533
=====================================output=====================================
9534-
[foo]()
9534+
[foo](<>)
95359535
95369536
[foo]: /url1
95379537

‎tests/format/markdown/link/__snapshots__/format.test.js.snap‎

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,55 @@ proseWrap: "always"
2222
================================================================================
2323
`;
2424

25+
exports[`empty-url.md - {"proseWrap":"always"} format 1`] = `
26+
====================================options=====================================
27+
parsers: ["markdown"]
28+
proseWrap: "always"
29+
printWidth: 80 (default) |
30+
=====================================input======================================
31+
[link](<> "title")
32+
33+
[link](<>)
34+
35+
[link]()
36+
37+
---
38+
39+
![link](<> "title")
40+
41+
![link](<>)
42+
43+
![link]()
44+
45+
---
46+
47+
[link]: <> "title"
48+
49+
[link]: <>
50+
51+
=====================================output=====================================
52+
[link](<> "title")
53+
54+
[link](<>)
55+
56+
[link](<>)
57+
58+
---
59+
60+
![link](<> "title")
61+
62+
![link](<>)
63+
64+
![link](<>)
65+
66+
---
67+
68+
[link]: <> "title"
69+
[link]: <>
70+
71+
================================================================================
72+
`;
73+
2574
exports[`encodedLink.md - {"proseWrap":"always"} format 1`] = `
2675
====================================options=====================================
2776
parsers: ["markdown"]
@@ -33,28 +82,35 @@ proseWrap: "always"
3382
[link](https://www.google.fr/foo-%3Ebar)
3483
[link](https://www.google.fr/foo-<bar)
3584
[link](https://www.google.fr/foo-%3Cbar)
85+
3686
![link](https://www.google.fr/()foo->bar)
3787
![link](https://www.google.fr/foo->bar)
3888
![link](https://www.google.fr/foo-%3Ebar)
3989
![link](https://www.google.fr/foo-<bar)
4090
![link](https://www.google.fr/foo-%3Cbar)
91+
4192
[link]: https://www.google.fr/()foo->bar
4293
[link]: https://www.google.fr/foo->bar
4394
[link]: https://www.google.fr/foo-%3Ebar
4495
[link]: https://www.google.fr/foo-<bar
4596
[link]: https://www.google.fr/foo-%3Cbar
97+
4698
=====================================output=====================================
4799
[link](<https://www.google.fr/()foo-%3Ebar>)
48100
[link](https://www.google.fr/foo->bar) [link](https://www.google.fr/foo-%3Ebar)
49101
[link](https://www.google.fr/foo-<bar) [link](https://www.google.fr/foo-%3Cbar)
102+
50103
![link](<https://www.google.fr/()foo-%3Ebar>)
51104
![link](https://www.google.fr/foo->bar)
52105
![link](https://www.google.fr/foo-%3Ebar)
53106
![link](https://www.google.fr/foo-<bar)
54-
![link](https://www.google.fr/foo-%3Cbar) [link]:
55-
https://www.google.fr/()foo->bar [link]: https://www.google.fr/foo->bar [link]:
56-
https://www.google.fr/foo-%3Ebar [link]: https://www.google.fr/foo-<bar [link]:
57-
https://www.google.fr/foo-%3Cbar
107+
![link](https://www.google.fr/foo-%3Cbar)
108+
109+
[link]: https://www.google.fr/()foo->bar
110+
[link]: https://www.google.fr/foo->bar
111+
[link]: https://www.google.fr/foo-%3Ebar
112+
[link]: https://www.google.fr/foo-<bar
113+
[link]: https://www.google.fr/foo-%3Cbar
58114
59115
================================================================================
60116
`;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[link](<> "title")
2+
3+
[link](<>)
4+
5+
[link]()
6+
7+
---
8+
9+
![link](<> "title")
10+
11+
![link](<>)
12+
13+
![link]()
14+
15+
---
16+
17+
[link]: <> "title"
18+
19+
[link]: <>

‎tests/format/markdown/link/encodedLink.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
[link](https://www.google.fr/foo-%3Ebar)
44
[link](https://www.google.fr/foo-<bar)
55
[link](https://www.google.fr/foo-%3Cbar)
6+
67
![link](https://www.google.fr/()foo->bar)
78
![link](https://www.google.fr/foo->bar)
89
![link](https://www.google.fr/foo-%3Ebar)
910
![link](https://www.google.fr/foo-<bar)
1011
![link](https://www.google.fr/foo-%3Cbar)
12+
1113
[link]: https://www.google.fr/()foo->bar
1214
[link]: https://www.google.fr/foo->bar
1315
[link]: https://www.google.fr/foo-%3Ebar
1416
[link]: https://www.google.fr/foo-<bar
15-
[link]: https://www.google.fr/foo-%3Cbar
17+
[link]: https://www.google.fr/foo-%3Cbar

0 commit comments

Comments
 (0)