Repository: prettier/prettier (JavaScript)

Issue:
Some empty lines is being removed in markdown YAML frontmatter
**Prettier 3.3.0**
[Playground link](https://prettier.io/playground/#N4Igxg9gdgLgprEAuEBadAdWBLGAbOJAAgCU4BDPIgBQCcIArOMGAZy20imNYAc8IMMAAssEWgBM4tYgEYsYcvADm4gJ5IsRIqiIAROAFsIWLBIhhWmqNt1RyhwkQDqlbFDhbtRKazDEAQSIAZWxDfjgAGhDyADM4IgBhCENHWBC1VnhDADovbQFVYmEYGF4rAHoKgHc3DxyGVhzxZQrCiBzeKGV8ogBXWjxi0vKkKtq8dzgGppaKhAre2jheCGGyyorlXGE+gCMcyEMaurhGk8mPXt5lgDdsOGriCvJWVjg2CrDyZTgLqYavB6UC8dgcTgAXgBrYJqKBgIgAUVo5F6vn8RCCMFofSyBDeRBuggsECoMGESn671YRDAtDUvBgEGUKN4wk4RFubgkuDUhPoEFiNKZ-Ig9ykRD8lHIewIRHIUAkRAE1VQkCyRGxCtY5BY2GgNOgSPJ0jgfUMvXa61GVXMlhy0NYcLAOX1FUdzoA+u1PbLdVCcqxbsDvP1BtbNnamh74a6IIsbN5lqsI2Mtjt9ocUhVDEp4LRUHgZax3VCnfDUNU4HtUNJyKgo9c7g8nkQXm8PiXvr9S+WXQwgaYoKtaDBYqT9VZQUR7I5iIlhAhlCEKd1egMhkQShs0yIl6hWKvlDlHE24PdHqmqnvugejye-iOxxOOuR7rElis1luRpttuTMyOCoAFlsCyaRUAACQgXg-hvZQ7wVBCn3HSYTBBRMwTnJJF26FckPXcMfx3a9cIQw8kIfM8L1bbcbQqeDEO6B8KhQl8cjfbAP0TbRk2-Oi-wzA4gNA8CC2g2CGLIpjkPEZ80KHWVmSnTCZ3BYhgNoHIJLgAByGkACFCjROA-GIZxF2WTVLISMDlWwZQSmiE0rLs4QYM8HjlWZfjfzTQwwPzVB3NgrNjnaQNg0IzcBP8wKIJC6Yjk-FNiPo-9dmE7NRKCnSQPi8SPJyDLM31aiW2eV53k+bs-kMWhEsBYFp1nJxgIgAB2fSiCM5kTLMkIUg+dk8OqaApFoWI+ioOyZQgPoYE1CAt3IXhYKgPIvKtNLNlibACBPTrDjACosiUTg2gAD2ET1tliK6bo62Riq46KrwqJTj2MDrjvmKAE1DPj3pKrLjm+sATt65Rgnm2gwGmf9ysvHa0z2g7vt+nlWChD7Ck9G5zxbT0EE6NcMNsNTsIAcT6CBuuoH5PNDdFiDoRhmEWu1zQQGBzqNBUlSyPoJD5KBBFMy0fOB2nLs4MiciZXg2h80mQ28DdpYgWX4IVmC-pS3ySPTADQYqGmIAADWwBcly2GW5aXF6TbjJHW3baqu1zHtlFpg9UF4Rnaw2rpgXQVAhxASIQBgmB9SgVhkFAchaHoaoGeWePkBAShakySOQD2FEwChD5gnBAAZKZkFiSh3ijwv-VLgOwHcZRkGxPoohAd4AvbnEu7gS7YNoMIecoAAVaR7BH0zq9rrvWFbggAEVafgOe8DrkBGku0JuhXte4A3reAEdD7Z8os9eVAPDgKQJHzrV9tb5JUnIK+8DwfPF-3uAAlKEeewFpwGoNISuHhj5dxKIYPAFlcCmWbnAYICBF6x3uDANQWdLAJyjrcTuABJRUPNgh0mwIyAIipggYIIJAqORJ3jOFZFnAm7xaC3CPlHdwrCYB0B+LmWhIAA60FYVnXMtAoTmGqFAfONx3AwGcNgCQ5JkAAA4AAMUdlhn2wMsXhyh+FIBrpvLuvM9gKKUcIZAAAmKOuI4Dj2LAIowew7733LkhPojMABi4hcylFblfBaEAQAAF8QlAA)
<!-- prettier-ignore -->
```sh
--parser markdown
```

**Input:**
<!-- prettier-ignore -->
```markdown
---
title: Real Projects
icon: splotch
order: 1
category:
  - Demo

docs:
  - name: Waline
    desc: A Simple, Safe Comment System.
    logo: https://waline.js.org/logo.png
    url: https://waline.js.org/en/
    repo: https://github.com/walinejs/waline
    preview: /assets/image/waline.jpg

  - name: zkSync Era
    desc: A trustless protocol that uses cryptographic validity proofs to provide scalable and low-cost transactions on Ethereum
    logo: https://docs.zksync.io/zksync_logo_black.svg
    url: https://docs.zksync.io/
    repo: https://github.com/matter-labs/zksync-web-era-docs
    preview: /assets/image/zksync.jpg

portfolios:
  - name: Cheng Shang
    url: https://cheng-shang.me
    preview: https://cheng-shang.me/portfolio.avif
    repo: https://github.com/Mister-Hope/cheng-shang-portfolio

  - name: Cheng Shang
    url: https://cheng-shang.me
    preview: https://cheng-shang.me/portfolio.avif
    repo: https://github.com/Mister-Hope/cheng-shang-portfolio

blogs:
  - name: Mr.Hope's Blog
    desc: Where there is light, there is hope
    logo: https://mister-hope.com/logo.svg
    url: https://mister-hope.com
    repo: https://github.com/Mister-Hope/Mister-Hope.github.io
    preview: /assets/image/mrhope.jpg

  - name: Mo7's Blog
    desc: Something wonderful is about to happen.
    logo: https://file.mo7.cc/static/lxh_gif/lxh_71.gif
    url: https://blog.mo7.cc/en/
    repo: https://github.com/mo7cc/BlogSource.git
    preview: https://file.mo7.cc/disk/blog_preview_en.png

  - name: Guo's Page
    desc: Project documentation and study notes
    logo: https://guoxicheng.top/logo.png
    url: https://guoxicheng.top/en
    repo: https://github.com/GuoXiCheng/guoxicheng.github.io
    preview: /assets/image/guo-s-page-en.png
---


```

**Output:**
<!-- prettier-ignore -->
```markdown
---
title: Real Projects
icon: splotch
order: 1
category:
  - Demo

docs:
  - name: Waline
    desc: A Simple, Safe Comment System.
    logo: https://waline.js.org/logo.png
    url: https://waline.js.org/en/
    repo: https://github.com/walinejs/waline
    preview: /assets/image/waline.jpg

  - name: zkSync Era
    desc: A trustless protocol that uses cryptographic validity proofs to provide scalable and low-cost transactions on Ethereum
    logo: https://docs.zksync.io/zksync_logo_black.svg
    url: https://docs.zksync.io/
    repo: https://github.com/matter-labs/zksync-web-era-docs
    preview: /assets/image/zksync.jpg

portfolios:
  - name: Cheng Shang
    url: https://cheng-shang.me
    preview: https://cheng-shang.me/portfolio.avif
    repo: https://github.com/Mister-Hope/cheng-shang-portfolio

  - name: Cheng Shang
    url: https://cheng-shang.me
    preview: https://cheng-shang.me/portfolio.avif
    repo: https://github.com/Mister-Hope/cheng-shang-portfolio

blogs:
  - name: Mr.Hope's Blog
    desc: Where there is light, there is hope
    logo: https://mister-hope.com/logo.svg
    url: https://mister-hope.com
    repo: https://github.com/Mister-Hope/Mister-Hope.github.io
    preview: /assets/image/mrhope.jpg
  - name: Mo7's Blog
    desc: Something wonderful is about to happen.
    logo: https://file.mo7.cc/static/lxh_gif/lxh_71.gif
    url: https://blog.mo7.cc/en/
    repo: https://github.com/mo7cc/BlogSource.git
    preview: https://file.mo7.cc/disk/blog_preview_en.png
  - name: Guo's Page
    desc: Project documentation and study notes
    logo: https://guoxicheng.top/logo.png
    url: https://guoxicheng.top/en
    repo: https://github.com/GuoXiCheng/guoxicheng.github.io
    preview: /assets/image/guo-s-page-en.png
---

```

**Expected output:**

Empty lines in `blogs` not being removed


**Why?**

......


Hints:
Interesting, thanks for reporting! A few shorter examples:

## Line gaps in the last list are removed

**Prettier 3.3.0**
[Playground link](https://prettier.io/playground/#N4Igxg9gdgLgprEAuEBadAdKAzCECMSWABMasQEYCGATvvliWZbfgEyNSnnV0DMnXBDZEuzXmwZQmPWmw7SxsmmwGKhfUdxY0+UmTr4KDvPmvSpOAWVoBrACYQA7lBAAaEBAAOMAJbQAZ2RQWhpnAAVaBCCUKgAbJyoATyCPChoqMFs4GABlKgBbOAAZXyg4ZGx4gLg0jKyc3K9MsoBzZBgaAFdakBqC3w7u3rgADy84Gl8i2HiAFUmoWl84GKq4mo8Atri4AEUuiHhK6t6AKwDR3J39w+Okdc2QAEc7uHCwrxiQKgDUcrg9kB7hAnSovjibQAwhACgUqMgfnE4iDtlBWrsAIIwTq+ChdeDhSalconDa9AAWMAKcQA6hTfPAAs0wHBctFGb4AG6MpKIsABVIgLk9ACSUCBsFyYCmPkxEtyMCSuzJTy8YRqtIyXkR6tWky5FQ8ZRqNBgHyorXhqt6zRopsR8JoDmcrg86rKMFpvnsMApyAAHAAGDw0OCvXxhi1WhEPU4eGBUCje33+pBsDxdGpzJNreMgOAFCiAoH2YpUdFdS1wABiEBo8JxbURVAJEBAAF8O0A)
<!-- prettier-ignore -->
```sh
--parser markdown
```

**Input:**
<!-- prettier-ignore -->
```markdown
---
foo1:
  - bar11

  - bar12

  - bar13

foo2:
  - bar21

  - bar22

  - bar23

foo3:
  - bar31

  - bar32

  - bar33
---

Markdown
```

**Output:**
<!-- prettier-ignore -->
```markdown
---
foo1:
  - bar11

  - bar12

  - bar13

foo2:
  - bar21

  - bar22

  - bar23

foo3:
  - bar31
  - bar32
  - bar33
---

Markdown

```

### Line gaps in last list are _not_ removed if followed by a comment

**Prettier 3.3.0**
[Playground link](https://prettier.io/playground/#N4Igxg9gdgLgprEAuEBadAdKAzCECMSWABMasQEYCGATvvliWZbfgEyNSnnV0DMnXBDZEuzXmwZQmPWmw7SxsmmwGKhfUdxY0+UmTr4KDvPmqwBiYgGcIAWzjFIdh7E7pUnALK0A1gBMIAHcoEAAaEAgABxgAS2hrZFBaGmCABVoERJQqABsgqgBPRIiKGiowXzgYAGUqBwAZWKg4ZGw86zhS8srqmqiK5oBzZBgaAFcukE67WNGJqbgADyi4GljXGDyAFTWoWli4bPbczojrYdy4AEVxiHg2jqmAK2slmsubu4ekE7OQACO3zgaVSUWyICo1lQLTg-jh4RAYyosVywwAwvY7FRkJDcrlERcoEMrgBBGBjWIUcbwNJrJotR6nKYACxgdlyAHUWbF4NYBmA4DUsrzYgA3XmFXFgawlEBiyYASSg8NgNTA6xipJVNRghSuTP+UVSnU55SiuONRzWYtaEWanRoMFBVCG2MNUwGNEduOxNACwVCEWNzRgnNi-hgLOQAA4AAwRGhwIGxJMut0435PCJbCjhyPRpBsCLjTrbKgUY7ZkBwOwUOHw-wNKjE8auuAAMQgNGxFOGuKoNIgIAAviOgA)
<!-- prettier-ignore -->
```sh
--parser markdown
```

**Input:**
<!-- prettier-ignore -->
```markdown
---
foo1:
  - bar11

  - bar12

  - bar13

foo2:
  - bar21

  - bar22

  - bar23

foo3:
  - bar31

  - bar32

  - bar33

# some comment

---

Markdown
```

**Output:**
<!-- prettier-ignore -->
```markdown
---
foo1:
  - bar11

  - bar12

  - bar13

foo2:
  - bar21

  - bar22

  - bar23

foo3:
  - bar31

  - bar32

  - bar33
# some comment
---

Markdown

```

### Line gaps in last list are _not_ removed if followed by another key

**Prettier 3.3.0**
[Playground link](https://prettier.io/playground/#N4Igxg9gdgLgprEAuEBadAdKAzCECMSWABMasQEYCGATvvliWZbfgEyNSnnV0DMnXBDZEuzXmwZQmPWmw7SxsmmwGKhfUdxY0+UmTr4KDvPmqxCALEmKXjUdKk4BZWgGsAJhADuUEABoQCAAHGABLaABnZFBaGh8ABVoEaJQqABtvKgBPaMCKGiowNzgYAGUqAFs4ABkwqDhkbAzIuHzC4tKy4KL6gHNkGBoAVzaQVsqwwZGxuAAPYLgaMOrYDIAVJahaMLhU5vTWwMj+9LgARWGIeCaWsYArSLmy04urm6QDo5AAR3e4BLxYKpEBUSKoBpwDxQgIgIZUMLpfoAYQglUqVGQoPS6VhJygfTOAEEYEMwhRhvAEks6g1bocxgALGCVdIAdUZYXgkR6YDgZRSXLCADcudksWBInkQMLRgBJKDQ2BlMDLUJExVlGDZM7077BeKtNmFYJYg17JbCxqBeqtGgwQFUPoYvVjHo0O1YjE0Tw+PyBA31GBssIeGCM5AADgADIEaHA-mF447nZjPndAjAqBQQ2GI0g2IFhq11tn9hmQHBKhQodCPDUqAThk64AAxCA0DGk-pYqiUiAgAC+g6AA)
<!-- prettier-ignore -->
```sh
--parser markdown
```

**Input:**
<!-- prettier-ignore -->
```markdown
---
foo1:
  - bar11

  - bar12

  - bar13

foo2:
  - bar21

  - bar22

  - bar23

foo3:
  - bar31

  - bar32

  - bar33

foo4: 42

---

Markdown
```

**Output:**
<!-- prettier-ignore -->
```markdown
---
foo1:
  - bar11

  - bar12

  - bar13

foo2:
  - bar21

  - bar22

  - bar23

foo3:
  - bar31

  - bar32

  - bar33

foo4: 42
---

Markdown

```

### Line gaps in last list are _not_ removed if it has duplicate key

**Prettier 3.3.0**
[Playground link](https://prettier.io/playground/#N4Igxg9gdgLgprEAuEBadAdKAzCECMSWABMasQEYCGATvvliWZbfgEyNSnnV0DMnXBDZEuzXmwZQmPWmw7SxsmmwGKhI4gGItxACYBXAA4AbAJZgq8YgGs4ATxksafKU958F72nzXpUnACytDZ6EADuUCAANCAQRjBm0ADOyKC0NBEACrQIqShUJuFU9qmxFDRUYHYwAMpUALZwADJmUHDI2IXJcOWV1XB1RlVtAObIMDQGvSA9DWYTUzNwAB5GcDRmTbCFACobULRmcPldJj2xyWMmcACKBhDwnd0zAFbJK7XXdw9PSGcXEAAR1+cCymSM+RAVGSqHacD0CJiIEmVDM5igowAwhAGg0qMhoSYTMirpibgBBGCTMwUAzwLIbVrtZ7nGYACxgDRMAHV2WZ4MlhmA4LU8gKzAA3AX2QlgZJlECS6YASSgiNgtTAmwSFPVtRg9hurMBRkyPR5lSMhLNJw2ko6sTaPRoMHBVFG+JNM2GNBdhPxNFCESisTNbRgPLMehg7OQAA4AAyxGhwEFmVPuz0E-4vWIwKgUKMxuNINixAw9XaF055kBwBoUBGIvTNKiYgweuAAMQgNHx1LGhKo9IgIAAvuOgA)
<!-- prettier-ignore -->
```sh
--parser markdown
```

**Input:**
<!-- prettier-ignore -->
```markdown
---
foo1:
  - bar11

  - bar12

  - bar13

foo2:
  - bar21

  - bar22

  - bar23

foo2: ## duplicate key
  - bar31

  - bar32

  - bar33
---

Markdown
```

**Output:**
<!-- prettier-ignore -->
```markdown
---
foo1:
  - bar11

  - bar12

  - bar13

foo2:
  - bar21

  - bar22

  - bar23

foo2: ## duplicate key
  - bar31

  - bar32

  - bar33
---

Markdown

```

### Line breaks are _not_ removed in plain yaml

**Prettier 3.3.0**
[Playground link](https://prettier.io/playground/#N4Igxg9gdgLgprEAuEAzCECMSA6UAE+AtPgEYCGATppnnoSRdQEx0HFlWYDMb6EzXO0ZVmtKPQ5NmrCcM6VmvOf25CGC7uMkjK3WTs3KQAGhAQADjACW0AM7JQVShADuABSoIHKcgBtXcgBPBzNSSnIwAGs4GABlcgBbOAAZayg4ZFR-OzgwiOjYuItI9IBzZBhKAFc8kFzE60qaurgADws4Smtk2H8AFS6oKms4H2y-XLM7cr84AEVqiHgsnLqAKzs2uNmFpZWkCamQAEd9uHcXCx8QcjsiDLgAE2fTECryaz9ygGEIRMS5GQtz8fjeMygZTmAEEYFVrKRqvB3F00hlVpM6gALGCJPwAdSx1ngdhKYDgcW8xOsADdiUFgWA7KEQDTagBJKAvWBxMDdKzQrlxGBBOYY44WFy5fERCzAyVjLo0zJmdK5SgwS7kMqA8V1EqUdXAoJJMFmSXpGD46xPGBY5AADgADGZKHAztY3VqdUDDmszDByKRrbb7UhmGZqrl+kHxv6QHBEqRni8nilyJDqtq4AAxCCUQFw8rA8hIiAgAC+FaAA)
<!-- prettier-ignore -->
```sh
--parser yaml
```

**Input:**
<!-- prettier-ignore -->
```yaml
foo1:
  - bar11

  - bar12

  - bar13

foo2:
  - bar21

  - bar22

  - bar23

foo3:
  - bar31

  - bar32

  - bar33

```

**Output:**
<!-- prettier-ignore -->
```yaml
foo1:
  - bar11

  - bar12

  - bar13

foo2:
  - bar21

  - bar22

  - bar23

foo3:
  - bar31

  - bar32

  - bar33

```

I flicked between markdown and mdx parser, did not find any difference. Looks like a bug in frontmatter to me.
A Note: this only happens in 3.3.0, while the previous versions are good. I think this can help you locating the issue
It seems there is bug in [`stripTrailingHardline`](https://github.com/prettier/prettier/blob/b26f56bd316a3ed11c33f50cd4a3dac44a4ee529/src/document/utils.js#L256)

Patch:
diff --git a/src/document/utils.js b/src/document/utils.js
--- a/src/document/utils.js
+++ b/src/document/utils.js
@@ -212,7 +212,6 @@ function stripTrailingHardlineFromParts(parts) {
 
 function stripTrailingHardlineFromDoc(doc) {
   switch (getDocType(doc)) {
-    case DOC_TYPE_ALIGN:
     case DOC_TYPE_INDENT:
     case DOC_TYPE_INDENT_IF_BREAK:
     case DOC_TYPE_GROUP:
@@ -238,6 +237,7 @@ function stripTrailingHardlineFromDoc(doc) {
     case DOC_TYPE_STRING:
       return doc.replace(/[\n\r]*$/, "");
 
+    case DOC_TYPE_ALIGN:
     case DOC_TYPE_CURSOR:
     case DOC_TYPE_TRIM:
     case DOC_TYPE_LINE_SUFFIX_BOUNDARY:


Test patch:
diff --git a/tests/format/misc/embedded_language_formatting/in-markdown/__snapshots__/format.test.js.snap b/tests/format/misc/embedded_language_formatting/in-markdown/__snapshots__/format.test.js.snap
--- a/tests/format/misc/embedded_language_formatting/in-markdown/__snapshots__/format.test.js.snap
+++ b/tests/format/misc/embedded_language_formatting/in-markdown/__snapshots__/format.test.js.snap
@@ -106,6 +106,71 @@ Hello world!
 ================================================================================
 `;
 
+exports[`issue-16342.md - {"embeddedLanguageFormatting":"off"} format 1`] = `
+====================================options=====================================
+embeddedLanguageFormatting: "off"
+parsers: ["markdown"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+---
+foo:
+  - bar1
+
+  - bar2
+
+  - bar3
+---
+
+Markdown
+
+=====================================output=====================================
+---
+foo:
+  - bar1
+
+  - bar2
+
+  - bar3
+---
+
+Markdown
+
+================================================================================
+`;
+
+exports[`issue-16342.md format 1`] = `
+====================================options=====================================
+parsers: ["markdown"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+---
+foo:
+  - bar1
+
+  - bar2
+
+  - bar3
+---
+
+Markdown
+
+=====================================output=====================================
+---
+foo:
+  - bar1
+
+  - bar2
+
+  - bar3
+---
+
+Markdown
+
+================================================================================
+`;
+
 exports[`test.md - {"embeddedLanguageFormatting":"off"} format 1`] = `
 ====================================options=====================================
 embeddedLanguageFormatting: "off"
diff --git a/tests/format/misc/embedded_language_formatting/in-markdown/issue-16342.md b/tests/format/misc/embedded_language_formatting/in-markdown/issue-16342.md
new file mode 100644
--- /dev/null
+++ b/tests/format/misc/embedded_language_formatting/in-markdown/issue-16342.md
@@ -0,0 +1,10 @@
+---
+foo:
+  - bar1
+
+  - bar2
+
+  - bar3
+---
+
+Markdown