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`
master
Ika 2018-06-08 00:20:25 +08:00 committed by GitHub
parent fb74dc54c3
commit 35a42bef24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 98 additions and 19 deletions

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]*$/,
""

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) {

View File

@ -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
`;

View File

@ -3,3 +3,6 @@
123\
456
- 123
123

View File

@ -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 -->
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -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>

View File

@ -902,7 +902,7 @@ exports[`example-85.md 1`] = `
exports[`example-86.md 1`] = `
foo
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
foo
foo
`;