Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] Add option to skip formatting the Frontmatter #417

Merged
merged 4 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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} />
Loading