npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

xast-util-to-xml

v4.0.0

Published

xast utility to serialize to XML

Downloads

10,942

Readme

xast-util-to-xml

Build Coverage Downloads Size Sponsors Backers Chat

xast utility to serialize as XML.

Contents

What is this?

This package is a utility that turns a xast tree into a string of XML.

When should I use this?

You can use this utility when you want to get the serialized XML that is represented by the syntax tree, either because you’re done with the syntax tree, or because you’re integrating with another tool that does not support syntax trees.

This utility has options to configure how the XML is serialized. These options help when building tools that make output pretty (such as formatters) or ugly (such as minifiers).

The utility xast-util-from-xml does the inverse of this utility. It turns XML into xast.

The utility hast-util-to-html does the same as this utility but for HTML: it turns hast into HTML.

Install

This package is ESM only. In Node.js (version 16+), install with npm:

npm install xast-util-to-xml

In Deno with esm.sh:

import {toXml} from 'https://esm.sh/xast-util-to-xml@4'

In browsers with esm.sh:

<script type="module">
  import {toXml} from 'https://esm.sh/xast-util-to-xml@4?bundle'
</script>

Use

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

const 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 identifier toXml. There is no default export.

toXml(tree[, options])

Serialize a xast tree to XML.

Parameters
  • tree (Array<Node> or Node) — xast node(s) to serialize
  • options (Options, optional) — configuration
Returns

Serialized XML (string).

Options

Configuration (TypeScript type).

Fields
allowDangerousXml

Allow raw nodes and insert them as raw XML (boolean, default: false).

When false, Raw nodes are encoded.

⚠️ Danger: only set this if you completely trust the content.

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.

quote

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

quoteSmart

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

tightClose

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

👉 Note: only used if closeEmptyElements: true.

Quote

XML quotes for attribute values (TypeScript type).

Type
type Quote = '"' | "'"

Raw

Raw (TypeScript type).

Type
import type {Data, Literal} from 'xast'

interface Raw extends Literal {
  type: 'raw'
  data?: RawData | undefined
}

export interface RawData extends Data {}

Types

This package is fully typed with TypeScript. It exports the additional types Options, Quote, and Raw.

It also registers the node type with @types/xast. If you’re working with the syntax tree, make sure to import this utility somewhere in your types, as that registers the new node types in the tree.

/**
 * @typedef {import('xast-util-to-xml')}
 */

import {visit} from 'unist-util-visit'

/** @type {import('xast').Root} */
const tree = getXastNodeSomeHow()

visit(tree, function (node) {
  // `node` can now be a raw node.
})

Compatibility

Projects maintained by the unified collective are compatible with maintained versions of Node.js.

When we cut a new major release, we drop support for unmaintained versions of Node. This means we try to keep the current release line, xast-util-to-xml@^4, compatible with Node.js 16.

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