Skip to content

Commit

Permalink
fix: preserve comments in frontmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
KermanX committed Feb 3, 2024
1 parent e656b29 commit 80cb44a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
17 changes: 15 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,18 @@ jobs:
uses: pnpm/[email protected]
with:
version: 8
- name: Install and run tests
run: pnpm it
- name: Clone KermanX/slidev
run: git clone https://github.com/KermanX/slidev
working-directory: ../
- name: Install KermanX/slidev
run: pnpm i
working-directory: ../slidev
- name: Build KermanX/slidev
run: pnpm build
working-directory: ../slidev
- name: Install modified version of Slidev
run: |
pnpm i ../slidev/packages/parser
pnpm i -D ../slidev/packages/types
- name: Run tests
run: pnpm test
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ A [Prettier](https://prettier.io/) plugin for [Slidev](https://sli.dev/).
npm i -D prettier prettier-plugin-slidev
```

Currently, it requires https://github.com/slidevjs/slidev/pull/1268 to run correctly. So you should install `@slidev/parser` and `@slidev/types` from https://github.com/KermanX/slidev/tree/frontmatter-raw :

```sh
```

### Activate the plugin

Create or modify your [prettier configuration file](https://prettier.io/docs/en/configuration) to activate the plugin:
Expand Down
9 changes: 5 additions & 4 deletions src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ async function printFrontmatter(
info: SlideInfoBase,
textToDoc: (text: string, options: Options) => Promise<Doc>
): Promise<Doc[]> {
if (Object.keys(info.frontmatter ?? {}).length === 0) return [];
const trimed = info.frontmatterRaw?.trim() ?? "";
if (trimed.length === 0) return [];

const frontmatter = await textToDoc(YAML.dump(info.frontmatter), {
const formatted = await textToDoc(trimed, {
parser: "yaml",
});
return info.frontmatterStyle === "yaml"
? [hardline, "```yaml", hardline, frontmatter, hardline, "```", hardline]
: [frontmatter, hardline, "---", hardline];
? [hardline, "```yaml", hardline, formatted, hardline, "```", hardline]
: [formatted, hardline, "---", hardline];
}

async function printContent(
Expand Down
2 changes: 1 addition & 1 deletion test/inputs/tricky.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
a: 1
a: 1 # hi
b: 2
---

Expand Down
7 changes: 4 additions & 3 deletions test/outputs/tricky_config1.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
a: 1
a: 1 # hi
b: 2
title: Title
---

# Title
Expand All @@ -11,7 +10,7 @@ title: Title
| c | d |

---
layout: abcd
layout: 'abcd'
---

# Title
Expand Down Expand Up @@ -70,6 +69,8 @@ b: 2
let a = '111'
```

---
# empty frontmatter
---

# Title
Expand Down
7 changes: 4 additions & 3 deletions test/outputs/tricky_default.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
a: 1
a: 1 # hi
b: 2
title: Title
---

# Title
Expand All @@ -11,7 +10,7 @@ title: Title
| c | d |

---
layout: abcd
layout: "abcd"
---

# Title
Expand Down Expand Up @@ -70,6 +69,8 @@ b: 2
let a = "111";
```

---
# empty frontmatter
---

# Title
Expand Down

0 comments on commit 80cb44a

Please sign in to comment.