From c1623d88e6a693d7fa4de2f511ef34f342bef2aa Mon Sep 17 00:00:00 2001 From: Ika Date: Sun, 3 Dec 2017 12:02:13 +0800 Subject: [PATCH] feat(markdown): preserve `break` style (#3325) --- src/doc-printer.js | 7 ++++++- src/printer-markdown.js | 7 ++++++- .../markdown_break/__snapshots__/jsfmt.spec.js.snap | 6 ++++++ tests/markdown_break/simple.md | 3 +++ tests/markdown_spec/__snapshots__/jsfmt.spec.js.snap | 12 ++++++------ 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/doc-printer.js b/src/doc-printer.js index 2bbf63d1..5b2b8e4e 100644 --- a/src/doc-printer.js +++ b/src/doc-printer.js @@ -398,7 +398,12 @@ function printDocToString(doc, options) { out.pop(); } - if (out.length) { + if ( + out.length && + (options.parser !== "markdown" || + // preserve markdown's `break` node (two trailing spaces) + !/\S {2}$/.test(out[out.length - 1])) + ) { out[out.length - 1] = out[out.length - 1].replace( /[^\S\n]*$/, "" diff --git a/src/printer-markdown.js b/src/printer-markdown.js index eeca4d7b..13b211fe 100644 --- a/src/printer-markdown.js +++ b/src/printer-markdown.js @@ -315,7 +315,12 @@ function genericPrint(path, options, print) { case "tableCell": return printChildren(path, options, print); case "break": - return concat(["\\", hardline]); + return concat([ + /\s/.test(options.originalText[node.position.start.offset]) + ? " " + : "\\", + hardline + ]); case "tableRow": // handled in "table" default: throw new Error(`Unknown markdown type ${JSON.stringify(node.type)}`); diff --git a/tests/markdown_break/__snapshots__/jsfmt.spec.js.snap b/tests/markdown_break/__snapshots__/jsfmt.spec.js.snap index 2318b036..d1492cb9 100644 --- a/tests/markdown_break/__snapshots__/jsfmt.spec.js.snap +++ b/tests/markdown_break/__snapshots__/jsfmt.spec.js.snap @@ -3,7 +3,13 @@ exports[`simple.md 1`] = ` 123 456 + +123\\ +456 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +123 +456 + 123\\ 456 diff --git a/tests/markdown_break/simple.md b/tests/markdown_break/simple.md index 16bd8d22..e56f8d7c 100644 --- a/tests/markdown_break/simple.md +++ b/tests/markdown_break/simple.md @@ -1,2 +1,5 @@ 123 456 + +123\ +456 diff --git a/tests/markdown_spec/__snapshots__/jsfmt.spec.js.snap b/tests/markdown_spec/__snapshots__/jsfmt.spec.js.snap index 01d94deb..a81a1cab 100644 --- a/tests/markdown_spec/__snapshots__/jsfmt.spec.js.snap +++ b/tests/markdown_spec/__snapshots__/jsfmt.spec.js.snap @@ -903,7 +903,7 @@ exports[`example-85.md 1`] = ` exports[`example-86.md 1`] = ` foo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - foo + foo `; @@ -2120,7 +2120,7 @@ exports[`example-186.md 1`] = ` aaa bbb ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -aaa\\ +aaa bbb `; @@ -5772,7 +5772,7 @@ exports[`example-597.md 1`] = ` foo baz ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -foo\\ +foo baz `; @@ -5790,7 +5790,7 @@ exports[`example-599.md 1`] = ` foo baz ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -foo\\ +foo baz `; @@ -5799,7 +5799,7 @@ exports[`example-600.md 1`] = ` foo bar ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -foo\\ +foo bar `; @@ -5817,7 +5817,7 @@ exports[`example-602.md 1`] = ` *foo bar* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -_foo\\ +_foo bar_ `;