xast-util-to-xml

xast utility to serialize to XML

Usage no npm install needed!

<script type="module">
  import xastUtilToXml from 'https://cdn.skypack.dev/xast-util-to-xml';
</script>

README

xast-util-to-xml

Build Coverage Downloads Size Sponsors Backers Chat

xast utility to serialize to XML.

Install

This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.

npm:

npm install xast-util-to-xml

Use

import {u} from 'unist-builder'
import {x} from 'xastscript'
import {toXml} from 'xast-util-to-xml'

var tree = u('root', [
  u('instruction', {name: 'xml'}, 'version="1.0" encoding="utf-8"'),
  u('text', '\n'),
  x('ncx', {xmlns: 'http://www.daisy.org/z3986/2005/ncx/', version: '2005-1'}, [
    u('text', '\n  '),
    x('head', [
      u('text', '\n    '),
      x('meta', {name: 'dtb:uid', content: 'urn:isbn:9781234567891'}),
      u('text', '\n  ')
    ]),
    u('text', '\n  '),
    x('docTitle', [x('text', 'A Christmas Carol')]),
    u('text', '\n  '),
    x('docAuthor', [x('text', 'Charles Dickens')]),
    u('text', '\n')
  ])
])

console.log(toXml(tree))

Yields:

<?xml version="1.0" encoding="utf-8"?>
<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">
  <head>
    <meta name="dtb:uid" content="urn:isbn:9781234567891"></meta>
  </head>
  <docTitle><text>A Christmas Carol</text></docTitle>
  <docAuthor><text>Charles Dickens</text></docAuthor>
</ncx>

API

This package exports the following identifiers: toXml. There is no default export.

toXml(tree[, options])

Serialize the given xast tree (or list of nodes).

options.quote

Preferred quote to use ('"' or '\'', default: '"').

options.quoteSmart

Use the other quote if that results in less bytes (boolean, default: false).

options.closeEmptyElements

Close elements without any content with slash (/) on the opening tag instead of an end tag: <circle /> instead of <circle></circle> (boolean, default: false). See tightClose to control whether a space is used before the slash.

options.tightClose

Do not use an extra space when closing self-closing elements: <circle/> instead of <circle /> (boolean, default: false).

options.allowDangerousXml

Allow raw nodes and insert them as raw XML. When falsey, encodes raw nodes (boolean, default: false). Note: Only set this if you completely trust the content.

Security

XML can be a dangerous language: don’t trust user-provided data.

Related

Contribute

See contributing.md in syntax-tree/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MIT © Titus Wormer