Détail du package

posthtml-transform

JetBrains38.5kMIT1.0.10

PostHTML plugin to add/modify tags and attributes

posthtml, posthtml plugin

readme

posthtml-transform

PostHTML plugin to add/modify tags attributes.

Demo

posthtml([
  transform({ attr: 'fill', value: 'red' })
]);

Input

<svg>
  <path />
</svg>

Output

<svg fill="red">
  <path fill="red" />
</svg>

Install

npm install posthtml-transform

Usage

const { readFileSync } = require('fs');
const posthtml = require('posthtml');
const transform = require('posthtml-transform');

const input = readFileSync('input.html');

posthtml()
  .use(transform(options))
  .process(input)
  .then(({ html }) => {
    console.log(html);
  });

Configuration

Transformer as object

Plugin accepts one or array of transformers. Transformer is an object with following fields:

  • attr - name of tag attribute to modify. If doesn't exists it will be created. Should be used in combination with value.
  • value - attribute value to set. Should be used in combination with attr.
  • tag - tag name to set. Should be used in combination with selector (or without, if you want to rename all tags :).
  • selector (optional, * by default) - CSS selector to find nodes. If not presented all nodes will be selected. Complete list of supported selectors see on posthtml-match-helper documentation. Here is a short list:
    • Tags div.
    • Ids #id.
    • Classes .class, .class1.class2.
    • Attributes [attr=value], [attr!=value] etc.
    • Multiple selectors path, .class, #id. Nested selectors (g path, g > path) are not supported.

Example

transform({ attr: 'fill', value: 'red' }); // add fill="red" to all nodes
transform({ attr: 'fill', value: 'red', selector: 'path' }); // add fill="red" only to paths
transform({ attr: 'stroke', value: 'black', selector: '#logo' }); // add `stroke` attr to node with id="logo"
transform({ selector: 'g', tag: 'symbol' }); // rename all <g> to <symbol>

Transformer as URL query string

It is also possible to pass URL query params string to plugin. It will be parsed and converted to transformer params, e.g:

transform('fill=red'); // => { attr: 'fill', value: 'red' }
transform('fill=red&20path'); // => { attr: 'fill', value: 'red, selector: 'path' }

Parameter value has following syntax: attr_name=attr_value optional_selector. Parameters can be combined, eg fill=red&stroke=black.

Examples:

fill=red
fill=red path
fill=red .class
fill=red #id, black .class
fill=red #id&stroke=black .class

LICENSE

MIT

changelog

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

1.0.10 (2020-04-03)

Note: Version bump only for package posthtml-transform

1.0.9 (2020-04-01)

Note: Version bump only for package posthtml-transform

1.0.8 (2020-04-01)

Note: Version bump only for package posthtml-transform

1.0.7 (2020-01-24)

Note: Version bump only for package posthtml-transform

1.0.6 (2019-02-12)

Bug Fixes

  • allow pass quoted values (33b0de9), closes #50
  • select node value properly (c80eb05)

1.0.5 (2019-02-08)

Note: Version bump only for package posthtml-transform

1.0.4 (2019-02-08)

Note: Version bump only for package posthtml-transform

1.0.3 (2018-12-05)

Note: Version bump only for package posthtml-transform

1.0.2 (2018-10-29)

Note: Version bump only for package posthtml-transform

1.0.1 (2018-10-29)

Bug Fixes

  • refer to license file in readme (e34a289)

1.0.0 (2018-09-26)

Bug Fixes

  • change posthtml-transform behaviour - process root node by default (26a68e7)

BREAKING CHANGES

  • behaviour changed - now it processes default node by default

0.2.0 (2018-07-23)

Features

  • add possibility to convert alpha colors to rgb (dea0807)

0.1.2 (2018-05-11)

Note: Version bump only for package posthtml-transform

0.1.1 (2018-04-28)

Note: Version bump only for package posthtml-transform

0.1.0 (2018-04-21)

Features

  • add query-string as transformer (f515306)
  • first posthtml-transform implementation (ec2ba64)