💡 Use the original prettier-setup instead, which comes with lint-staged and is regularly updated by @lipis. ✨
Based on prettier-setup by the amazing @lipis. 🙏
This project sets up formatting tools to keep your code clean and consistent, so you never have to argue about style again:
- Prettier is an opinionated code formatter that enforces a consistent style by parsing your code and re-printing it with its own rules.
- Husky uses Git hooks to ensure that Prettier runs on all staged changes to format files before committing.
The setup described here will format DITA XML, JSON, Sass, Markdown, and YAML files, but you can adjust the settings to your own needs.
This step adds Prettier and the XML plugin to your project's dependencies so they're available locally regardless of the system configuration.
yarn add prettier @prettier/plugin-xml --dev --exact
Install with npm
npm install prettier @prettier/plugin-xml --save-dev --save-exact
Open the package.json
file in your project and add the following scripts
* entries (or copy them from here):
"scripts": {
"fmt": "yarn format",
"format": "yarn prettier --write",
"prettier": "prettier \"**/*.{dita,json,md,scss,yaml,yml}\"",
"test": "yarn prettier --list-different"
}
* If you are using npm
, replace yarn
with npm run
in the above section.
To call the formatting script manually, run yarn format
. (Run it here to format the files in the test/
folder.)
If you're one of those impatient types that likes to save keystrokes, you can use the yarn fmt
alias instead.
But the whole point of this setup is that you shouldn't have to do this sort of thing manually, so read on to automate the process and ensure that any lines you change will be formatted before you commit your changes.
Set up Husky and the precise-commits
commit hooks to format changed lines before each commit.
yarn add husky precise-commits --dev --exact
Install with npm
npm install husky precise-commits --save-dev --save-exact
Add the precise-commits
and husky
rules to the package.json
file in your project:
"precise-commits": {
"*.{dita*,json,md,scss,xml,yaml,yml}": ["prettier --write"]
},
"husky": {
"hooks": {
"pre-commit": "precise-commits"
}
},
✨ And now you're good to go.
Whenever you commit DITA XML, JSON, Sass, Markdown, or YAML files, your changes — and only your changes — will be Precisely Prettier. ✨
Prettier rules
This project defines the following settings in the .prettierrc.json
file. You can adjust these values according to your own preferences.
Rule | Value* |
---|---|
arrowParens |
avoid |
bracketSpacing |
false |
endOfLine |
lf |
htmlWhitespaceSensitivity |
css |
jsxBracketSameLine |
false |
printWidth |
80 |
proseWrap |
preserve |
requirePragma |
false |
semi |
true |
singleQuote |
true |
tabWidth |
2 |
trailingComma |
all |
useTabs |
false |
vueIndentScriptAndStyle |
true |
xmlSelfClosingSpace |
false |
xmlWhitespaceSensitivity |
strict |
* Values in bold differ from the Prettier defaults.