Markdown: Fix extra empty line added after empty table (#9654)

This commit is contained in:
fisker Cheung authored and GitHub committed 2020-11-13 19:13:03 +08:00
1 parent 9b206b5605
commit 4bcf8a3a36
5 files changed
+201 -18

No files matched your search

+29
View File
@@ -0,0 +1,29 @@
#### Fix extra empty line added after empty table (#9654 by @fisker)
<!-- prettier-ignore -->
```markdown
<!-- Input -->
Test line 1
| Specify the selected option : | Option 1 |
| ----------------------------- | -------- |
Test line 6
<!-- Prettier stable -->
Test line 1
| Specify the selected option : | Option 1 |
| ----------------------------- | -------- |
Test line 6
<!-- Prettier master -->
Test line 1
| Specify the selected option : | Option 1 |
| ----------------------------- | -------- |
Test line 6
```
+17 -18
View File
@@ -595,33 +595,32 @@ function printTable(path, options, print) {
),
contents[0].map(() => 3) // minimum width = 3 (---, :--, :-:, --:)
);
const alignedTable = join(hardlineWithoutBreakParent, [
printRow(contents[0]),
printSeparator(),
join(
hardlineWithoutBreakParent,
contents.slice(1).map((rowContents) => printRow(rowContents))
),
]);
const alignedTable = printTableContents(/* isCompact */ false);
if (options.proseWrap !== "never") {
return concat([breakParent, alignedTable]);
}
// Only if the --prose-wrap never is set and it exceeds the print width.
const compactTable = join(hardlineWithoutBreakParent, [
printRow(contents[0], /* isCompact */ true),
printSeparator(/* isCompact */ true),
join(
hardlineWithoutBreakParent,
contents
.slice(1)
.map((rowContents) => printRow(rowContents, /* isCompact */ true))
),
]);
const compactTable = printTableContents(/* isCompact */ true);
return concat([breakParent, group(ifBreak(compactTable, alignedTable))]);
function printTableContents(isCompact) {
const parts = [printRow(contents[0], isCompact), printSeparator(isCompact)];
if (contents.length > 1) {
parts.push(
join(
hardlineWithoutBreakParent,
contents
.slice(1)
.map((rowContents) => printRow(rowContents, isCompact))
)
);
}
return join(hardlineWithoutBreakParent, parts);
}
function printSeparator(isCompact) {
return concat([
"| ",
@@ -0,0 +1,140 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`empty-table.md - {"proseWrap":"always"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
printWidth: 80
proseWrap: "always"
| printWidth
=====================================input======================================
Text
| Specify the selected option : | Option 1 |
|:--| --- |
Text
| Should print as compact table when --proseWrap=never|a long long long long long long long long long long long long long head|
|---|--:|
Text
=====================================output=====================================
Text
| Specify the selected option : | Option 1 |
| :---------------------------- | -------- |
Text
| Should print as compact table when --proseWrap=never | a long long long long long long long long long long long long long head |
| ---------------------------------------------------- | ----------------------------------------------------------------------: |
Text
================================================================================
`;
exports[`empty-table.md - {"proseWrap":"never"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
printWidth: 80
proseWrap: "never"
| printWidth
=====================================input======================================
Text
| Specify the selected option : | Option 1 |
|:--| --- |
Text
| Should print as compact table when --proseWrap=never|a long long long long long long long long long long long long long head|
|---|--:|
Text
=====================================output=====================================
Text
| Specify the selected option : | Option 1 |
| :---------------------------- | -------- |
Text
| Should print as compact table when --proseWrap=never | a long long long long long long long long long long long long long head |
| --- | --: |
Text
================================================================================
`;
exports[`empty-table.md - {"proseWrap":"preserve"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
printWidth: 80
proseWrap: "preserve"
| printWidth
=====================================input======================================
Text
| Specify the selected option : | Option 1 |
|:--| --- |
Text
| Should print as compact table when --proseWrap=never|a long long long long long long long long long long long long long head|
|---|--:|
Text
=====================================output=====================================
Text
| Specify the selected option : | Option 1 |
| :---------------------------- | -------- |
Text
| Should print as compact table when --proseWrap=never | a long long long long long long long long long long long long long head |
| ---------------------------------------------------- | ----------------------------------------------------------------------: |
Text
================================================================================
`;
exports[`empty-table.md format 1`] = `
====================================options=====================================
parsers: ["markdown"]
printWidth: 80
| printWidth
=====================================input======================================
Text
| Specify the selected option : | Option 1 |
|:--| --- |
Text
| Should print as compact table when --proseWrap=never|a long long long long long long long long long long long long long head|
|---|--:|
Text
=====================================output=====================================
Text
| Specify the selected option : | Option 1 |
| :---------------------------- | -------- |
Text
| Should print as compact table when --proseWrap=never | a long long long long long long long long long long long long long head |
| ---------------------------------------------------- | ----------------------------------------------------------------------: |
Text
================================================================================
`;
@@ -0,0 +1,11 @@
Text
| Specify the selected option : | Option 1 |
|:--| --- |
Text
| Should print as compact table when --proseWrap=never|a long long long long long long long long long long long long long head|
|---|--:|
Text
@@ -0,0 +1,4 @@
run_spec(__dirname, ["markdown"]);
run_spec(__dirname, ["markdown"], { proseWrap: "never" });
run_spec(__dirname, ["markdown"], { proseWrap: "always" });
run_spec(__dirname, ["markdown"], { proseWrap: "preserve" });