Skip to content

Commit

Permalink
Break link definitions onto multiple lines when needed (#3531)
Browse files Browse the repository at this point in the history
* Break link definitions onto multiple lines when needed

* Simplify the conditional for breaking the title

* Don’t print the title if it’s blank

* Revert "Don’t print the title if it’s blank"

This reverts commit 2a8d0dd.

* Add another test

* Fix title printing

* Second time’s the charm

* Third time’s the charm?

* Fix snapshot

* Prettify

* Don’t break unless `proseWrap` is `always`

* Test `proseWrap: never` on link references
  • Loading branch information
j-f1 committed May 14, 2018
1 parent 1062f2a commit dc68a3d
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 12 deletions.
39 changes: 28 additions & 11 deletions src/language-markdown/printer-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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 @@ -308,14 +309,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 @@ -755,12 +765,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 @@ -774,7 +791,7 @@ function printTitle(title, options) {
? "'"
: '"';
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" });

0 comments on commit dc68a3d

Please sign in to comment.