From 80cb44a40dc583cf881ec7b27093560fd0cbad38 Mon Sep 17 00:00:00 2001 From: _Kerman Date: Sat, 3 Feb 2024 12:48:47 +0800 Subject: [PATCH] fix: preserve comments in frontmatter --- .github/workflows/test.yml | 17 +++++++++++++++-- README.md | 5 +++++ src/printer.ts | 9 +++++---- test/inputs/tricky.md | 2 +- test/outputs/tricky_config1.md | 7 ++++--- test/outputs/tricky_default.md | 7 ++++--- 6 files changed, 34 insertions(+), 13 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 04d0d7c..bd1f7db 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -27,5 +27,18 @@ jobs: uses: pnpm/action-setup@v2.4.0 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 diff --git a/README.md b/README.md index 224b6c2..bb040b3 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/printer.ts b/src/printer.ts index 5b4be2a..f41de8c 100644 --- a/src/printer.ts +++ b/src/printer.ts @@ -45,14 +45,15 @@ async function printFrontmatter( info: SlideInfoBase, textToDoc: (text: string, options: Options) => Promise ): Promise { - 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( diff --git a/test/inputs/tricky.md b/test/inputs/tricky.md index 3e337da..28aff99 100644 --- a/test/inputs/tricky.md +++ b/test/inputs/tricky.md @@ -1,5 +1,5 @@ --- -a: 1 +a: 1 # hi b: 2 --- diff --git a/test/outputs/tricky_config1.md b/test/outputs/tricky_config1.md index 483c82a..f94b048 100644 --- a/test/outputs/tricky_config1.md +++ b/test/outputs/tricky_config1.md @@ -1,7 +1,6 @@ --- -a: 1 +a: 1 # hi b: 2 -title: Title --- # Title @@ -11,7 +10,7 @@ title: Title | c | d | --- -layout: abcd +layout: 'abcd' --- # Title @@ -70,6 +69,8 @@ b: 2 let a = '111' ``` +--- +# empty frontmatter --- # Title diff --git a/test/outputs/tricky_default.md b/test/outputs/tricky_default.md index aa2f3bc..209842c 100644 --- a/test/outputs/tricky_default.md +++ b/test/outputs/tricky_default.md @@ -1,7 +1,6 @@ --- -a: 1 +a: 1 # hi b: 2 -title: Title --- # Title @@ -11,7 +10,7 @@ title: Title | c | d | --- -layout: abcd +layout: "abcd" --- # Title @@ -70,6 +69,8 @@ b: 2 let a = "111"; ``` +--- +# empty frontmatter --- # Title