Opinionated general formatter for your Angular, Vue, Svelte or pure HTML5 templates. Try it on the playground
- Indentation based primary on node-level + tag length, not content.
- Can parse Angular, Vue or HTML5 templates.
- Formats embedded content with prettier with respect to your local settings.
- Doesn't change the behaviour of your attributes and tags.
- Remove all superfluous white-space. There are two additional rules:
- Collapses multiple blank lines into a single blank line.
- Empty lines at the start and end of blocks are removed. (Files always end with a single newline, though.)
- Enforce consistent output of your HTML.
- Follows the same option philosophy as prettier.
Feature | Framework |
---|---|
HTML5 | all |
Self-closing custom elements | vue |
Self-closing none void elements | vue |
Case-sensitive attributes | angular |
Case-sensitive elements | angular |
- gformat-html CLI and API.
- gformat-html-formatter Formatter.
- gformat-html-hast-to-html Stringifier.
- prettyhtml-hastscript Hyperscript compatible DSL for creating virtual HAST trees.
- prettyhtml-sort-attributes Sort attributes alphabetically.
- gformat-html-git-patch Formats your changed files based on Git.
- webparser Optimized HTML parser for formatters
- expression-parser Framework agnostic template expression parser.
- rehype-webparser Adapter between HTML parser and rehype.
- rehype-minify-whitespace Collapse whitespace.
- hast-util-from-parse Transform webparser AST to HAST.
Adding this flag before a tag will preserve from whitespace and/or attribute wrapping.
- Preserve from indentation, whitespace and attribute wrapping
<!--gformat-ignore-->
<div></div>
- Preserve only from whitespace processing. This excludes indentation.
<!--gformat-preserve-whitespace-->
<h1> foo </h1>
- Preserve only from attribute wrapping
<!--gformat-preserve-attribute-wrapping-->
<h1 foo="bar" ...> foo </h1>
# regular
$ npm install gformat-html --global
# when using proxy like sinopia/verdaccio
$ npm install gformat-html --global --registry=https://registry.npmjs.org/
This will process recursively all HTML files in the current directory.
$ gformat-html example.html "./**/*.html"
$ gformat-html --help
We provide a simple package called gformat-html-git-patch which is able to format only changed files. This example use husky to manage git hooks in the package.json
{
"husky": {
"hooks": {
"precommit": "gformat-html-git-patch --staged"
}
}
}
Formats a string and returns a vFile
. The method can throw e.g when a parsing error was produced. The error is from type vfile-message
.
The space width of your indentation level (default: 2)
Use tabs instead spaces for indentation (default: false)
Use different maximum line length (default: 80)
Use prettier for embedded content (default: true)
Use custom prettier settings for embedded content (default: local config)
Use single quote instead double quotes (default: false)
Force to wrap attributes (when it has multiple, default: false)
Sort attributes alphabetically (default: false)
Prettier has landed HTML support some days ago. This is awesome and will help many people to reduce the headache of correct formatting in teams. The reason why I created this formatter is to allow everyone to define their styles rules without imposing any style rules.
- It is very easy to maintain because we have a specification and an ecosystem (and @vfile, @syntax-tree) of plugins.
- It should be able to format any superset of HTML as long it is parseable with minor tweaks. We use a modified version of the Angular 6 template parser. There is no need to maintain multiple parser.
- gformat-html doesn't try to understand all Javascript frameworks in depth even when it means that the user has to update some places manually.
This is a fork from the awesome prettyhtml library created by @starptech.
Big thanks to the creators of the excellent rehype and unified ecosystem.