Skip to content

Guide

Getting Started

Simply install prettier and @prettier/plugin-pug as your project’s npm devDependencies:

shell
npm add --save-dev prettier @prettier/plugin-pug
shell
pnpm add --save-dev prettier @prettier/plugin-pug
shell
yarn add --dev prettier @prettier/plugin-pug

Usage

CLI

Format all pug files in your project:

shell
npx prettier --write "**/*.pug" --plugin="@prettier/plugin-pug"
shell
pnpm prettier --write "**/*.pug" --plugin="@prettier/plugin-pug"
shell
yarn prettier --write "**/*.pug" --plugin="@prettier/plugin-pug"

Configuration file

You can write your .prettierrc.cjs like this to get fully type check support with documentation:

js
// @ts-check
/// <reference types="@prettier/plugin-pug/src/prettier" />

/**
 * @type {import('prettier').Options}
 */
module.exports = {
  plugins: ['@prettier/plugin-pug'],

  printWidth: 120,
  singleQuote: true,

  pugSingleQuote: false,
  // ... more pug* options
};

API

Using the plugins option on the API, you can format .pug files:

js
await prettier.format('code', {
  parser: 'pug',
  plugins: ['@prettier/plugin-pug'],
});

Selectively ignoring automatic formatting

You can disable code formatting for a particular element by adding //- prettier-ignore comments in your pug templates:

pug
div.text( color =   "primary",  disabled  ="true"  )
//- prettier-ignore
div.text( color =   "primary",  disabled  ="true"  )
//- prettier-ignore: because of reasons
div
  div.text( color =   "primary",  disabled  ="true"  )
pug
.text(color="primary", disabled)
//- prettier-ignore
div.text( color =   "primary",  disabled  ="true"  )
//- prettier-ignore: because of reasons
div
  div.text( color =   "primary",  disabled  ="true"  )

You can also disable code formatting in Markdown for a particular ```pug block by adding<!-- prettier-ignore --> before the block:

markdown
Pug code with preserved custom formatting:

<!-- prettier-ignore -->
```pug
div.text( color =   "primary",  disabled  ="true"  )
```

Pug code with automatic formatting:

```pug
.text(color="primary", disabled)
```

Released under the MIT License.