prettier-plugin-astro

repository·main·Indexed 20 days ago

https://github.com/withastro/prettier-plugin-astro

An official Prettier plugin that provides formatting support for .astro files. It includes the astro parser and printer to maintain consistent code style in Astro projects, with support for Astro-specific formatting options, shorthand attributes, and frontmatter.

Tokens
2.5K
Snippets
16
Records
17
Agent score
69%

What's inside prettier-plugin-astro

  1. Configure Prettier for Astro files

    main

    Add prettier-plugin-astro to the plugins array in your Prettier configuration.

    Recommended Configuration: For optimal compatibility across different package managers and plugins, manually specify the astro parser for .astro files using the overrides property.

    // .prettierrc.mjs
    /** @type {import("prettier").Config} */
    export default {
      plugins: ['prettier-plugin-astro'],
      overrides: [
        {
          files: '*.astro',
          options: {
            parser: 'astro',
          },
        },
      ],
    };
  2. Format Astro files in VS Code using the Prettier extension

    main

    The official Astro VS Code extension includes this plugin by default. You only need to install the Prettier extension and configure it manually if you prefer using the Prettier extension's specific features (like the status toolbar) or are not using the Astro extension.

    To use the Prettier extension for Astro files, add these settings to your VS Code settings.json:

    1. prettier.documentSelectors: Tells Prettier to recognize .astro files.
    2. [astro].editor.defaultFormatter: Sets Prettier as the default formatter for the Astro language.

    When reporting issues regarding VS Code formatting, specify whether you are using the Astro extension or the Prettier extension.

    {
      "prettier.documentSelectors": ["**/*.astro"],
      "[astro]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
      }
    }
  3. Configure Astro-specific formatting options

    main

    The plugin provides two specific options to control Astro-specific formatting behavior. These can be set via a Prettier configuration file, CLI flags, or the Prettier API.

    {
      "astroAllowShorthand": false,
      "astroSkipFrontmatter": false
    }
  4. Configure Astro Prettier plugin options

    main

    The prettier-plugin-astro provides two configuration options to customize how .astro files are formatted. These options can be added to your Prettier configuration file (e.g., .prettierrc, .prettierrc.json, or .prettierrc.cjs).

    {
      "astroAllowShorthand": true,
      "astroSkipFrontmatter": true
    }
  5. Reference: Astro formatting options

    main

    Use these options to customize how the plugin handles shorthand attributes and frontmatter.

    | Option | Default | CLI Override | API Override |
    | ------- | ------- | ------------ | ------------ |
    | `astroAllowShorthand` | `false` | `--astro-allow-shorthand <bool>` | `astroAllowShorthand: <bool>` |
    | `astroSkipFrontmatter` | `false` | `--astro-skip-frontmatter <bool>` | `astroSkipFrontmatter: <bool>` |
  6. Format class names in Astro

    main

    The printClassNames utility reformats a string of class names. It splits the input by newlines, trims each line, and ensures that internal whitespace within a line is collapsed to a single space, while preserving leading indentation.

    printClassNames('  class1   class2\n  class3'); // Returns '  class1 class2\n  class3'
  7. Determine if a node is a block or inline element

    main

    The plugin distinguishes between block and inline elements to manage whitespace sensitivity.

    • Block Elements: These are typically element nodes where the tag name is in the blockElements list. If htmlWhitespaceSensitivity is set to ignore in Prettier options, all tags (elements, custom-elements, components, fragments) are treated as block elements.
    • Inline Elements: Nodes that are TagLikeNode (element, component, custom-element, or fragment) but are NOT block elements and are NOT inside a <pre> tag or non-formattable attribute.
  8. Infer Prettier parser from type attributes

    main

    When dealing with content types (like in script tags), the plugin can infer which Prettier built-in parser to use based on the type attribute string.

    Mapping logic:

    • module, text/javascript, text/babel, application/javascript $\rightarrow$ babel
    • application/x-typescript $\rightarrow$ babel-ts
    • text/markdown $\rightarrow$ markdown
    • text/html $\rightarrow$ html
    • text/x-handlebars-template $\rightarrow$ glimmer
    • *json, *importmap, speculationrules $\rightarrow$ json
    • Default $\rightarrow$ babel-ts
    inferParserByTypeAttribute('text/javascript'); // Returns 'babel'
  9. Get preferred quotes for content

    main

    The getPreferredQuote utility determines whether to use single (') or double (") quotes for a string, based on a preferred quote and the existing content of the string. It aims to minimize the need for escaping quotes by choosing the quote character that appears less frequently in the raw content.

    getPreferredQuote(rawContent, preferredQuote); // Returns { quote, regex, escaped }