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

Package detail

@prettier/plugin-xml

prettier1.6mMIT3.4.1TypeScript support: included

prettier plugin for XML

readme

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 --plugin=@prettier/plugin-xml --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).
xmlQuoteAttributes --xml-quote-attributes "preserve" Options are "preserve", "single", and "double"
xmlSelfClosingSpace --xml-self-closing-space true Adds a space before self-closing tags.
xmlSortAttributesByKey --xml-sort-attributes-by-key false Orders XML attributes by key alphabetically while prioritizing xmlns attributes.
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 --plugin=@prettier/plugin-xml --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.

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

Unreleased

[3.4.1] - 2024-03-30

Changed

  • Fix the npm publish.

[3.4.0] - 2024-03-29

Added

  • Export plugin type.

Changed

  • Fix printing around reference nodes.
  • Trim only XML whitespace, not JS whitespace.

3.3.1 - 2024-02-10

Changed

  • Allow elements to be marked as whitespace ignored even when they have reference nodes.

3.3.0 - 2024-02-09

Added

  • Support for formatWithCursor.

Changed

  • Always keep whitespace around in xsl:text tags.

3.2.2 - 2023-10-27

Changed

  • Fixed a bug with the xmlSortAttributesByKey option when xmlns attributes are present.

3.2.1 - 2023-09-11

Added

  • Updated the documentation to reflect that you need to pass the plugin path now.

Changed

  • Error messages thrown by the plugin are now much closer to the error messages thrown by prettier.
  • Fixed a bug where self-closing tags that looked like embeds would throw an error.

3.2.0 - 2023-08-08

Added

  • Respect xml:space="preserve" as an override to the xmlWhitespaceSensitivity option.

3.1.1 - 2023-07-14

Changed

  • Required prettier as a peer dependency instead of a runtime dependency.

3.1.0 - 2023-07-07

Added

  • the xmlSortAttributesByKey: true | false option has been added. See the README.
  • The xmlQuoteAttributes: "preserve" | "single" | "double" option has been added. See the README.

3.0.0 - 2023-07-06

Changed

  • See alpha release notes.

3.0.0-alpha.0 - 2023-06-02

Added

  • The xmlWhitespaceSensitivity: "preserve" option has been added. See the README.

Changed

  • Fixed the behavior of bracketSameLine when the attributes on the parent element broke into multiple lines.
  • BREAKING Require prettier v3.
  • BREAKING Migrate to ESM modules.

2.2.0 - 2022-05-12

Added

  • Better error messages in the case of a syntax error.

2.1.0 - 2022-04-16

Added

  • Support for the singleAttributePerLine option.

2.0.1 - 2022-03-22

Added

  • Better idempotency when printing long strings of text content within elements with ignored whitespace.

2.0.0 - 2022-03-22

Changed

  • Require prettier 2.4.0 for the bracketSameLine option.

1.2.0 - 2021-12-23

Added

  • Support formatting .xsl files.

1.1.0 - 2021-09-26

Added

  • Bring back the xmlSelfClosingSpace option.

1.0.2 - 2021-07-17

Changed

  • Removed duplicated inner comments when xmlWhitespaceSensitivity is set to "strict".

1.0.1 - 2021-07-14

Changed

  • Fix the export to work in non-TypeScript environments.

1.0.0 - 2021-07-14

Added

  • Support for the bracketSameLine option to mirror the core option.

Removed

  • The xmlSelfClosingSpace option is now removed to make it easier to maintain.

0.13.1 - 2021-03-03

Changed

  • Fixed a bug with newlines when there is empty content.

0.13.0 - 2021-01-22

Added

  • Maintain newlines if there are some in the original source.

0.12.0 - 2020-08-31

Added

  • Allow embedded parsers to handle content if element tags contain only text content and the tag name matches the name of an existing parser. For example:
<style type="text/css">
.box {
  height: 100px;
  width: 100px;
}
</style>
  • Additionally support .inx files.

0.11.0 - 2020-08-14

Changed

  • Support for a whole wide variety of file types, as per linguist.

0.10.0 - 2020-07-24

Changed

  • Some better support for indenting mixed content when whitespace is set to ignore.

0.9.0 - 2020-07-21

Added

  • Ignored print ranges using the special <!-- prettier-ignore-start --> and <!-- prettier-ignore-end --> comments. For example, you can now do:
<foo>
  <!-- prettier-ignore-start -->
    < this-content-will-not-be-formatted />
  <!-- prettier-ignore-end -->
</foo>

and it will maintain your formatting.

0.8.0 - 2020-07-03

Added

  • Support .wsdl files.

0.7.2 - 2020-02-12

Changed

  • Bump dependency on @xml-tools/parser to v1.0.2.

0.7.1 - 2020-02-10

Changed

  • Require prettier/doc instead of prettier to load less code in standalone mode.

0.7.0 - 2020-01-29

Added

  • Handle processing instructions inside elements.
  • Properly handle mult-line CData tags.

0.6.0 - 2020-01-27

Added

  • The xmlWhitespaceSensitivity option, with current valid values of "strict" and "ignore". "strict" behavior maintains the current behavior, while "ignore" allows the plugin more freedom in where to place nodes.

0.5.0 - 2020-01-21

Added

  • Support for DOCTYPE nodes.

0.4.0 - 2020-01-19

Added

  • A dependency on the @xml-tools/parser package to handle parsing.
  • We now register as supporting .svg and .xsd files.
  • The xmlSelfClosingSpace option for specifying whether or not to add spaces before self-closing element tags.

0.3.0 - 2019-11-14

Added

  • Support for cdata tags.
  • Support for the locStart and locEnd functions by tracking node metadata in the new parser.
  • Support for comment nodes.
  • Support for <?xml ... ?> and <?xml-model ... ?> tags.

Changed

  • Dropped the dependency on fast-xml-parser in favor of writing our own for better control over comments and node location.

0.2.0 - 2019-11-12

Changed

  • Renamed package to @prettier/plugin-xml.

0.1.0 - 2019-11-12

Added

  • Initial release 🎉