Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

utility to serialize to XML

License

NotificationsYou must be signed in to change notification settings

syntax-tree/xast-util-to-xml

BuildCoverageDownloadsSizeSponsorsBackersChat

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 isrepresented 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 syntaxtrees.

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

The utilityxast-util-from-xml does the inverse of thisutility.It turns XML into xast.

The utilityhast-util-to-html does the same as thisutility but for HTML: it turnshast into HTML.

Install

This package isESM only.In Node.js (version 16+), install withnpm:

npm install xast-util-to-xml

In Deno withesm.sh:

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

In browsers withesm.sh:

<scripttype="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'consttree=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"?><ncxxmlns="http://www.daisy.org/z3986/2005/ncx/"version="2005-1">  <head>    <metaname="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 identifiertoXml.There is no default export.

toXml(tree[, options])

Serialize a xast tree to XML.

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

Serialized XML (string).

Options

Configuration (TypeScript type).

Fields
allowDangerousXml

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

Whenfalse,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 insteadof an end tag:<circle /> instead of<circle></circle> (boolean, default:false).

SeetightClose 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 ifcloseEmptyElements: true.

Quote

XML quotes for attribute values (TypeScript type).

Type
typeQuote='"'|"'"

Raw

Raw (TypeScript type).

Type
importtype{Data,Literal}from'xast'interfaceRawextendsLiteral{type:'raw'data?:RawData|undefined}exportinterfaceRawDataextendsData{}

Types

This package is fully typed withTypeScript.It exports the additional typesOptions,Quote,andRaw.

It also registers the node type with@types/xast.If you’re working with the syntax tree, make sure to import this utilitysomewhere 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} */consttree=getXastNodeSomeHow()visit(tree,function(node){// `node` can now be a raw node.})

Compatibility

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

When we cut a new major release, we drop support for unmaintained versions ofNode.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

Seecontributing.md insyntax-tree/.github forways to get started.Seesupport.md for ways to get help.

This project has acode of conduct.By interacting with this repository, organization, or community you agree toabide by its terms.

License

MIT ©Titus Wormer


[8]ページ先頭

©2009-2025 Movatter.jp