Skip to content

Commit

Permalink
fix(markdown): better handling for trailing spaces (#4593)
Browse files Browse the repository at this point in the history
- preserve trailing spaces in html, excluding html comment
- `doc-printer` is now unrelated to `options.parser`
- fix some cases that two trailing spaces are mis-considered as `break`
  • Loading branch information
ikatyang committed Jun 7, 2018
1 parent fb74dc5 commit 35a42be
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 19 deletions.
8 changes: 1 addition & 7 deletions src/doc/doc-printer.js
Expand Up @@ -465,13 +465,7 @@ function printDocToString(doc, options) {
out.pop();
}

if (
out.length &&
typeof out[out.length - 1] === "string" &&
(options.parser !== "markdown" ||
// preserve markdown's `break` node (two trailing spaces)
!/\S {2}$/.test(out[out.length - 1]))
) {
if (out.length && typeof out[out.length - 1] === "string") {
out[out.length - 1] = out[out.length - 1].replace(
/[^\S\n]*$/,
""
Expand Down
25 changes: 14 additions & 11 deletions src/language-markdown/printer-markdown.js
Expand Up @@ -8,6 +8,8 @@ const {
concat,
join,
line,
literalline,
markAsRoot,
hardline,
softline,
fill,
Expand Down Expand Up @@ -217,11 +219,15 @@ function genericPrint(path, options, print) {
return node.value;
case "html": {
const parentNode = path.getParentNode();
return replaceNewlinesWithHardlines(
const value =
parentNode.type === "root" &&
privateUtil.getLast(parentNode.children) === node
? node.value.trimRight()
: node.value
: node.value;
const isHtmlComment = /^<!--[\s\S]*-->$/.test(value);
return replaceNewlinesWith(
value,
isHtmlComment ? hardline : markAsRoot(literalline)
);
}
case "list": {
Expand Down Expand Up @@ -356,14 +362,11 @@ function genericPrint(path, options, print) {
case "tableCell":
return printChildren(path, options, print);
case "break":
return concat([
/\s/.test(options.originalText[node.position.start.offset])
? " "
: "\\",
hardline
]);
return /\s/.test(options.originalText[node.position.start.offset])
? concat([" ", markAsRoot(literalline)])
: concat(["\\", hardline]);
case "liquidNode":
return replaceNewlinesWithHardlines(node.value);
return replaceNewlinesWith(node.value, hardline);
case "tableRow": // handled in "table"
case "listItem": // handled in "list"
default:
Expand Down Expand Up @@ -414,8 +417,8 @@ function getNthListSiblingIndex(node, parentNode) {
);
}

function replaceNewlinesWithHardlines(str) {
return join(hardline, str.split("\n"));
function replaceNewlinesWith(str, doc) {
return join(doc, str.split("\n"));
}

function getNthSiblingIndex(node, parentNode, condition) {
Expand Down
6 changes: 6 additions & 0 deletions tests/markdown_break/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -6,11 +6,17 @@ exports[`simple.md 1`] = `
123\\
456
- 123
123
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
123
456
123\\
456
- 123
123
`;
3 changes: 3 additions & 0 deletions tests/markdown_break/simple.md
Expand Up @@ -3,3 +3,6 @@

123\
456

- 123
123
51 changes: 51 additions & 0 deletions tests/markdown_html/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -62,6 +62,57 @@ exports[`multiline.md 1`] = `
`;
exports[`multiline-with-trailing-space.md 1`] = `
1. Some test text, the goal is to have the html table below nested within this number. When formating on save Prettier will continue to add an indent each time pushing the table further and further out of sync.
<table class="table table-striped">
<tr>
<th>Test</th>
<th>Table</th>
</tr>
<tbody>
<tr>
<td>will</td>
<td>be</td>
</tr>
<tr>
<td>pushed</td>
<td>When</td>
</tr>
<tr>
<td>Format on</td>
<td>Save</td>
</tr>
</tbody>
</table>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1. Some test text, the goal is to have the html table below nested within this
number. When formating on save Prettier will continue to add an indent each
time pushing the table further and further out of sync.
<table class="table table-striped">
<tr>
<th>Test</th>
<th>Table</th>
</tr>
<tbody>
<tr>
<td>will</td>
<td>be</td>
</tr>
<tr>
<td>pushed</td>
<td>When</td>
</tr>
<tr>
<td>Format on</td>
<td>Save</td>
</tr>
</tbody>
</table>
`;
exports[`simple.md 1`] = `
<!-- hello world -->
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
22 changes: 22 additions & 0 deletions tests/markdown_html/multiline-with-trailing-space.md
@@ -0,0 +1,22 @@
1. Some test text, the goal is to have the html table below nested within this number. When formating on save Prettier will continue to add an indent each time pushing the table further and further out of sync.

<table class="table table-striped">
<tr>
<th>Test</th>
<th>Table</th>
</tr>
<tbody>
<tr>
<td>will</td>
<td>be</td>
</tr>
<tr>
<td>pushed</td>
<td>When</td>
</tr>
<tr>
<td>Format on</td>
<td>Save</td>
</tr>
</tbody>
</table>
2 changes: 1 addition & 1 deletion tests/markdown_spec/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -902,7 +902,7 @@ exports[`example-85.md 1`] = `
exports[`example-86.md 1`] = `
foo
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
foo
foo
`;

Expand Down

0 comments on commit 35a42be

Please sign in to comment.