Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@prettier/plugin-pug

prettier60.5kMIT3.2.0TypeScript support: included

Prettier Pug Plugin

pug, prettier-plugin, prettier, formatter

readme

     Prettier           Pug     

Prettier Pug plugin

license: MIT NPM package downloads Code Style: Prettier Build Status

Intro

Prettier is an opinionated code formatter. It enforces a consistent style by parsing your code and re-printing it with its own rules that take the maximum line length into account, wrapping code when necessary.

This plugin adds support for the Pug language to Prettier.


Getting started

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

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

Usage

CLI

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

Configuration file (.prettierrc)

{
  "plugins": ["@prettier/plugin-pug"]
}

See Standard Prettier overrides and Pug specific options for more options.

API

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

Workaround / Known Issue

There are some code examples that are not formatted well with this plugin and can damage your code.
But there are workarounds for it. These generate even better pug code!

Example

Issue 114

If you have tags at the top/root that are indented, they will lose indentation due to a technical limitation of pug.
Please check these before committing after running this plugin for the first time and fix them manually.

Integration with editors

If you are using a text editor that supports Prettier integration (e.g. Atom), you can have all Prettier perks for your Pug code too!

Use VSCode extension to get support for VSCode.

In order to get @prettier/plugin-pug working in projects that do not have local npm dependencies, you can install this plugin globally:

npm install --global prettier @prettier/plugin-pug

In this case, you might need to check the settings of your editor’s Prettier extension to make sure that a globally installed Prettier is used when it is not found in project dependencies (i.e. package.json).

Nevertheless, it is recommended to rely on local copies of prettier and @prettier/plugin-pug as this reduces the chance of formatting conflicts between project collaborators. This may happen if different global versions of Prettier or its Pug plugin are used.

Installing @prettier/plugin-pug either locally or globally may require you to restart the editor if formatting does not work right away.

Implementation details

This plugin is written in TypeScript and its quality is maintained using Prettier and Vitest.

Contributing

If you’re interested in contributing to the development of Prettier for Pug, you can follow the CONTRIBUTING guide from Prettier, as it all applies to this repository too.

To run @prettier/plugin-pug locally:

  • Clone this repository.
  • Execute pnpm run preflight.

Credits

This project was inspired by https://github.com/gicentre/prettier-plugin-elm.
Many thanks also to @j-f1, @lipis and @azz for the help in transferring this repository to the prettier organization.

Thanks to @fisker for the constant help and support in terms of Prettier Core support.

Thanks to @Peilonrayz, who gave me the idea to rewrite the printer into a class and thus make the code a lot more maintainable.

Thanks to @lehni, @SkyaTura and @shadowgate15 for the massive contribution and the introduction of many new features into the project.

Thanks to @maoberlehner to acknowledge @prettier/plugin-pug as officially well TTD tested at Vue Amsterdam 2023.

changelog

Next

diff

3.1.0

diff

  • Add option to set indent depth of closing brackets (#502)
  • Fix class attributes not counting towards attribute wrapping (#503)

3.0.0

diff

BREAKING CHANGES

Dropped support for Prettier v2 and CJS (#458)

If you are using Prettier v2 / CJS, you need to stay on 2.5.2.

2.5.2

diff

This version does not contain any changes to the plugin itself, but only internal changes to the docs, build and CI.

This might be the last version that supports Prettier v2 as well as CJS.
The next version will probably the v3 release and will only support Prettier v3 and ESM.

2.5.1

diff

  • Fix whitespace in interpolations (#447)

2.5.0

diff

BREAKING CHANGES

  • Dropped support for node v14 (#441)

Other changes

  • Fix trailing whitespace after class (#442)

2.4.2

diff

  • No changes, just an update to TypeScript v5 and therefore possibly better emitted code.

2.4.1

diff

  • Omit leading semicolon in code expressions (#428)

2.4.0

diff

  • Fix trailing whitespace after tag (#425)
  • Add semi code interpolation option (#426)

2.3.0

diff

  • Transform trailing whitespace to recommended way (#417)

2.2.0

diff

  • Preserve script tag after //- prettier-ignore (#402)
  • Fix indent with pugExplicitDiv and pugClassNotation=attribute (#405)
  • Add pugPreserveAttributeBrackets option (#404)
  • Replace trimRight with trimEnd (c955b9f)

2.1.1

diff

  • Avoid use . or circular dependency in src/options files (#398)

2.1.0

diff

  • Rework logging (#172)
  • Update docs to VitePress v1 (alpha)
    • Known issue: The "Get Started" link is not working correctly right now.
      Click on "Guide" in the upper right nav menu for now.

2.0.0

diff

BREAKING CHANGES

  • Dropped support for node v12 (faf9f2e)

1.20.1

diff

  • Handle vue v- directives when framework is set to vue (#362)

1.20.0

diff

  • Fix single quotes in framework interpolation (#339)

1.19.3

diff

  • Add funding key to package.json (#350)

1.19.2

diff

  • Handle nested Angular translations (#329)
    at least don't fail for now

1.19.1

diff

  • Handle colon token in combination with pugClassLocation: 'after-attributes' (#326)

1.19.0

diff

  • New option pugClassLocation (#313)

1.18.0

diff

  • Handle singleQuote option more precisely (#45)
    • for some configurations, this could be a "breaking" change

1.17.3

diff

  • Treat v-t from vue-i18n as Vue expression (#310)

1.17.2

diff

  • Fix indentation issue of inline javascript (#307)

1.17.1

diff

  • Add type definitions that can be used in .prettierrc.cjs

1.17.0

diff

BREAKING CHANGES

  • Remove non prefixes options (#295)
    You now need to use e.g. pugAttributeSeparator instead of attributeSeparator
  • Replace pugClosingBracketPosition with pugBracketSameLine (#291)

Other changes

  • Preserve escaped quotes within bindings (#303)

1.16.7

diff

  • Fix handling of prettier-ignore (#292)

1.16.6

diff

  • Handle Vue slot shorthand formatting (#289)

1.16.5

diff

  • Fix indentation of templates starting with class or ID when using pugSingleFileComponentIndentation (#282)

1.16.4

diff

  • Fix trailing indentations after pipeless text (#278)

1.16.3

diff

  • Use pugTabWidth for calculating line length (#268)

1.16.2

diff

  • Preserve backticks for class and id attributes (#261)
  • Apply useTabs also for code interpolations (#262)

1.16.1

diff

  • Prevent wrong quotation (#255)

1.16.0

diff

  • New option pugExplicitDiv (#241)
  • Ignore formatting for single line interpolated attributes (#238)
  • Handle non-JS script content (#244)
  • Fix corrupt attribute values on dynamically bound Vue props without quotes (#250)

1.15.3

diff

  • Upgrade to Prettier v2.3
    This fixes pugSingleFileComponentIndentation option in combination with prettier v2.3

1.15.2

diff

  • Handle v-bind expression separately (4827855)

1.15.1

diff

  • Same as 1.15.0 but with dist folder (#211)

1.15.0

diff

  • Handle svelte syntax (#201)
  • Support bracket spacing for interpolated-code token (#208)
  • Fix invalid added ; in front of template string (#206)

1.14.0

diff

1.13.5

diff

  • Handle v-slot expression correctly (#180)

1.13.4

diff

  • Fix attribute interpolation using quotes (#179)

1.13.3

diff

  • Handle v-on expression separately (#176)

1.13.2

diff

  • Fix blank lines in inline syntax (#171)

1.13.1

diff

  • Set prettier peer dependency to >= 2.1.0 (#168)
  • Handle error thrown by interpolated code in pipeless script tag (#169)

1.13.0

diff

  • Option for disabling formatting of class and id literals (#36)
  • Switching from master to main branch (#166)

1.12.0

diff

  • Format value of style attribute in tags (#162)

1.11.0

diff

  • Handle for...of in vue v-for (#129)
  • Support embedded formatting of inline script and style tags (#158)
    If there are problems, please create an issue

1.10.1

diff

  • Fix handling of initial indentation for pugSingleFileComponentIndentation (#145)
  • Fix bracket interpolation in combination with pugSingleFileComponentIndentation (#148)
  • Do not wrap attributes in enclosed code interpolation (#149)
  • Print warning for parsed object shorthand notation (#147)
  • Add 'pug' to vscodeLanguageIds (#150)

1.10.0

diff

  • Implement pugEmptyAttributes and pugEmptyAttributesForceQuotes option (#128)
  • Improve warning message (#143, c0319bf)

1.9.0

diff

  • Support indentation for SFC (#140)
  • Fix issues with plain-text and nesting (#138)

1.8.1

diff

  • Improve conversion of tags to literals (#133)
  • Handle plain-text tokens after outdent (#135)

1.8.0

diff

  • Implement pugWrapAttributesThreshold option (#118)
  • Implement pugWrapAttributesPattern option (#126)
  • Support //- prettier-ignore comments inside pug templates (#125)
  • Add middle attributes sorting (#120)
  • Fix counting of current line-length (#121)
  • Fix handling of literal attributes (#130)
  • Add @lehni and @SkyaTura as contributors

1.7.0

diff

  • Support sorting of attributes with pugSortAttributesBeginning and pugSortAttributesEnd (#22)
    Example
    This feature was planned since 1.2.0, but it was always a bit unstable and opinionated.
    If there are any bugs, please report them.
  • Pass more prettier code-interpolation options (#117)
  • Allow "none" as an option for attributeSeparator (#102)
  • Improve warning message (03777f5)

1.6.1

diff

  • Fix wrapped attribute multiline code indentation (5a6082c)

1.6.0

diff

Big Change

This plugin now supports alias options!
You can configure pug embedded in vue with them :tada:

So instead of

{
  "singleQuote": true,
  "overrides": [
    {
      "files": ["*.pug"],
      "options": {
        "singleQuote": false
      }
    }
  ]
}

you can now write

{
  "singleQuote": true,
  "pugSingleQuote": false
}

Aliases added for:

  • pugPrintWidth
  • pugSingleQuote
  • pugTabWidth
  • pugUseTabs
  • pugBracketSpacing
  • pugSemi

Because prettier currently have something like a bug, you need to set e.g. "pugSingleQuote": "true" instead of "pugSingleQuote": true, if you want to set it to true
This needs to be done only for boolean true values

If an alias is not set, the plugin will fallback to the non alias version
And in the special case of singleQuote option will take the inverse as before
So no breaking changes are introduced

Other Changes

  • Fix escape id for complex id values (559ee6d)
  • Fix indentation of objects in multiline class attributes (#110)

1.5.1

diff

  • Support bracketSpacing option for attributes (#103)

1.5.0

diff

  • Support bracketSpacing option (only text-token) (#98)

1.4.4

diff

  • Trim input on left side (#97)

1.4.3

diff

  • Fix sorting of occurring id behind classes (#95)

1.4.2

diff

  • Ignore formatting of interpolation attribute values (#94)

1.4.1

diff

  • Fix indentation in multi-line blank lines (#89)

1.4.0

diff

  • Fix semi in code interpolation (#85)
  • Upgrade pug-lexer to 5.0.0
    Support new token eachOf

1.3.0

diff

  • Introduce new option closingBracketPosition (#82)
    Possible values: new-line and last-line

1.2.0

diff

  • Support Prettier v2 (Prettier v1 is no longer supported, use v1.1.11)

Minor change:

//- Input
v-btn(@click="toggle = !toggle")

//- Output (1.1.11)
v-btn(@click="(toggle = !toggle)")

//- Output (1.2.0)
v-btn(@click="toggle = !toggle")

1.1.11

diff

  • Just a dependency update

1.1.10

diff

  • Suppress formatting for Angular if there is more than one binding within an attribute value (#78)
  • Support more Vue expressions (v-if, v-else-if, v-for, v-text, v-html) (b671027)

1.1.9

diff

  • Improve accuracy of printWidth (0ffe395)

1.1.8

diff

  • Calculate correct line width (26fefff)
  • Fix indentation for doctype (51d3860)
  • Handle Angular pipe with parameter (#70)
  • Handle space for indented text (#74)

1.1.7

diff

  • Fix handling of classes (#72)

1.1.6

diff

  • Improve handling of Angular attribute values (#67, #68)
  • Restructure internal code
  • Add issue templates

1.1.5

diff

  • Improve handling of id and class attributes (#63)

  • Some minor internal changes like

1.1.4

diff

  • Fix wrapping of framework bindings (#61)

1.1.3

diff

  • Fix valid backslash escaping (#59)

1.1.2

diff

  • Transform only valid id attributes (#51)
  • Indent interpolated-code after outdent (#52)
  • Suggest workarounds in README.md (8d69f1e, 58157f6)

1.1.1

diff

  • Fix space after id followed by interpolated code (#49)

1.1.0

diff

  • Introduce new option commentPreserveSpaces (#47)
    Possible values: keep-all, keep-leading and trim-all

1.0.11

diff

  • Handle yield token (94fdc0c)
  • Handle slash token (194f103)
  • Dont format code if formatter produces error (e105bae)
  • Dont format code is multiline (49b4b4c)
  • Fix code slicing (8478562)
  • Fix whitespace issues (b7d26c7)
  • Fix whitespace after interpolation and call (7670ade)
  • Fix whitespace after filter token (4ac7812)
  • Fix missing key for each token (aff4969)
  • Fix whitespace before start-pug-interpolationtoken (09e96a3)
  • Fix escaping of non-interpolated text (e5c3944)
  • Fix only text body (c32a8d1)
  • Fix indent after newline for interpolated-code token (bfb90c6)
  • Fix partial quoted surrounded attribute token (20be986)

1.0.10

diff

  • Handle else if token (#38)
  • Handle interpolation token (#39)
  • Handle blockcode token (#40)
  • Handle mustEscape for interpolated-code token (d8f6446)
  • Add support for unless (ca0b537)
  • Improve formatting of code attribute (47c0355, d6c896c)
  • Apply semicolons correctly (48c58b8)
    This could lead to a slight change
    You can override the semi attribute and set it to false, if semicolons are not wanted

1.0.9

diff

  • Move prettier to devDependencies (#37)
  • Some minor changes like
    • formatting
    • using TS 3.7 optional chaining
    • output test coverage

1.0.8

diff

  • Handle while and case token (#34)

1.0.7

diff

  • Handle mustEscape for buffered code token (#31)
  • Updated README.md

1.0.6

diff

  • Handle each token (#29)

1.0.5

diff

  • Fix handling with interpolated-code token (#27)

1.0.4

diff

  • Handle text-html token (#24)

1.0.3

diff

  • Format Vue v-slot

1.0.2

diff

  • Fix spaces around interpolated tags (#20)

1.0.1

diff

  • Fix indent for include (#18)
  • Fix space after doctype if there is no value

1.0.0

diff

  • No real changes to 1.0.0-alpha.11

1.0.0-alpha.11

diff

  • Fix preceding div if there is a normal class attribute
  • Improve interpolation in text and bindings
  • Introduce new option attributeSeparator (#13)
    Possible values: always and as-needed

1.0.0-alpha.10

diff

  • Correct quotes for Angular attributes (#10)

1.0.0-alpha.9

diff

  • Support mixins (#11)
  • Fix some issues with indentation
  • Fix buffered attributes

1.0.0-alpha.8

diff

  • Suppress log output in production

1.0.0-alpha.7

diff

  • Support for wrap after printWidth (#8)
    Currently not very accurate, but works

1.0.0-alpha.6

diff

  • Fix indentation for comments
  • Improve indentation for tags
  • Support singleQuote option
  • Setup eslint with TypeScript configuration (#4)
  • Setup Azure Pipelines

1.0.0-alpha.5

diff

  • Fix indentation of piped text

1.0.0-alpha.4

diff

  • Fix indentation after outdent

1.0.0-alpha.3

diff

  • Improvement of indents, empty lines and spaces

  • Keep indent for code if wanted

    //- Input
    block page_meta
      - var page_title = "Markdown"
      - var page_description = "This is a page description."
    
    //- Output (1.0.0-alpha.2)
    block page_meta
    - var page_title = "Markdown"
    - var page_description = "This is a page description."
    
    //- Output (1.0.0-alpha.3)
    block page_meta
      - var page_title = "Markdown"
      - var page_description = "This is a page description."

1.0.0-alpha.2

diff

  • Correct some unnecessary blank lines

  • Correct a missing whitespace after interpolated-code token

  • Handle new Tokens

  • Improve space formatting

    Use recommended solutions link

    //- Input
    v-btn(color="primary", name="save", @click="save", :disabled="saving", :loading="saving")
      v-icon save
      |  {{ $t('mylangkeys.crud.save') }}
    
    //- Output (1.0.0-alpha.1)
    v-btn(color="primary", name="save", @click="save", :disabled="saving", :loading="saving")
      v-icon save
      | {{ $t('mylangkeys.crud.save') }}
    
    //- Output (1.0.0-alpha.2)
    v-btn(color="primary", name="save", @click="save", :disabled="saving", :loading="saving")
      v-icon save
      |
      | {{ $t('mylangkeys.crud.save') }}

1.0.0-alpha.1

diff

  • Initial alpha release