unified

Project: syntax-tree/xast-util-to-xml

Package: xast-util-to-xml@1.0.0

  1. Dependencies: 2·Dependents: 1
  2. xast utility to serialize to XML
  1. util 118
  2. utility 114
  3. unist 110
  4. xast 5
  5. xml 5

xast-util-to-xml

Build Coverage Downloads Size Sponsors Backers Chat

xast utility to serialize to XML.

Install

npm:

npm install xast-util-to-xml

Usage

var u = require('unist-builder')
var x = require('xastscript')
var toXml = require('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

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.

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, organisation, or community you agree to abide by its terms.

License

MIT © Titus Wormer