postsvg

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

MIT 16 个版本
安装
npm install postsvg
yarn add postsvg
pnpm add postsvg
bun add postsvg
README

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

版本列表
2.2.7 2019-02-08
2.2.6 2018-12-05
2.2.5 2018-10-29
2.2.4 2018-10-29
2.2.3 2018-06-13
2.2.2 2018-05-11
2.2.1 2018-04-28
2.2.0 2018-04-21
2.1.2-alpha.0 2018-04-09
2.1.2 2018-04-13
2.1.0 2018-04-09
2.0.0 2017-10-22
0.0.3 2016-01-17
0.0.2 2016-01-15
0.0.1 2016-01-12