rehype-slug
Advanced tools
Comparing version 4.0.1 to 5.0.0
41
index.js
@@ -1,23 +0,28 @@ | ||
'use strict' | ||
/** | ||
* @typedef {import('hast').Root} Root | ||
*/ | ||
var slugs = require('github-slugger')() | ||
var has = require('hast-util-has-property') | ||
var rank = require('hast-util-heading-rank') | ||
var toString = require('hast-util-to-string') | ||
var visit = require('unist-util-visit') | ||
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' | ||
module.exports = slug | ||
const slugs = new Slugger() | ||
function slug() { | ||
return transformer | ||
} | ||
/** | ||
* Plugin to add `id`s to headings. | ||
* | ||
* @type {import('unified').Plugin<[], Root>} | ||
*/ | ||
export default function rehypeSlug() { | ||
return (tree) => { | ||
slugs.reset() | ||
function transformer(tree) { | ||
slugs.reset() | ||
visit(tree, 'element', function (node) { | ||
if (rank(node) && !has(node, 'id')) { | ||
node.properties.id = slugs.slug(toString(node)) | ||
} | ||
}) | ||
visit(tree, 'element', (node) => { | ||
if (headingRank(node) && node.properties && !hasProperty(node, 'id')) { | ||
node.properties.id = slugs.slug(toString(node)) | ||
} | ||
}) | ||
} | ||
} |
{ | ||
"name": "rehype-slug", | ||
"version": "4.0.1", | ||
"version": "5.0.0", | ||
"description": "rehype plugin to add `id` attributes to headings", | ||
@@ -26,43 +26,40 @@ "license": "MIT", | ||
], | ||
"sideEffects": false, | ||
"type": "module", | ||
"types": "index.d.ts", | ||
"main": "index.js", | ||
"files": [ | ||
"index.js", | ||
"types/index.d.ts" | ||
"index.d.ts", | ||
"index.js" | ||
], | ||
"types": "types/index.d.ts", | ||
"dependencies": { | ||
"@types/hast": "^2.0.0", | ||
"github-slugger": "^1.1.1", | ||
"hast-util-has-property": "^1.0.0", | ||
"hast-util-heading-rank": "^1.0.0", | ||
"hast-util-to-string": "^1.0.0", | ||
"unist-util-visit": "^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" | ||
}, | ||
"devDependencies": { | ||
"browserify": "^17.0.0", | ||
"dtslint": "^4.0.0", | ||
"nyc": "^15.0.0", | ||
"@types/github-slugger": "^1.3.0", | ||
"@types/tape": "^4.0.0", | ||
"c8": "^7.0.0", | ||
"prettier": "^2.0.0", | ||
"rehype": "^11.0.0", | ||
"rehype": "^12.0.0", | ||
"remark-cli": "^9.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"rimraf": "^3.0.0", | ||
"tape": "^5.0.0", | ||
"tinyify": "^3.0.0", | ||
"unified": "^9.0.0", | ||
"xo": "^0.34.0" | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.42.0" | ||
}, | ||
"scripts": { | ||
"build": "rimraf \"*.d.ts\" && tsc && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"build-bundle": "browserify index.js -s rehypeSlug -o rehype-slug.js", | ||
"build-mangle": "browserify index.js -s rehypeSlug -o rehype-slug.min.js -p tinyify", | ||
"build": "npm run build-bundle && npm run build-mangle", | ||
"test-api": "node test", | ||
"test-coverage": "nyc --reporter lcov tape test.js", | ||
"test-types": "dtslint types", | ||
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types" | ||
"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" | ||
}, | ||
"nyc": { | ||
"check-coverage": true, | ||
"lines": 100, | ||
"functions": 100, | ||
"branches": 100 | ||
}, | ||
"prettier": { | ||
@@ -77,7 +74,3 @@ "tabWidth": 2, | ||
"xo": { | ||
"prettier": true, | ||
"esnext": false, | ||
"ignores": [ | ||
"rehype-slug.js" | ||
] | ||
"prettier": true | ||
}, | ||
@@ -88,3 +81,9 @@ "remarkConfig": { | ||
] | ||
}, | ||
"typeCoverage": { | ||
"atLeast": 100, | ||
"detail": true, | ||
"strict": true, | ||
"ignoreCatch": true | ||
} | ||
} |
@@ -15,2 +15,5 @@ # rehype-slug | ||
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): | ||
Node 12+ is needed to use it and it must be `import`ed instead of `require`d. | ||
[npm][]: | ||
@@ -37,11 +40,13 @@ | ||
```js | ||
var fs = require('fs') | ||
var rehype = require('rehype') | ||
var slug = require('rehype-slug') | ||
import fs from 'node:fs' | ||
import {rehype} from 'rehype' | ||
import slug from 'rehype-slug' | ||
const buf = fs.readFileSync('fragment.html') | ||
rehype() | ||
.data('settings', {fragment: true}) | ||
.use(slug) | ||
.process(fs.readFileSync('fragment.html'), function(err, file) { | ||
if (err) throw err | ||
.process(buf) | ||
.then((file) => { | ||
console.log(String(file)) | ||
@@ -63,4 +68,7 @@ }) | ||
### `rehype().use(slug)` | ||
This package exports no identifiers. | ||
The default export is `rehypeSlug`. | ||
### `unified().use(rehypeSlug)` | ||
Add `id` properties to h1-h6 headings that don’t already have one. | ||
@@ -67,0 +75,0 @@ |
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
8116
33
153
Yes
7
12
+ 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-has-property@2.0.1(transitive)
+ Addedhast-util-heading-rank@2.1.1(transitive)
+ Addedhast-util-to-string@2.0.0(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)
- Removedhast-util-has-property@1.0.4(transitive)
- Removedhast-util-heading-rank@1.0.1(transitive)
- Removedhast-util-to-string@1.0.4(transitive)
- Removedunist-util-is@4.1.0(transitive)
- Removedunist-util-visit@2.0.3(transitive)
- Removedunist-util-visit-parents@3.1.1(transitive)
Updatedhast-util-to-string@^2.0.0
Updatedunist-util-visit@^4.0.0