Skip to content

Commit

Permalink
(fix) no empty line at the end of markdown embed
Browse files Browse the repository at this point in the history
Prettier's markdown parser tries to strip the last hardline of the embedded docs, but fails to do so in our case because it does not recurse into concat parts -> we need to strip it ourselves.
Fixes sveltejs#202
  • Loading branch information
Simon Holthausen committed Feb 8, 2021
1 parent 031c44f commit df52baa
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# prettier-plugin-svelte changelog

## 2.1.3 (Unreleased)

* Don't print empty line at the end of the code if embedded inside markdown ([#202](https://github.com/sveltejs/prettier-plugin-svelte/issues/202))

## 2.1.2

* Keep whitespace around `<script>`/`<style>` tags ([#197](https://github.com/sveltejs/prettier-plugin-svelte/issues/197))
Expand Down
11 changes: 10 additions & 1 deletion src/print/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { extractAttributes } from '../lib/extractAttributes';
import { getText } from '../lib/getText';
import { hasSnippedContent, unsnipContent } from '../lib/snipTagContent';
import { parseSortOrder, SortOrderPart } from '../options';
import { isEmptyDoc, isLine, trim } from './doc-helpers';
import { isEmptyDoc, isLine, trim, trimRight } from './doc-helpers';
import { flatten, isASTNode, isPreTagContent } from './helpers';
import {
checkWhitespaceAtEndOfSvelteBlock,
Expand Down Expand Up @@ -673,6 +673,15 @@ function printTopLevelParts(
ignoreNext = false;
svelteOptionsDoc = undefined;

// If this is invoked as an embed of markdown, remove the last hardline.
// The markdown parser tries this, too, but fails because it does not
// recurse into concats. Doing this will prevent an empty line
// at the end of the embedded code block.
if (options.parentParser === 'markdown') {
const lastDoc = docs[docs.length - 1];
trimRight([lastDoc], isLine);
}

return groupConcat([join(hardline, docs)]);
}

Expand Down
1 change: 0 additions & 1 deletion test/printer/samples/markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ by offloading the formatting of the fenced code block to prettier-plugin-svelte.
color: green;
}
</style>
```

0 comments on commit df52baa

Please sign in to comment.