From e74e6565b44d1ea412828d75d42f94969ec6ee06 Mon Sep 17 00:00:00 2001 From: Ika Date: Fri, 1 Jun 2018 00:33:37 +0800 Subject: [PATCH] fix(markdown): respect blank line between block htmls (#4609) Fixes #4605 The AST here is actually two `html`s and it did follow the [CommonMark spec](https://spec.commonmark.org/0.28/#html-blocks), so I ended up with respecting the blank line between block `html`s. --- src/language-markdown/printer-markdown.js | 13 ++++++++++++- .../markdown_html/__snapshots__/jsfmt.spec.js.snap | 11 +++++++++++ tests/markdown_html/blank-line-between-htmls.md | 3 +++ .../__snapshots__/jsfmt.spec.js.snap | 2 -- 4 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 tests/markdown_html/blank-line-between-htmls.md diff --git a/src/language-markdown/printer-markdown.js b/src/language-markdown/printer-markdown.js index 856bbcd8e..27043431f 100644 --- a/src/language-markdown/printer-markdown.js +++ b/src/language-markdown/printer-markdown.js @@ -705,9 +705,20 @@ function shouldPrePrintDoubleHardline(node, data) { const isPrevNodePrettierIgnore = isPrettierIgnore(data.prevNode) === "next"; + const isBlockHtmlWithoutBlankLineBetweenPrevHtml = + node.type === "html" && + data.prevNode && + data.prevNode.type === "html" && + data.prevNode.position.end.line + 1 === node.position.start.line; + return ( isPrevNodeLooseListItem || - !(isSiblingNode || isInTightListItem || isPrevNodePrettierIgnore) + !( + isSiblingNode || + isInTightListItem || + isPrevNodePrettierIgnore || + isBlockHtmlWithoutBlankLineBetweenPrevHtml + ) ); } diff --git a/tests/markdown_html/__snapshots__/jsfmt.spec.js.snap b/tests/markdown_html/__snapshots__/jsfmt.spec.js.snap index 9b031dee3..ae384f68d 100644 --- a/tests/markdown_html/__snapshots__/jsfmt.spec.js.snap +++ b/tests/markdown_html/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,16 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`blank-line-between-htmls.md 1`] = ` + +

labelify

+ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +

labelify

+ + +`; + exports[`multiline.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. diff --git a/tests/markdown_html/blank-line-between-htmls.md b/tests/markdown_html/blank-line-between-htmls.md new file mode 100644 index 000000000..5b7acd3ea --- /dev/null +++ b/tests/markdown_html/blank-line-between-htmls.md @@ -0,0 +1,3 @@ + +

labelify

+ diff --git a/tests/markdown_ignore/__snapshots__/jsfmt.spec.js.snap b/tests/markdown_ignore/__snapshots__/jsfmt.spec.js.snap index 935c8d015..0665c3c04 100644 --- a/tests/markdown_ignore/__snapshots__/jsfmt.spec.js.snap +++ b/tests/markdown_ignore/__snapshots__/jsfmt.spec.js.snap @@ -63,7 +63,6 @@ exports[`top-level-range.md 1`] = ` > -> > > > | some | table | @@ -72,7 +71,6 @@ exports[`top-level-range.md 1`] = ` > | 2 | b | > > -> > `;