postsvg
TypeScript icon, indicating that this package has built-in type declarations

2.2.7 • Public • Published

PostSVG

A tiny wrapper over posthtml with the same API optimized for working with SVG.

Differences from PostHTML

  • Content is parsed in xml mode.
  • Properly renders SVG self-closing tags (<path />, <line /> etc).
  • Processing result is instance of Tree class which is wrapper around Array and backward compatible with posthtml parser.

Tree

PostSVG tree has several useful methods for work with AST:

const { parse } = require('postsvg');
 
const tree = parse('<svg><path /><path class="qwe" /></svg>');
 
/**
 * `root` getter returns <svg> node
 * @return {Node} 
 */ 
tree.root;
 
/**
 * Find all <path/> nodes
 * @return {Array<Node>} 
 */
tree.select('path'); 
 
/**
 * Select only nodes with class="qwe"
 * @return {Array<Node>} 
 */
tree.select('.qwe');
 
/**
 * Fill each <path/> node with red color 
 */
tree.each('path', node => node.attrs.fill = 'red');

Node has following structure:

Node<{
  tag: string,
  attrs?: Object,
  content?: Array<Node>
}>

LICENSE

MIT

Package Sidebar

Install

npm i postsvg

Weekly Downloads

13,572

Version

2.2.7

License

MIT

Unpacked Size

12.5 kB

Total Files

10

Last publish

Collaborators

  • kisenka