Skip to content

About

Additional YAML formatting options for Prettier

Topics

Resources

Security policy

Stars

12 stars

Watchers

1 watching

Forks

Repository files navigation

prettier-plugin-yaml

Additional YAML formatting options for Prettier.

 

Install

npm install --save-dev prettier-plugin-yaml
pnpm add --save-dev prettier-plugin-yaml

Usage

Reference prettier-plugin-yaml in your Prettier config:

{
    "plugins": [
        "prettier-plugin-yaml"
    ]
}

If you’re using any other YAML plugins, make sure prettier-plugin-yaml is listed last.

Options

In addition to Prettier’s built-in options that affect YAML formatting, prettier-plugin-yaml offers additional configuration options.

interface PluginOptions {
    /**
     * Enforce a single block style for multi-line string values. Does not apply
     * to flow collections.
     */
    yamlBlockStyle?: 'folded' | 'literal' | undefined;
    /**
     * Enforce a single collection style for maps and sequences.
     */
    yamlCollectionStyle?: 'block' | 'flow' | undefined;
    /**
     * Quote all scalar mapping keys. Leaves YAML merge keys and explicitly
     * tagged non-string keys unchanged. Removes unnecessary quotes when disabled.
     * @default false
     */
    yamlQuoteKeys?: boolean | undefined;
    /**
     * Quote keys that match a specific pattern. Non-string keys are matched
     * based on their string representation. Leaves YAML merge keys and explicitly
     * tagged non-string keys unchanged. Doesn’t affect keys that must be quoted
     * anyway.
     */
    yamlQuoteKeysMatching?: string | undefined;
    /**
     * Quote all string values. Takes precedence over `yamlBlockStyle`. Removes
     * unnecessary quotes when disabled.
     * @default false
     */
    yamlQuoteValues?: boolean | undefined;
    /**
     * Quote values that match a specific pattern. Non-string values are matched
     * based on their string representation. Leaves explicitly tagged non-string
     * values unchanged. Takes precedence over `yamlBlockStyle`. Doesn’t affect
     * values that must be quoted anyway.
     */
    yamlQuoteValuesMatching?: string | undefined;
}

FAQ

How do I automatically quote integer values?

Use yamlQuoteValuesMatching. Unlike yamlQuoteValues, which only applies to string values, it matches values based on their string representation. For example, to quote integers:

{
    "yamlQuoteValuesMatching": "^\\d+$"
}

You can quote boolean and null values similarly:

{
    "yamlQuoteValuesMatching": "^(true|false|null)$"
}

To quote mapping keys, use yamlQuoteKeys and yamlQuoteKeysMatching.

Why is the pattern a string instead of a regular expression?

yamlQuoteValuesMatching and yamlQuoteKeysMatching accept strings due to a limitation in how Prettier handles configuration values. RegExp is not a supported type, so the pattern must be provided as a string.

Internally, the plugin applies the pattern as new RegExp(pattern). Remember to escape backslashes and other special characters accordingly.

Related

License

MIT © Dom Porada

About

Additional YAML formatting options for Prettier

Topics

Resources

Security policy

Stars

12 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages