rehype-remove-duplicate-attribute-values
Advanced tools
Comparing version 2.0.2 to 3.0.0
61
index.js
@@ -8,38 +8,39 @@ /** | ||
'use strict' | ||
/** | ||
* @typedef {import('hast').Root} Root | ||
*/ | ||
var uniq = require('uniq') | ||
var array = require('x-is-array') | ||
var visit = require('unist-util-visit') | ||
var is = require('hast-util-is-element') | ||
var attributes = require('./schema') | ||
import {visit} from 'unist-util-visit' | ||
import {isElement} from 'hast-util-is-element' | ||
import {schema} from './schema.js' | ||
module.exports = empty | ||
const own = {}.hasOwnProperty | ||
var own = {}.hasOwnProperty | ||
/** | ||
* Remove duplicates in attribute values with unique tokens. | ||
* | ||
* @type {import('unified').Plugin<[], Root>} | ||
*/ | ||
export default function rehypeRemoveDuplicateAttributeValues() { | ||
return (tree) => { | ||
visit(tree, 'element', (node) => { | ||
const props = node.properties || {} | ||
/** @type {string} */ | ||
let prop | ||
function empty() { | ||
return transform | ||
} | ||
for (prop in props) { | ||
if (own.call(props, prop)) { | ||
const value = props[prop] | ||
function transform(tree) { | ||
visit(tree, 'element', visitor) | ||
} | ||
function visitor(node) { | ||
var props = node.properties | ||
var prop | ||
var value | ||
for (prop in props) { | ||
value = props[prop] | ||
if ( | ||
own.call(attributes, prop) && | ||
is(node, attributes[prop]) && | ||
array(value) | ||
) { | ||
uniq(value) | ||
} | ||
if ( | ||
own.call(schema, prop) && | ||
isElement(node, schema[prop]) && | ||
Array.isArray(value) | ||
) { | ||
props[prop] = [...new Set(value)] | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} |
{ | ||
"name": "rehype-remove-duplicate-attribute-values", | ||
"version": "2.0.2", | ||
"version": "3.0.0", | ||
"description": "rehype plugin to remove duplicates in attribute values with unique tokens", | ||
@@ -29,13 +29,29 @@ "license": "MIT", | ||
], | ||
"sideEffects": false, | ||
"type": "module", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"index.d.ts", | ||
"index.js", | ||
"schema.json" | ||
"schema.d.ts", | ||
"schema.js" | ||
], | ||
"dependencies": { | ||
"hast-util-is-element": "^1.0.0", | ||
"uniq": "^1.0.1", | ||
"unist-util-visit": "^2.0.0", | ||
"x-is-array": "^0.1.0" | ||
"@types/hast": "^2.0.0", | ||
"hast-util-is-element": "^2.0.0", | ||
"unified": "^10.0.0", | ||
"unist-util-visit": "^4.0.0" | ||
}, | ||
"xo": false | ||
"scripts": { | ||
"build": "rimraf \"*.d.ts\" && tsc && type-coverage", | ||
"test": "node --conditions development test.js" | ||
}, | ||
"xo": false, | ||
"typeCoverage": { | ||
"atLeast": 100, | ||
"detail": true, | ||
"strict": true, | ||
"ignoreCatch": true | ||
} | ||
} |
@@ -17,2 +17,5 @@ <!--This file is generated by `build-packages.js`--> | ||
This package is [ESM only][esm]: | ||
Node 12+ is needed to use it and it must be `imported`ed instead of `required`d. | ||
[npm][]: | ||
@@ -24,2 +27,31 @@ | ||
This package exports no identifiers. | ||
The default export is `rehypeRemoveDuplicateAttributeValues` | ||
## Use | ||
On the API: | ||
```diff | ||
import {unified} from 'unified' | ||
import rehypeParse from 'rehype-parse' | ||
+import rehypeRemoveDuplicateAttributeValues from 'rehype-remove-duplicate-attribute-values' | ||
import rehypeStringify from 'rehype-stringify' | ||
unified() | ||
.use(rehypeParse) | ||
+ .use(rehypeRemoveDuplicateAttributeValues) | ||
.use(rehypeStringify) | ||
.process('<span>some html</span>', function (err, file) { | ||
console.error(report(err || file)) | ||
console.log(String(file)) | ||
}) | ||
``` | ||
On the CLI: | ||
```sh | ||
rehype input.html --use remove-duplicate-attribute-values --output output.html | ||
``` | ||
## Example | ||
@@ -53,5 +85,5 @@ | ||
[build-badge]: https://img.shields.io/travis/rehypejs/rehype-minify.svg | ||
[build-badge]: https://github.com/rehypejs/rehype-minify/workflows/main/badge.svg | ||
[build]: https://travis-ci.org/rehypejs/rehype-minify | ||
[build]: https://github.com/rehypejs/rehype-minify/actions | ||
@@ -76,6 +108,8 @@ [coverage-badge]: https://img.shields.io/codecov/c/github/rehypejs/rehype-minify.svg | ||
[chat-badge]: https://img.shields.io/badge/chat-spectrum-7b16ff.svg | ||
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg | ||
[chat]: https://spectrum.chat/unified/rehype | ||
[chat]: https://github.com/rehypejs/rehype/discussions | ||
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c | ||
[npm]: https://docs.npmjs.com/cli/install | ||
@@ -82,0 +116,0 @@ |
6343
6
64
124
Yes
+ Added@types/hast@^2.0.0
+ Addedunified@^10.0.0
+ Added@types/hast@2.3.10(transitive)
+ Addedbail@2.0.2(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedhast-util-is-element@2.1.3(transitive)
+ Addedis-buffer@2.0.5(transitive)
+ Addedis-plain-obj@4.1.0(transitive)
+ Addedtrough@2.2.0(transitive)
+ Addedunified@10.1.2(transitive)
+ Addedunist-util-is@5.2.1(transitive)
+ Addedunist-util-stringify-position@3.0.3(transitive)
+ Addedunist-util-visit@4.1.2(transitive)
+ Addedunist-util-visit-parents@5.1.3(transitive)
+ Addedvfile@5.3.7(transitive)
+ Addedvfile-message@3.1.4(transitive)
- Removeduniq@^1.0.1
- Removedx-is-array@^0.1.0
- Removedhast-util-is-element@1.1.0(transitive)
- Removeduniq@1.0.1(transitive)
- Removedunist-util-is@4.1.0(transitive)
- Removedunist-util-visit@2.0.3(transitive)
- Removedunist-util-visit-parents@3.1.1(transitive)
- Removedx-is-array@0.1.0(transitive)
Updatedhast-util-is-element@^2.0.0
Updatedunist-util-visit@^4.0.0