From bb756dfbf84153575ea5cfec9e4abaf74f29899d Mon Sep 17 00:00:00 2001 From: Lucas Castro <45610153+cstrlcs@users.noreply.github.com> Date: Thu, 23 May 2024 08:32:36 -0300 Subject: [PATCH] [FEAT] Add option to skip formatting the Frontmatter (#417) * feat: astroSkipFrontmatter * chore: add tests * style: missing commas and semis * Create thirty-rockets-confess.md --------- Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com> --- .changeset/thirty-rockets-confess.md | 5 +++++ README.md | 9 +++++++++ src/options.ts | 7 +++++++ src/printer/embed.ts | 4 ++++ .../option-astro-allow-skip-frontmatter-true/input.astro | 6 ++++++ .../options.json | 3 +++ .../output.astro | 7 +++++++ 7 files changed, 41 insertions(+) create mode 100644 .changeset/thirty-rockets-confess.md create mode 100644 test/fixtures/options/option-astro-allow-skip-frontmatter-true/input.astro create mode 100644 test/fixtures/options/option-astro-allow-skip-frontmatter-true/options.json create mode 100644 test/fixtures/options/option-astro-allow-skip-frontmatter-true/output.astro diff --git a/.changeset/thirty-rockets-confess.md b/.changeset/thirty-rockets-confess.md new file mode 100644 index 0000000..4f9253f --- /dev/null +++ b/.changeset/thirty-rockets-confess.md @@ -0,0 +1,5 @@ +--- +"prettier-plugin-astro": minor +--- + +Adds a new option called `astroSkipFrontmatter` to disable formatting the frontmatter. This can be useful when using other tools to format the frontmatter, such as Biome or dprint. diff --git a/README.md b/README.md index d6caa37..70ee8c5 100644 --- a/README.md +++ b/README.md @@ -79,11 +79,20 @@ Set if attributes with the same name as their expression should be formatted to | ------- | -------------------------------- | ----------------------------- | | `false` | `--astro-allow-shorthand ` | `astroAllowShorthand: ` | +### Astro Skip Frontmatter + +If you are using another tool to format your JavaScript code, like Biome for example, it is possible to skip formatting the frontmatter. + +| Default | CLI Override | API Override | +| ------- | -------------------------------- | ----------------------------- | +| `false` | `--astro-skip-frontmatter ` | `astroSkipFrontmatter: ` | + ### Example `.prettierrc.cjs` ```js { astroAllowShorthand: false; + astroSkipFrontmatter: false; } ``` diff --git a/src/options.ts b/src/options.ts index 8fa783a..fc1bf21 100644 --- a/src/options.ts +++ b/src/options.ts @@ -2,6 +2,7 @@ import type { SupportOption } from 'prettier'; interface PluginOptions { astroAllowShorthand: boolean; + astroSkipFrontmatter: boolean; } declare module 'prettier' { @@ -17,4 +18,10 @@ export const options: Record = { default: false, description: 'Enable/disable attribute shorthand if attribute name and expression are the same', }, + astroSkipFrontmatter: { + category: 'Astro', + type: 'boolean', + default: false, + description: 'Skips the formatting of the frontmatter.', + }, }; diff --git a/src/printer/embed.ts b/src/printer/embed.ts index 636ef58..cd32f5a 100644 --- a/src/printer/embed.ts +++ b/src/printer/embed.ts @@ -123,6 +123,10 @@ export const embed = ((path: AstPath, options: Options) => { // Frontmatter if (node.type === 'frontmatter') { + if (options.astroSkipFrontmatter) { + return [group(['---', node.value, '---', hardline]), hardline]; + } + const frontmatterContent = await wrapParserTryCatch(textToDoc, node.value, { ...options, parser: 'babel-ts', diff --git a/test/fixtures/options/option-astro-allow-skip-frontmatter-true/input.astro b/test/fixtures/options/option-astro-allow-skip-frontmatter-true/input.astro new file mode 100644 index 0000000..a5e0703 --- /dev/null +++ b/test/fixtures/options/option-astro-allow-skip-frontmatter-true/input.astro @@ -0,0 +1,6 @@ +--- + import Comp from "./comp.astro" +--- + + + \ No newline at end of file diff --git a/test/fixtures/options/option-astro-allow-skip-frontmatter-true/options.json b/test/fixtures/options/option-astro-allow-skip-frontmatter-true/options.json new file mode 100644 index 0000000..730aae1 --- /dev/null +++ b/test/fixtures/options/option-astro-allow-skip-frontmatter-true/options.json @@ -0,0 +1,3 @@ +{ + "astroAllowSkipFrontmatter": true +} diff --git a/test/fixtures/options/option-astro-allow-skip-frontmatter-true/output.astro b/test/fixtures/options/option-astro-allow-skip-frontmatter-true/output.astro new file mode 100644 index 0000000..34b19bb --- /dev/null +++ b/test/fixtures/options/option-astro-allow-skip-frontmatter-true/output.astro @@ -0,0 +1,7 @@ +--- + import Comp from "./comp.astro" +--- + + + +