fix(markdown): better handling for trailing spaces (#4593)

- 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`
This commit is contained in:
Ika authored and GitHub committed 2018-06-08 00:20:25 +08:00
1 parent fb74dc54c3
commit 35a42bef24
7 files changed
+98 -19

No files matched your search

+1 -7
View File
@@ -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]*$/,
""
+14 -11
View File
@@ -8,6 +8,8 @@ const {
concat,
join,
line,
literalline,
markAsRoot,
hardline,
softline,
fill,
@@ -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": {
@@ -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:
@@ -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) {
@@ -6,6 +6,9 @@ exports[`simple.md 1`] = `
123\\
456
- 123
123
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
123
456
@@ -13,4 +16,7 @@ exports[`simple.md 1`] = `
123\\
456
- 123
123
`;
+3
View File
@@ -3,3 +3,6 @@
123\
456
- 123
123
@@ -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 -->
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -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>
@@ -902,7 +902,7 @@ exports[`example-85.md 1`] = `
exports[`example-86.md 1`] = `
foo
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
foo
foo
`;