Skip to content

Commit

Permalink
[FEAT] Add option to skip formatting the Frontmatter (#417)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
cstrlcs and Princesseuh authored May 23, 2024
1 parent 5f7c26e commit bb756df
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-rockets-confess.md
Original file line number Diff line number Diff line change
@@ -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.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,20 @@ Set if attributes with the same name as their expression should be formatted to
| ------- | -------------------------------- | ----------------------------- |
| `false` | `--astro-allow-shorthand <bool>` | `astroAllowShorthand: <bool>` |

### 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 <bool>` | `astroSkipFrontmatter: <bool>` |

### Example `.prettierrc.cjs`

```js
{
astroAllowShorthand: false;
astroSkipFrontmatter: false;
}
```

Expand Down
7 changes: 7 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { SupportOption } from 'prettier';

interface PluginOptions {
astroAllowShorthand: boolean;
astroSkipFrontmatter: boolean;
}

declare module 'prettier' {
Expand All @@ -17,4 +18,10 @@ export const options: Record<keyof PluginOptions, SupportOption> = {
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.',
},
};
4 changes: 4 additions & 0 deletions src/printer/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
import Comp from "./comp.astro"
---
<Comp options={options} />
<Comp options={ options } />
<Comp options={ options } />
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"astroAllowSkipFrontmatter": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
import Comp from "./comp.astro"
---

<Comp options={options} />
<Comp options={options} />
<Comp options={options} />

0 comments on commit bb756df

Please sign in to comment.