diff --git a/changelog_unreleased/markdown/19487.md b/changelog_unreleased/markdown/19487.md new file mode 100644 index 000000000000..53fdb055d537 --- /dev/null +++ b/changelog_unreleased/markdown/19487.md @@ -0,0 +1,15 @@ +#### Avoid corrupting empty link with title (#19487 by @andersk) + +Do not remove `<>` from an inline link or image with an empty URL and a title, as this removal would change its interpretation. + + +```md + +[link](<> "title") + + +[link]( "title") + + +[link](<> "title") +``` diff --git a/src/language-markdown/print/mdast.js b/src/language-markdown/print/mdast.js index e70751cc8a18..ca3a5991bfd3 100644 --- a/src/language-markdown/print/mdast.js +++ b/src/language-markdown/print/mdast.js @@ -224,7 +224,9 @@ function printMdast(path, options, print) { "[", printChildren(path, options, print), "](", - printUrl(node.url, ")"), + options.parser !== "mdx" && node.url === "" + ? "<>" + : printUrl(node.url, ")"), printTitle(node.title, options), ")", ]; @@ -239,7 +241,9 @@ function printMdast(path, options, print) { "![", printImageAlt(node, options), "](", - printUrl(node.url, ")"), + options.parser !== "mdx" && node.url === "" + ? "<>" + : printUrl(node.url, ")"), printTitle(node.title, options), ")", ]; diff --git a/tests/format/markdown/blockquote/__snapshots__/format.test.js.snap b/tests/format/markdown/blockquote/__snapshots__/format.test.js.snap index 14cf13f5544d..baf0416ffadd 100644 --- a/tests/format/markdown/blockquote/__snapshots__/format.test.js.snap +++ b/tests/format/markdown/blockquote/__snapshots__/format.test.js.snap @@ -879,8 +879,9 @@ proseWrap: "always" > 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\` -> 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\` +> 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\` ================================================================================ `; @@ -926,7 +927,7 @@ proseWrap: "never" > 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\` -> 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\` +> 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\` ================================================================================ `; @@ -977,7 +978,7 @@ proseWrap: "preserve" > proseWrap to \`never\` > This is a quote with a link [across multuple lines -> which should just work](). So make sure there is no > if we set +> which should just work](<>). So make sure there is no > if we set > proseWrap to \`never\` ================================================================================ diff --git a/tests/format/markdown/commonmark-test-suite/__snapshots__/format.test.js.snap b/tests/format/markdown/commonmark-test-suite/__snapshots__/format.test.js.snap index 9a3664f8dec5..3e2bf98d086a 100644 --- a/tests/format/markdown/commonmark-test-suite/__snapshots__/format.test.js.snap +++ b/tests/format/markdown/commonmark-test-suite/__snapshots__/format.test.js.snap @@ -8195,7 +8195,7 @@ proseWrap: "always" [link]() =====================================output===================================== -[link]() +[link](<>) ================================================================================ `; @@ -8209,7 +8209,7 @@ proseWrap: "always" [link](<>) =====================================output===================================== -[link]() +[link](<>) ================================================================================ `; @@ -8223,7 +8223,7 @@ proseWrap: "always" []() =====================================output===================================== -[]() +[](<>) ================================================================================ `; @@ -9531,7 +9531,7 @@ proseWrap: "always" [foo]: /url1 =====================================output===================================== -[foo]() +[foo](<>) [foo]: /url1 diff --git a/tests/format/markdown/link/__snapshots__/format.test.js.snap b/tests/format/markdown/link/__snapshots__/format.test.js.snap index 891edd2189de..f896dc4143af 100644 --- a/tests/format/markdown/link/__snapshots__/format.test.js.snap +++ b/tests/format/markdown/link/__snapshots__/format.test.js.snap @@ -22,6 +22,55 @@ proseWrap: "always" ================================================================================ `; +exports[`empty-url.md - {"proseWrap":"always"} format 1`] = ` +====================================options===================================== +parsers: ["markdown"] +proseWrap: "always" + printWidth: 80 (default) | +=====================================input====================================== +[link](<> "title") + +[link](<>) + +[link]() + +--- + +![link](<> "title") + +![link](<>) + +![link]() + +--- + +[link]: <> "title" + +[link]: <> + +=====================================output===================================== +[link](<> "title") + +[link](<>) + +[link](<>) + +--- + +![link](<> "title") + +![link](<>) + +![link](<>) + +--- + +[link]: <> "title" +[link]: <> + +================================================================================ +`; + exports[`encodedLink.md - {"proseWrap":"always"} format 1`] = ` ====================================options===================================== parsers: ["markdown"] @@ -33,28 +82,35 @@ proseWrap: "always" [link](https://www.google.fr/foo-%3Ebar) [link](https://www.google.fr/foo-bar) ![link](https://www.google.fr/foo->bar) ![link](https://www.google.fr/foo-%3Ebar) ![link](https://www.google.fr/foo-bar [link]: https://www.google.fr/foo->bar [link]: https://www.google.fr/foo-%3Ebar [link]: https://www.google.fr/foo-) [link](https://www.google.fr/foo->bar) [link](https://www.google.fr/foo-%3Ebar) [link](https://www.google.fr/foo-) ![link](https://www.google.fr/foo->bar) ![link](https://www.google.fr/foo-%3Ebar) ![link](https://www.google.fr/foo-bar [link]: https://www.google.fr/foo->bar [link]: -https://www.google.fr/foo-%3Ebar [link]: https://www.google.fr/foo-bar +[link]: https://www.google.fr/foo->bar +[link]: https://www.google.fr/foo-%3Ebar +[link]: https://www.google.fr/foo- "title") + +[link](<>) + +[link]() + +--- + +![link](<> "title") + +![link](<>) + +![link]() + +--- + +[link]: <> "title" + +[link]: <> diff --git a/tests/format/markdown/link/encodedLink.md b/tests/format/markdown/link/encodedLink.md index 151023cad541..73f6aaec6a5c 100644 --- a/tests/format/markdown/link/encodedLink.md +++ b/tests/format/markdown/link/encodedLink.md @@ -3,13 +3,15 @@ [link](https://www.google.fr/foo-%3Ebar) [link](https://www.google.fr/foo-bar) ![link](https://www.google.fr/foo->bar) ![link](https://www.google.fr/foo-%3Ebar) ![link](https://www.google.fr/foo-bar [link]: https://www.google.fr/foo->bar [link]: https://www.google.fr/foo-%3Ebar [link]: https://www.google.fr/foo-