Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

💅 The formatter for the modern webhttps://prettyhtml.netlify.com/

License

NotificationsYou must be signed in to change notification settings

Prettyhtml/prettyhtml

Repository files navigation

Prettyhtml Banner

Build Statuslernanpm version

Opinionated general formatter for your Angular, Vue, Svelte or pure HTML5 templates. Try it on theplayground.

Project status: Unfortunately, This project lacks maintainers. Prettier has landed HTML support in 1.15.0. I recommend it to use it if it matches your requirements. If you want to contribute to this project, feel free to create a PR/Issue.

Features

  • Indentation based primary on node-level + tag length, not content.
  • Can parse Angular, Vue or HTML5 templates.
  • Formats embedded content withprettier 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 optionphilosophy as prettier.

Framework specific features

FeatureFramework
HTML5all
Self-closing custom elementsvue
Self-closing none void elementsvue
Case-sensitive attributesangular
Case-sensitive elementsangular

Packages

Ignore element

Adding this flag before a tag will preserve from whitespace and/or attribute wrapping.

  1. Preserve from indentation, whitespace and attribute wrapping
<!--prettyhtml-ignore--><div></div>
  1. Preserve only from whitespace processing. This excludes indentation.
<!--prettyhtml-preserve-whitespace--><h1> foo</h1>
  1. Preserve only from attribute wrapping
<!--prettyhtml-preserve-attribute-wrapping--><h1foo="bar"...> foo</h1>

Install

# regular$ npm install @starptech/prettyhtml --global# when using proxy like sinopia/verdaccio$ npm install @starptech/prettyhtml --global --registry=https://registry.npmjs.org/

CLI

This will process recursively all HTML files in the current directory.

$ prettyhtml example.html "./**/*.html"

Help

$ prettyhtml --help

Pre-Commit hook integration

We provide a simple package calledprettyhtml-quick which is able to format only changed files. This example usehusky to manage git hooks in the package.json

{"husky": {"hooks": {"precommit":"prettyhtml-quick --staged"    }  }}

API

prettyhtml(doc: string, options?): vFile

Formats a string and returns avFile. The method can throw e.g when a parsing error was produced. The error is from typevfile-message.

options
options.tabWidth

The space width of your indentation level (default: 2)

options.useTabs

Use tabs instead spaces for indentation (default: false)

options.printWidth

Use different maximum line length (default: 80)

options.usePrettier

Use prettier for embedded content (default: true)

options.prettier

Use custom prettier settings for embedded content (default: local config)

options.singleQuote

Use single quote instead double quotes (default: false)

options.wrapAttributes

Force to wrap attributes (when it has multiple, default: false)

options.sortAttributes

Sort attributes alphabetically (default: false)

Editor support

  • VSCode extension (not published yet)
  • Vetur Vue tooling for VS Code

Why

Prettier haslanded 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 still using prettyhtml is clear:

  • It is very easy to maintain because we have aspecification and anecosystem (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.
  • Prettyhtml doesn't try to understand all Javascript frameworks in depth even when it means that the user has to update some places manually.

Acknowledgement

Big thanks to the creators of the excellentrehype andunified ecosystem.


[8]ページ先頭

©2009-2025 Movatter.jp