Skip to content

Latest commit

 

History

History
55 lines (41 loc) · 1.63 KB

README.md

File metadata and controls

55 lines (41 loc) · 1.63 KB
PostHTML

posthtml-rename-attrs

Programmatically rename HTML attributes

Version Build License Downloads

About

This PostHTML plugin allows you to programmatically rename HTML attributes through a custom function.

Installation

npm i -D posthtml posthtml-rename-attrs

Usage

You simply define a function that returns the renamed attribute.

For example, let's rename all src attributes:

const posthtml = require('posthtml');
const renameAttrs = require('posthtml-rename-attrs');

// If the attribute is 'class', rename it
const prefix = (v) => v === 'src' ? `data-${v}` : v;

posthtml([renameAtrs(prefix)])
  .process('<img src="...">')
  .then(function(result) {
    console.log(result);
  });

Result:

<img data-src="...">