remark-slug
Advanced tools
Comparing version 6.1.0 to 7.0.0
47
index.js
@@ -1,29 +0,34 @@ | ||
'use strict' | ||
/** | ||
* @typedef {import('mdast').Root} Root | ||
* @typedef {import('hast').Properties} Properties | ||
*/ | ||
var toString = require('mdast-util-to-string') | ||
var visit = require('unist-util-visit') | ||
var slugs = require('github-slugger')() | ||
import {toString} from 'mdast-util-to-string' | ||
import {visit} from 'unist-util-visit' | ||
import BananaSlug from 'github-slugger' | ||
module.exports = slug | ||
const slugs = new BananaSlug() | ||
function slug() { | ||
return transformer | ||
} | ||
/** | ||
* Plugin to add anchors headings using GitHub’s algorithm. | ||
* | ||
* @type {import('unified').Plugin<void[], Root>} | ||
*/ | ||
export default function remarkSlug() { | ||
return (tree) => { | ||
slugs.reset() | ||
// Patch slugs on heading nodes. | ||
function transformer(ast) { | ||
slugs.reset() | ||
visit(tree, 'heading', (node) => { | ||
const data = node.data || (node.data = {}) | ||
const props = /** @type {Properties} */ ( | ||
data.hProperties || (data.hProperties = {}) | ||
) | ||
let id = props.id | ||
visit(ast, 'heading', visitor) | ||
id = id ? slugs.slug(String(id), true) : slugs.slug(toString(node)) | ||
function visitor(node) { | ||
var data = node.data || (node.data = {}) | ||
var props = data.hProperties || (data.hProperties = {}) | ||
var id = props.id | ||
id = id ? slugs.slug(id, true) : slugs.slug(toString(node)) | ||
data.id = id | ||
props.id = id | ||
data.id = id | ||
props.id = id | ||
}) | ||
} | ||
} |
{ | ||
"name": "remark-slug", | ||
"version": "6.1.0", | ||
"version": "7.0.0", | ||
"description": "remark plugin to add anchors to headings", | ||
@@ -30,35 +30,40 @@ "license": "MIT", | ||
], | ||
"sideEffects": false, | ||
"type": "module", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"index.js", | ||
"types/index.d.ts" | ||
"index.d.ts", | ||
"index.js" | ||
], | ||
"types": "types/index.d.ts", | ||
"dependencies": { | ||
"@types/hast": "^2.3.2", | ||
"@types/mdast": "^3.0.0", | ||
"github-slugger": "^1.0.0", | ||
"mdast-util-to-string": "^1.0.0", | ||
"unist-util-visit": "^2.0.0" | ||
"mdast-util-to-string": "^3.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", | ||
"remark": "^13.0.0", | ||
"remark-cli": "^9.0.0", | ||
"remark": "^14.0.0", | ||
"remark-cli": "^10.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"rimraf": "^3.0.0", | ||
"tape": "^5.0.0", | ||
"tinyify": "^3.0.0", | ||
"unist-builder": "^2.0.0", | ||
"unist-util-remove-position": "^3.0.0", | ||
"xo": "^0.37.0" | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"unist-builder": "^3.0.0", | ||
"unist-util-remove-position": "^4.0.0", | ||
"xo": "^0.43.0" | ||
}, | ||
"scripts": { | ||
"build": "rimraf \"*.d.ts\" && tsc && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"build-bundle": "browserify . -s remarkSlug > remark-slug.js", | ||
"build-mangle": "browserify . -s remarkSlug -p tinyify > remark-slug.min.js", | ||
"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" | ||
}, | ||
@@ -74,8 +79,3 @@ "prettier": { | ||
"xo": { | ||
"prettier": true, | ||
"esnext": false, | ||
"ignores": [ | ||
"remark-slug.js", | ||
"types/index.d.ts" | ||
] | ||
"prettier": true | ||
}, | ||
@@ -86,3 +86,9 @@ "remarkConfig": { | ||
] | ||
}, | ||
"typeCoverage": { | ||
"atLeast": 100, | ||
"detail": true, | ||
"strict": true, | ||
"ignoreCatch": true | ||
} | ||
} |
@@ -26,2 +26,5 @@ # remark-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][]: | ||
@@ -49,19 +52,21 @@ | ||
And our script, `example.js`, looks as follows: | ||
And our module, `example.js`, looks as follows: | ||
```js | ||
var fs = require('fs') | ||
var unified = require('unified') | ||
var markdown = require('remark-parse') | ||
var slug = require('remark-slug') | ||
var remark2rehype = require('remark-rehype') | ||
var html = require('rehype-stringify') | ||
import fs from 'node:fs' | ||
import {unified} from 'unified' | ||
import remarkParse from 'remark-parse' | ||
import remarkSlug from 'remark-slug' | ||
import remarkRehype from 'remark-rehype' | ||
import rehypeStringify from 'rehype-stringify' | ||
const buf = fs.readFileSync('example.md') | ||
unified() | ||
.use(markdown) | ||
.use(slug) | ||
.use(remark2rehype) | ||
.use(html) | ||
.process(fs.readFileSync('example.md'), function(err, file) { | ||
if (err) throw err | ||
.use(remarkParse) | ||
.use(remarkSlug) | ||
.use(remarkRehype) | ||
.use(rehypeStringify) | ||
.process(buf) | ||
.then((file) => { | ||
console.log(String(file)) | ||
@@ -83,4 +88,7 @@ }) | ||
### `remark().use(slug)` | ||
This package exports no identifiers. | ||
The default export is `remarkSlug`. | ||
### `unified().use(remarkSlug)` | ||
Add anchors headings using GitHub’s algorithm. | ||
@@ -87,0 +95,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
9569
38
187
Yes
6
14
+ Added@types/hast@^2.3.2
+ Added@types/mdast@^3.0.0
+ Addedunified@^10.0.0
+ Added@types/hast@2.3.10(transitive)
+ Added@types/mdast@3.0.15(transitive)
+ Addedbail@2.0.2(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedis-buffer@2.0.5(transitive)
+ Addedis-plain-obj@4.1.0(transitive)
+ Addedmdast-util-to-string@3.2.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)
- Removedmdast-util-to-string@1.1.0(transitive)
- Removedunist-util-is@4.1.0(transitive)
- Removedunist-util-visit@2.0.3(transitive)
- Removedunist-util-visit-parents@3.1.1(transitive)
Updatedmdast-util-to-string@^3.0.0
Updatedunist-util-visit@^4.0.0