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
- Host: GitHub
- URL: https://github.com/posthtml/posthtml-rename-attrs
- Owner: posthtml
- License: mit
- Created: 2017-10-17T13:59:32.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2025-02-19T20:39:42.000Z (12 months ago)
- Last Synced: 2025-10-01T12:34:47.583Z (4 months ago)
- Language: JavaScript
- Homepage:
- Size: 193 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
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