rehype-slug
Advanced tools
Comparing version 5.1.0 to 6.0.0
@@ -1,20 +0,2 @@ | ||
/** | ||
* Plugin to add `id`s to headings. | ||
* | ||
* @type {import('unified').Plugin<[Options?]|Array<void>, Root>} | ||
*/ | ||
export default function rehypeSlug( | ||
options?: void | Options | undefined | ||
): | ||
| void | ||
| import('unified').Transformer<import('hast').Root, import('hast').Root> | ||
export type Root = import('hast').Root | ||
/** | ||
* Configuration (optional). | ||
*/ | ||
export type Options = { | ||
/** | ||
* Prefix to add in front of `id`s. | ||
*/ | ||
prefix?: string | undefined | ||
} | ||
export { default } from "./lib/index.js"; | ||
export type Options = import('./lib/index.js').Options; |
36
index.js
/** | ||
* @typedef {import('hast').Root} Root | ||
* @typedef {import('./lib/index.js').Options} Options | ||
*/ | ||
/** | ||
* @typedef Options | ||
* Configuration (optional). | ||
* @property {string} [prefix=''] | ||
* Prefix to add in front of `id`s. | ||
*/ | ||
import Slugger from 'github-slugger' | ||
import {hasProperty} from 'hast-util-has-property' | ||
import {headingRank} from 'hast-util-heading-rank' | ||
import {toString} from 'hast-util-to-string' | ||
import {visit} from 'unist-util-visit' | ||
const slugs = new Slugger() | ||
/** | ||
* Plugin to add `id`s to headings. | ||
* | ||
* @type {import('unified').Plugin<[Options?]|Array<void>, Root>} | ||
*/ | ||
export default function rehypeSlug(options = {}) { | ||
const prefix = options.prefix || '' | ||
return (tree) => { | ||
slugs.reset() | ||
visit(tree, 'element', (node) => { | ||
if (headingRank(node) && node.properties && !hasProperty(node, 'id')) { | ||
node.properties.id = prefix + slugs.slug(toString(node)) | ||
} | ||
}) | ||
} | ||
} | ||
export {default} from './lib/index.js' |
{ | ||
"name": "rehype-slug", | ||
"version": "5.1.0", | ||
"version": "6.0.0", | ||
"description": "rehype plugin to add `id` attributes to headings", | ||
"license": "MIT", | ||
"keywords": [ | ||
"unified", | ||
"heading", | ||
"html", | ||
"id", | ||
"plugin", | ||
"rehype", | ||
"rehype-plugin", | ||
"plugin", | ||
"html", | ||
"heading", | ||
"slug", | ||
"id" | ||
"unified" | ||
], | ||
@@ -28,5 +28,5 @@ "repository": "rehypejs/rehype-slug", | ||
"type": "module", | ||
"types": "index.d.ts", | ||
"main": "index.js", | ||
"exports": "./index.js", | ||
"files": [ | ||
"lib/", | ||
"index.d.ts", | ||
@@ -36,44 +36,38 @@ "index.js" | ||
"dependencies": { | ||
"@types/hast": "^2.0.0", | ||
"@types/hast": "^3.0.0", | ||
"github-slugger": "^2.0.0", | ||
"hast-util-has-property": "^2.0.0", | ||
"hast-util-heading-rank": "^2.0.0", | ||
"hast-util-to-string": "^2.0.0", | ||
"unified": "^10.0.0", | ||
"unist-util-visit": "^4.0.0" | ||
"hast-util-heading-rank": "^3.0.0", | ||
"hast-util-to-string": "^3.0.0", | ||
"unist-util-visit": "^5.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/tape": "^4.0.0", | ||
"c8": "^7.0.0", | ||
"prettier": "^2.0.0", | ||
"rehype": "^12.0.0", | ||
"@types/node": "^20.0.0", | ||
"c8": "^8.0.0", | ||
"prettier": "^3.0.0", | ||
"rehype": "^13.0.0", | ||
"remark-cli": "^11.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"rimraf": "^3.0.0", | ||
"tape": "^5.0.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.52.0" | ||
"typescript": "^5.0.0", | ||
"xo": "^0.56.0" | ||
}, | ||
"scripts": { | ||
"build": "rimraf \"*.d.ts\" && tsc && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"build": "tsc --build --clean && tsc --build && type-coverage", | ||
"format": "remark . --frail --output --quiet && prettier . --log-level warn --write && xo --fix", | ||
"prepack": "npm run build && npm run format", | ||
"test": "npm run build && npm run format && npm run test-coverage", | ||
"test-api": "node --conditions development test.js", | ||
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov npm run test-api", | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
"test-coverage": "c8 --100 --check-coverage --reporter lcov npm run test-api" | ||
}, | ||
"prettier": { | ||
"tabWidth": 2, | ||
"useTabs": false, | ||
"bracketSpacing": false, | ||
"singleQuote": true, | ||
"bracketSpacing": false, | ||
"semi": false, | ||
"trailingComma": "none" | ||
"tabWidth": 2, | ||
"trailingComma": "none", | ||
"useTabs": false | ||
}, | ||
"xo": { | ||
"prettier": true | ||
}, | ||
"remarkConfig": { | ||
"plugins": [ | ||
"preset-wooorm" | ||
"remark-preset-wooorm" | ||
] | ||
@@ -84,5 +78,8 @@ }, | ||
"detail": true, | ||
"strict": true, | ||
"ignoreCatch": true | ||
"ignoreCatch": true, | ||
"strict": true | ||
}, | ||
"xo": { | ||
"prettier": true | ||
} | ||
} |
@@ -21,2 +21,3 @@ # rehype-slug | ||
* [`unified().use(rehypeSlug[, options])`](#unifieduserehypeslug-options) | ||
* [`Options`](#options) | ||
* [Types](#types) | ||
@@ -54,4 +55,4 @@ * [Compatibility](#compatibility) | ||
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c). | ||
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]: | ||
This package is [ESM only][esm]. | ||
In Node.js (version 16+), install with [npm][]: | ||
@@ -65,3 +66,3 @@ ```sh | ||
```js | ||
import rehypeSlug from 'https://esm.sh/rehype-slug@5' | ||
import rehypeSlug from 'https://esm.sh/rehype-slug@6' | ||
``` | ||
@@ -73,3 +74,3 @@ | ||
<script type="module"> | ||
import rehypeSlug from 'https://esm.sh/rehype-slug@5?bundle' | ||
import rehypeSlug from 'https://esm.sh/rehype-slug@6?bundle' | ||
</script> | ||
@@ -90,3 +91,3 @@ ``` | ||
And our module `example.js` looks as follows: | ||
…and our module `example.js` looks as follows: | ||
@@ -106,3 +107,3 @@ ```js | ||
Now, running `node example.js` yields: | ||
…then running `node example.js` yields: | ||
@@ -120,3 +121,3 @@ ```html | ||
This package exports no identifiers. | ||
The default export is `rehypeSlug`. | ||
The default export is [`rehypeSlug`][api-rehype-slug]. | ||
@@ -127,22 +128,35 @@ ### `unified().use(rehypeSlug[, options])` | ||
##### `options` | ||
###### Parameters | ||
Configuration (optional). | ||
* `options` ([`Options`][api-options], optional) | ||
— configuration | ||
###### `options.prefix` | ||
###### Returns | ||
Prefix to add in front of `id`s (`string`, default: `''`). | ||
Transform ([`Transformer`][unified-transformer]). | ||
### `Options` | ||
Configuration (TypeScript type). | ||
###### Fields | ||
* `prefix` (`string`, default: `''`) | ||
— prefix to add in front of `id`s | ||
## Types | ||
This package is fully typed with [TypeScript][]. | ||
It exports the additional type `Options`. | ||
It exports the additional type [`Options`][api-options]. | ||
## Compatibility | ||
Projects maintained by the unified collective are compatible with all maintained | ||
Projects maintained by the unified collective are compatible with maintained | ||
versions of Node.js. | ||
As of now, that is Node.js 12.20+, 14.14+, and 16.0+. | ||
Our projects sometimes work with older versions, but this is not guaranteed. | ||
When we cut a new major release, we drop support for unmaintained versions of | ||
Node. | ||
This means we try to keep the current release line, `rehype-slug@^6`, | ||
compatible with Node.js 16. | ||
This plugin works with `rehype-parse` version 1+, `rehype-stringify` version 1+, | ||
@@ -193,5 +207,5 @@ `rehype` version 1+, and `unified` version 4+. | ||
[size-badge]: https://img.shields.io/bundlephobia/minzip/rehype-slug.svg | ||
[size-badge]: https://img.shields.io/bundlejs/size/rehype-slug | ||
[size]: https://bundlephobia.com/result?p=rehype-slug | ||
[size]: https://bundlejs.com/?q=rehype-slug | ||
@@ -210,2 +224,4 @@ [sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg | ||
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c | ||
[esmsh]: https://esm.sh | ||
@@ -215,7 +231,7 @@ | ||
[contributing]: https://github.com/rehypejs/.github/blob/HEAD/contributing.md | ||
[contributing]: https://github.com/rehypejs/.github/blob/main/contributing.md | ||
[support]: https://github.com/rehypejs/.github/blob/HEAD/support.md | ||
[support]: https://github.com/rehypejs/.github/blob/main/support.md | ||
[coc]: https://github.com/rehypejs/.github/blob/HEAD/code-of-conduct.md | ||
[coc]: https://github.com/rehypejs/.github/blob/main/code-of-conduct.md | ||
@@ -226,2 +242,12 @@ [license]: license | ||
[github-slugger]: https://github.com/Flet/github-slugger | ||
[rehype]: https://github.com/rehypejs/rehype | ||
[rehype-autolink-headings]: https://github.com/rehypejs/rehype-autolink-headings | ||
[rehype-sanitize]: https://github.com/rehypejs/rehype-sanitize | ||
[rehype-sanitize-example]: https://github.com/rehypejs/rehype-sanitize#example-headings-dom-clobbering | ||
[typescript]: https://www.typescriptlang.org | ||
@@ -231,12 +257,8 @@ | ||
[rehype]: https://github.com/rehypejs/rehype | ||
[unified-transformer]: https://github.com/unifiedjs/unified#transformer | ||
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting | ||
[github-slugger]: https://github.com/Flet/github-slugger | ||
[api-options]: #options | ||
[rehype-autolink-headings]: https://github.com/rehypejs/rehype-autolink-headings | ||
[rehype-sanitize]: https://github.com/rehypejs/rehype-sanitize | ||
[rehype-sanitize-example]: https://github.com/rehypejs/rehype-sanitize#example-headings-dom-clobbering | ||
[api-rehype-slug]: #unifieduserehypeslug-options |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11515
5
9
7
67
252
+ Added@types/hast@3.0.4(transitive)
+ Added@types/unist@3.0.3(transitive)
+ Addedhast-util-heading-rank@3.0.0(transitive)
+ Addedhast-util-to-string@3.0.1(transitive)
+ Addedunist-util-is@6.0.0(transitive)
+ Addedunist-util-visit@5.0.0(transitive)
+ Addedunist-util-visit-parents@6.0.1(transitive)
- Removedhast-util-has-property@^2.0.0
- Removedunified@^10.0.0
- Removed@types/hast@2.3.10(transitive)
- Removed@types/unist@2.0.11(transitive)
- Removedbail@2.0.2(transitive)
- Removedextend@3.0.2(transitive)
- Removedhast-util-has-property@2.0.1(transitive)
- Removedhast-util-heading-rank@2.1.1(transitive)
- Removedhast-util-to-string@2.0.0(transitive)
- Removedis-buffer@2.0.5(transitive)
- Removedis-plain-obj@4.1.0(transitive)
- Removedtrough@2.2.0(transitive)
- Removedunified@10.1.2(transitive)
- Removedunist-util-is@5.2.1(transitive)
- Removedunist-util-stringify-position@3.0.3(transitive)
- Removedunist-util-visit@4.1.2(transitive)
- Removedunist-util-visit-parents@5.1.3(transitive)
- Removedvfile@5.3.7(transitive)
- Removedvfile-message@3.1.4(transitive)
Updated@types/hast@^3.0.0
Updatedhast-util-to-string@^3.0.0
Updatedunist-util-visit@^5.0.0