|
| 1 | +<div align="center"> |
| 2 | + <img width="150" height="150" title="PostHTML" src="https://posthtml.github.io/posthtml/logo.svg"> |
| 3 | + <h1>Extra Attributes</h1> |
| 4 | + <p>Add new attributes to elements in your HTML</p> |
| 5 | + |
| 6 | + [![Version][npm-version-shield]][npm] |
| 7 | + [![License][license-shield]][license] |
| 8 | + [![Build][travis-ci-shield]][travis-ci] |
| 9 | + [![Downloads][npm-stats-shield]][npm-stats] |
| 10 | +</div> |
| 11 | + |
| 12 | +## Introduction |
| 13 | + |
| 14 | +This PostHTML plugin can add extra attributes to elements in your HTML: |
| 15 | + |
| 16 | +- does not overwrite existing attributes (configurable) |
| 17 | +- appends class names to existing ones |
| 18 | +- supports a variety of CSS-like selectors |
| 19 | + |
| 20 | +## Installation |
| 21 | + |
| 22 | +``` |
| 23 | +$ npm i posthtml posthtml-extra-attributes |
| 24 | +``` |
| 25 | + |
| 26 | +## Usage |
| 27 | + |
| 28 | +```js |
| 29 | +const posthtml = require('posthtml') |
| 30 | +const addAttributes = require('posthtml-extra-attributes') |
| 31 | + |
| 32 | +posthtml([ |
| 33 | + addAttributes({ |
| 34 | + attributes: { |
| 35 | + div: { |
| 36 | + class: 'new', |
| 37 | + id: 'new' |
| 38 | + } |
| 39 | + } |
| 40 | + }) |
| 41 | + ]) |
| 42 | + .process('<div class="test">Test</div>') |
| 43 | + .then(result => console.log(result.html)) |
| 44 | + |
| 45 | + // <div class="test new" id="new">Test</div> |
| 46 | +``` |
| 47 | + |
| 48 | +## Options |
| 49 | + |
| 50 | +### `attributes` |
| 51 | + |
| 52 | +Type: `object`\ |
| 53 | +Default: `{}` |
| 54 | + |
| 55 | +An object defining which elements should get what attributes. |
| 56 | + |
| 57 | +Elements can be any [posthtml-match-helper](https://github.com/phloe/posthtml-match-helper) selector. |
| 58 | + |
| 59 | +#### Select by tag |
| 60 | + |
| 61 | +Add `id="new"` to all `<div>` tags: |
| 62 | + |
| 63 | +```js |
| 64 | +const attributes = { |
| 65 | + div: { |
| 66 | + id: 'new' |
| 67 | + }, |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +#### Select by class |
| 72 | + |
| 73 | +Add `editable="true"` to all elements with a `test` class: |
| 74 | + |
| 75 | +```js |
| 76 | +const attributes = { |
| 77 | + '.test': { |
| 78 | + 'editable': true |
| 79 | + }, |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +#### Select by id |
| 84 | + |
| 85 | +Add `class="new"` to any element with `id="test"`: |
| 86 | + |
| 87 | +```js |
| 88 | +const attributes = { |
| 89 | + '#test': { |
| 90 | + class: 'new' |
| 91 | + }, |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +If the element already has a `class` attribute, the value will be appended: |
| 96 | + |
| 97 | +```html |
| 98 | +<div id="test" class="test">Test</div> |
| 99 | +``` |
| 100 | + |
| 101 | +... will result in: |
| 102 | + |
| 103 | +```html |
| 104 | +<div id="test" class="test new">Test</div> |
| 105 | +``` |
| 106 | + |
| 107 | +#### Select by attribute |
| 108 | + |
| 109 | +Adds `aria-roledescription="slide"` to all elements with a `role` attribute: |
| 110 | + |
| 111 | +```js |
| 112 | +const attributes = { |
| 113 | + '[role]': { |
| 114 | + 'aria-roledescription': 'slide' |
| 115 | + }, |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +#### Select multiple tags |
| 120 | + |
| 121 | +Add multiple attributes to multiple elements in one go: |
| 122 | + |
| 123 | +```js |
| 124 | +const attributes = { |
| 125 | + 'div, p': { |
| 126 | + class: 'test', |
| 127 | + }, |
| 128 | + 'div[role=alert], section.alert': { |
| 129 | + class: 'alert' |
| 130 | + }, |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +### `overwrite` |
| 135 | + |
| 136 | +Type: `boolean`\ |
| 137 | +Default: `false` |
| 138 | + |
| 139 | +By default, the plugin will not overwrite existing attribute values. |
| 140 | + |
| 141 | +Set this option to `true` to enable attribute value overwriting: |
| 142 | + |
| 143 | +```js |
| 144 | +posthtml([ |
| 145 | + addAttributes({ |
| 146 | + attributes: { |
| 147 | + div: { |
| 148 | + id: 'new' |
| 149 | + } |
| 150 | + }, |
| 151 | + overwrite: true |
| 152 | + }) |
| 153 | + ]) |
| 154 | + .process('<div id="test">Test</div>') |
| 155 | + .then(result => console.log(result.html)) |
| 156 | + |
| 157 | + // <div id="new">Test</div> |
| 158 | +``` |
| 159 | + |
| 160 | +[npm]: https://www.npmjs.com/package/posthtml-extra-attributes |
| 161 | +[npm-version-shield]: https://img.shields.io/npm/v/posthtml-extra-attributes.svg |
| 162 | +[npm-stats]: http://npm-stat.com/charts.html?package=posthtml-extra-attributes |
| 163 | +[npm-stats-shield]: https://img.shields.io/npm/dt/posthtml-extra-attributes.svg |
| 164 | +[travis-ci]: https://travis-ci.org/posthtml/posthtml-extra-attributes/ |
| 165 | +[travis-ci-shield]: https://img.shields.io/travis/posthtml/posthtml-extra-attributes/master.svg |
| 166 | +[license]: ./LICENSE |
| 167 | +[license-shield]: https://img.shields.io/npm/l/posthtml-extra-attributes.svg |
0 commit comments