An open API service indexing awesome lists of open source software.

https://github.com/posthtml/posthtml-rename-attrs

Programmatically rename HTML attributes
https://github.com/posthtml/posthtml-rename-attrs

Last synced: 3 days ago
JSON representation

Programmatically rename HTML attributes

Awesome Lists containing this project

README

          


PostHTML

posthtml-rename-attrs


Programmatically rename HTML attributes

[![Version][npm-version-shield]][npm]
[![Build][github-ci-shield]][github-ci]
[![License][license-shield]][license]
[![Downloads][npm-stats-shield]][npm-stats]

## About

This [PostHTML](https://github.com/posthtml/posthtml) plugin allows you to programmatically rename HTML attributes through a custom function.

## Installation

```sh
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:

```js
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('')
.then(function(result) {
console.log(result);
});
```

Result:

```html

```

[npm]: https://www.npmjs.com/package/posthtml-rename-attrs
[npm-version-shield]: https://img.shields.io/npm/v/posthtml-rename-attrs.svg
[npm-stats]: http://npm-stat.com/charts.html?package=posthtml-rename-attrs
[npm-stats-shield]: https://img.shields.io/npm/dt/posthtml-rename-attrs.svg
[github-ci]: https://github.com/posthtml/posthtml-rename-attrs/actions/workflows/nodejs.yml
[github-ci-shield]: https://github.com/posthtml/posthtml-rename-attrs/actions/workflows/nodejs.yml/badge.svg
[license]: ./LICENSE
[license-shield]: https://img.shields.io/npm/l/posthtml-rename-attrs.svg