Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Break link definitions onto multiple lines when needed #3531

Merged
merged 13 commits into from
May 14, 2018
39 changes: 28 additions & 11 deletions src/language-markdown/printer-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const hardline = docBuilders.hardline;
const softline = docBuilders.softline;
const fill = docBuilders.fill;
const align = docBuilders.align;
const indent = docBuilders.indent;
const group = docBuilders.group;
const printDocToString = doc.printer.printDocToString;

Expand Down Expand Up @@ -281,14 +282,23 @@ function genericPrint(path, options, print) {
node.referenceType === "collapsed" ? "[]" : ""
]);
}
case "definition":
return concat([
"[",
node.identifier,
"]: ",
printUrl(node.url),
printTitle(node.title, options)
]);
case "definition": {
const lineOrSpace = options.proseWrap === "always" ? line : " ";
return group(
concat([
concat(["[", node.identifier, "]:"]),
indent(
concat([
lineOrSpace,
printUrl(node.url),
node.title === null
? ""
: concat([lineOrSpace, printTitle(node.title, options, false)])
])
)
])
);
}
case "footnote":
return concat(["[^", printChildren(path, options, print), "]"]);
case "footnoteReference":
Expand Down Expand Up @@ -651,12 +661,19 @@ function printUrl(url, dangerousCharOrChars) {
: url;
}

function printTitle(title, options) {
function printTitle(title, options, printSpace) {
if (printSpace == null) {
printSpace = true;
}

if (!title) {
return "";
}
if (printSpace) {
return " " + printTitle(title, options, false);
}
if (title.includes('"') && title.includes("'") && !title.includes(")")) {
return ` (${title})`; // avoid escaped quotes
return `(${title})`; // avoid escaped quotes
}
// faster than using RegExps: https://jsperf.com/performance-of-match-vs-split
const singleCount = title.split("'").length - 1;
Expand All @@ -666,7 +683,7 @@ function printTitle(title, options) {
? '"'
: doubleCount > singleCount ? "'" : options.singleQuote ? "'" : '"';
title = title.replace(new RegExp(`(${quote})`, "g"), "\\$1");
return ` ${quote}${title}${quote}`;
return `${quote}${title}${quote}`;
}

function normalizeParts(parts) {
Expand Down
129 changes: 128 additions & 1 deletion tests/markdown_linkReference/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,101 @@ exports[`cjk.md 1`] = `

`;

exports[`cjk.md 2`] = `
[這是一段很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長的段落][]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[這是一段很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長的段落][]

`;

exports[`collapsed.md 1`] = `
[hello][]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[hello][]

`;

exports[`collapsed.md 2`] = `
[hello][]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[hello][]

`;

exports[`definition.md 1`] = `
[just-url]: https://example.com
[url-with-short-title]: https://example.com "title"
[url-with-long-title]: https://example.com "a long, long title. It's really really long. Here have words."
[empty-title]: https://example.com ""

[long]: https://example.com/a-long-url/another-segment/yet-another-segment/a-really-long-file-name.php.aspx
[long-with-title]: https://example.com/a-long-url/another-segment/yet-another-segment/a-really-long-file-name.php.aspx "look a title!"
[long-with-empty-title]: https://example.com/a-long-url/another-segment/yet-another-segment/a-really-long-file-name.php.aspx ""
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[just-url]: https://example.com
[url-with-short-title]: https://example.com "title"
[url-with-long-title]:
https://example.com
"a long, long title. It's really really long. Here have words."
[empty-title]: https://example.com
[long]:
https://example.com/a-long-url/another-segment/yet-another-segment/a-really-long-file-name.php.aspx
[long-with-title]:
https://example.com/a-long-url/another-segment/yet-another-segment/a-really-long-file-name.php.aspx
"look a title!"
[long-with-empty-title]:
https://example.com/a-long-url/another-segment/yet-another-segment/a-really-long-file-name.php.aspx

`;

exports[`definition.md 2`] = `
[just-url]: https://example.com
[url-with-short-title]: https://example.com "title"
[url-with-long-title]: https://example.com "a long, long title. It's really really long. Here have words."
[empty-title]: https://example.com ""

[long]: https://example.com/a-long-url/another-segment/yet-another-segment/a-really-long-file-name.php.aspx
[long-with-title]: https://example.com/a-long-url/another-segment/yet-another-segment/a-really-long-file-name.php.aspx "look a title!"
[long-with-empty-title]: https://example.com/a-long-url/another-segment/yet-another-segment/a-really-long-file-name.php.aspx ""
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[just-url]: https://example.com
[url-with-short-title]: https://example.com "title"
[url-with-long-title]: https://example.com "a long, long title. It's really really long. Here have words."
[empty-title]: https://example.com
[long]: https://example.com/a-long-url/another-segment/yet-another-segment/a-really-long-file-name.php.aspx
[long-with-title]: https://example.com/a-long-url/another-segment/yet-another-segment/a-really-long-file-name.php.aspx "look a title!"
[long-with-empty-title]: https://example.com/a-long-url/another-segment/yet-another-segment/a-really-long-file-name.php.aspx

`;

exports[`full.md 1`] = `
[hello][world]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[hello][world]

`;

exports[`full.md 2`] = `
[hello][world]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[hello][world]

`;

exports[`shortcut.md 1`] = `
[hello]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[hello]

`;

exports[`shortcut.md 2`] = `
[hello]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[hello]

`;

exports[`title.md 1`] = `
[ref]: https://example.com (bar)
[other-ref]: https://example.com (Shakespeare's "Romeo and Juliet" is a famous play)
Expand Down Expand Up @@ -56,7 +130,9 @@ exports[`title.md 1`] = `

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[ref]: https://example.com "bar"
[other-ref]: https://example.com (Shakespeare's "Romeo and Juliet" is a famous play)
[other-ref]:
https://example.com
(Shakespeare's "Romeo and Juliet" is a famous play)

[a]: https://example.com "\\"" [a]: https://example.com '\\"' [a]:
https://example.com (\\")
Expand All @@ -80,3 +156,54 @@ https://example.com (\\\\\\))
[a]: https://example.com '"'

`;

exports[`title.md 2`] = `
[ref]: https://example.com (bar)
[other-ref]: https://example.com (Shakespeare's "Romeo and Juliet" is a famous play)

[a]: https://example.com "\\""
[a]: https://example.com '\\"'
[a]: https://example.com (\\")

[a]: https://example.com "\\'"
[a]: https://example.com '\\''
[a]: https://example.com (\\')

[a]: https://example.com "\\'"
[a]: https://example.com '\\)'
[a]: https://example.com (\\))

<!-- mis-parsing, \`\\\` are missing: -->

[a]: https://example.com "\\\\\\""
[a]: https://example.com '\\\\\\''
[a] https://example.com (\\\\\\))

[a]: https://example.com "\\\\'"
[a]: https://example.com '\\\\"'
[a]: https://example.com (\\\\")

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[ref]: https://example.com "bar"
[other-ref]: https://example.com (Shakespeare's "Romeo and Juliet" is a famous play)

[a]: https://example.com "\\"" [a]: https://example.com '\\"' [a]: https://example.com (\\")

[a]: https://example.com "'"

[a]: https://example.com '\\'' [a]: https://example.com (\\')

[a]: https://example.com "'"
[a]: https://example.com ")"

[a]: https://example.com (\\))

<!-- mis-parsing, \`\\\` are missing: -->

[a]: https://example.com "\\\\\\"" [a]: https://example.com '\\\\\\'' [a] https://example.com (\\\\\\))

[a]: https://example.com "'"
[a]: https://example.com '"'
[a]: https://example.com '"'

`;
8 changes: 8 additions & 0 deletions tests/markdown_linkReference/definition.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[just-url]: https://example.com
[url-with-short-title]: https://example.com "title"
[url-with-long-title]: https://example.com "a long, long title. It's really really long. Here have words."
[empty-title]: https://example.com ""

[long]: https://example.com/a-long-url/another-segment/yet-another-segment/a-really-long-file-name.php.aspx
[long-with-title]: https://example.com/a-long-url/another-segment/yet-another-segment/a-really-long-file-name.php.aspx "look a title!"
[long-with-empty-title]: https://example.com/a-long-url/another-segment/yet-another-segment/a-really-long-file-name.php.aspx ""
1 change: 1 addition & 0 deletions tests/markdown_linkReference/jsfmt.spec.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
run_spec(__dirname, ["markdown"], { proseWrap: "always" });
run_spec(__dirname, ["markdown"], { proseWrap: "never" });