xml-but-prettier

raw JSON →
1.0.1 verified Sat Apr 25 auth: no javascript maintenance

A minimal XML beautifier that formats XML documents by placing each tag and text node on its own line with proper indentation. The current stable version is 1.0.1, released in 2017 with no further updates since. It is a fork of xml-beautifier and offers a simple function signature with two options: indentor (custom indentation string) and textNodesOnSameLine (to compress text nodes onto the same line as their parent tags). It supports ESM imports and has no runtime dependencies.

error TypeError: xmlButPrettier is not a function
cause Using named import instead of default import.
fix
Use import xmlButPrettier from 'xml-but-prettier'
error Error: Expected a string, got object
cause Passing options as separate arguments instead of config object in v1.x. Options must be an object.
fix
Call xmlButPrettier(xml, { indentor: ' ', textNodesOnSameLine: true })
error Cannot find module 'xml-but-prettier'
cause Package not installed or not in node_modules.
fix
Run npm install xml-but-prettier
breaking Options moved from second argument to config object in v1.0.0. Previously, options were passed as separate arguments, e.g. xmlButPrettier(xml, ' ', true). Now they must be passed as an object: xmlButPrettier(xml, { indentor: ' ', textNodesOnSameLine: true }).
fix Update call to use config object: xmlButPrettier(xml, { indentor: ' ', textNodesOnSameLine: true }).
breaking Node.js 0.12 support dropped in v1.0.0.
fix Upgrade Node.js to version 4 or higher.
deprecated No releases since 2017. The package is in maintenance mode and may not receive updates.
fix Consider using an actively maintained alternative like 'xml-formatter' or 'pretty-data'.
npm install xml-but-prettier
yarn add xml-but-prettier
pnpm add xml-but-prettier

Formats an XML string with 2-space indentation and keeps text nodes on the same line as their parent tags.

import xmlButPrettier from 'xml-but-prettier';

const ugly = '<div><span>foo</span><p>bar</p></div>';
const pretty = xmlButPrettier(ugly, {
  indentor: '  ',
  textNodesOnSameLine: true
});
console.log(pretty);
// Output:
// <div>
//   <span>foo</span>
//   <p>bar</p>
// </div>