• Stars
    star
    229
  • Rank 174,666 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 5 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Prettier XML plugin

Prettier for XML

Gitter GitHub Actions NPM Version code style: prettier Follow+Prettier+on+Twitter

@prettier/plugin-xml is a prettier plugin for XML. prettier is an opinionated code formatter that supports multiple languages and integrates with most editors. The idea is to eliminate discussions of style in code review and allow developers to get back to thinking about code design instead.

Getting started

To run prettier with the XML plugin, you're going to need node.

If you're using the npm CLI, then add the plugin by:

npm install --save-dev prettier @prettier/plugin-xml

Or if you're using yarn, then add the plugin by:

yarn add --dev prettier @prettier/plugin-xml

The prettier executable is now installed and ready for use:

./node_modules/.bin/prettier --write '**/*.xml'

Configuration

Below are the options (from src/plugin.js) that @prettier/plugin-xml currently supports:

API Option CLI Option Default Description
bracketSameLine --bracket-same-line true Same as in Prettier (see prettier docs)
printWidth --print-width 80 Same as in Prettier (see prettier docs).
singleAttributePerLine --single-attribute-per-line false Same as in Prettier (see prettier docs)
tabWidth --tab-width 2 Same as in Prettier (see prettier docs).
xmlSelfClosingSpace --xml-self-closing-space true Adds a space before self-closing tags.
xmlWhitespaceSensitivity --xml-whitespace-sensitivity "strict" Options are "strict", "preserve", and "ignore". You may want "ignore" or "preserve", see below.

Any of these can be added to your existing prettier configuration file. For example:

{
  "tabWidth": 4
}

Or, they can be passed to prettier as arguments:

prettier --tab-width 4 --write '**/*.xml'

Whitespace

In XML, by default, all whitespace inside elements has semantic meaning. For prettier to maintain its contract of not changing the semantic meaning of your program, this means the default for xmlWhitespaceSensitivity is "strict". When running in this mode, prettier's ability to rearrange your markup is somewhat limited, as it has to maintain the exact amount of whitespace that you input within elements.

If you're sure that the XML files that you're formatting do not require whitespace sensitivity, you can use the "ignore" option, as this will produce a standardized amount of whitespace. This will fix any indentation issues, and collapse excess blank lines (max of 1 blank line). For most folks most of the time, this is probably the option that you want.

You can also use the "preserve" option, if you want to preserve the whitespace of text nodes within XML elements and attributes. See #478 for more detail.

Ignore ranges

You can use two special comments to get prettier to ignore formatting a specific piece of the document, as in the following example:

<foo>
  <!-- prettier-ignore-start -->
    <this-content-will-not-be-formatted     />
  <!-- prettier-ignore-end -->
</foo>

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/prettier/plugin-xml.

License

The package is available as open source under the terms of the MIT License.

More Repositories

1

prettier

Prettier is an opinionated code formatter.
JavaScript
49,325
star
2

eslint-config-prettier

Turns off all rules that are unnecessary or might conflict with Prettier.
JavaScript
5,386
star
3

prettier-vscode

Visual Studio Code extension for Prettier
TypeScript
5,130
star
4

prettier-eslint

Code ➑️ prettier ➑️ eslint --fix ➑️ Formatted Code ✨
JavaScript
3,994
star
5

eslint-plugin-prettier

ESLint plugin for Prettier formatting
JavaScript
3,283
star
6

pretty-quick

⚑ Get Pretty Quick
TypeScript
2,211
star
7

vim-prettier

A Vim plugin for Prettier
Vim Script
1,780
star
8

plugin-php

Prettier PHP Plugin
PHP
1,736
star
9

plugin-ruby

Prettier Ruby Plugin
JavaScript
1,462
star
10

tslint-config-prettier

Use TSLint with Prettier without any conflict
TypeScript
1,234
star
11

prettier-atom

An atom package for the prettier formatter.
JavaScript
755
star
12

prettier-eslint-cli

CLI for prettier-eslint
JavaScript
541
star
13

plugin-python

Prettier Python Plugin
JavaScript
517
star
14

prettier-emacs

Minor mode to format JS code on file save
Emacs Lisp
374
star
15

stylelint-config-prettier

Turns off all rules that are unnecessary or might conflict with prettier.
JavaScript
374
star
16

stylelint-prettier

Stylelint plugin for Prettier formatting
JavaScript
346
star
17

plugin-swift

[DEPRECATED] Prettier Swift Plugin - WARNING: The AST parser is not stable yet
JavaScript
274
star
18

tslint-plugin-prettier

Runs Prettier as a TSLint rule and reports differences as individual TSLint issues
TypeScript
235
star
19

prettier-browser-extension

Prettier Browser Extension
JavaScript
200
star
20

plugin-pug

Prettier Pug Plugin
TypeScript
197
star
21

plugin-lua

Prettier Lua Plugin (WIP)
Lua
78
star
22

prettier-logo

The Prettier logo.
JavaScript
39
star
23

prettier-cli

TypeScript
28
star
24

yaml-unist-parser

A YAML parser that produces output compatible with unist
TypeScript
25
star
25

prettier-synchronized

Synchronous version of Prettier
JavaScript
24
star
26

stylelint-config-prettier-scss

Turns off all CSS and SCSS rules that are unnecessary or might conflict with prettier.
JavaScript
24
star
27

prettier-printer

Library for building and pretty printing text documents
JavaScript
23
star
28

prettier-linter-helpers

Helper functions for exposing prettier changes within linting tools
JavaScript
18
star
29

angular-estree-parser

A parser that converts Angular source code into an ESTree-compatible form
TypeScript
16
star
30

prettier-rpc

Single-file build of prettier with JSON-RPC communication
JavaScript
13
star
31

prettier-regression-testing

Automates the prettier/prettier regression checks with GitHub Actions.
TypeScript
12
star
32

parse-srcset

A spec-conformant JavaScript parser for the HTML5 srcset attribute
JavaScript
9
star
33

plugin-haml

Plugin for the HAML template language
JavaScript
5
star
34

pre-commit

Mirror of Prettier package for pre-commit.
JavaScript
5
star
35

core-test-fixtures

Test fixtures for Prettier core.
JavaScript
2
star
36

is-es5-identifier-name

Check if provided string is an `IdentifierName` as specified in ECMA262 edition 5.1 section 7.6.
JavaScript
2
star
37

html-ua-styles

User agent stylesheet defined in the WHATWG HTML specification.
JavaScript
1
star
38

eslint-plugin-prettier-internal-rules

Wrapper for Prettier internal rules
JavaScript
1
star