mdast-util-heading-range
Advanced tools
Comparing version 2.1.5 to 3.0.0
75
index.js
@@ -1,20 +0,54 @@ | ||
'use strict' | ||
/** | ||
* @typedef {import('unist').Node} Node | ||
* @typedef {import('unist').Parent} Parent | ||
* @typedef {import('mdast').Heading} Heading | ||
* | ||
* @typedef {(value: string, node: Heading) => boolean} TestFunction | ||
* @typedef {string|RegExp|TestFunction} Test | ||
* | ||
* @typedef Options | ||
* @property {Test} test | ||
* @property {boolean} [ignoreFinalDefinitions=false] | ||
* | ||
* @typedef ZoneInfo | ||
* @property {number} start | ||
* @property {number} end | ||
* @property {Parent|null} parent | ||
* | ||
* @callback Handler | ||
* @param {Heading|undefined} start | ||
* @param {Array.<Node>} between | ||
* @param {Node|undefined} end | ||
* @param {ZoneInfo} info | ||
*/ | ||
var toString = require('mdast-util-to-string') | ||
import {toString} from 'mdast-util-to-string' | ||
module.exports = headingRange | ||
var splice = [].splice | ||
// Search `node` with `options` and invoke `callback`. | ||
function headingRange(node, options, callback) { | ||
/** | ||
* Search `node` with `options` and invoke `callback`. | ||
* | ||
* @param {Node} node | ||
* @param {Test|Options} options | ||
* @param {Handler} handler | ||
*/ | ||
// eslint-disable-next-line complexity | ||
export function headingRange(node, options, handler) { | ||
var test = options | ||
var children = node.children | ||
/** @type {Array.<Node>} */ | ||
// @ts-ignore looks like children. | ||
var children = node.children || [] | ||
var index = -1 | ||
/** @type {boolean} */ | ||
var ignoreFinalDefinitions | ||
/** @type {number} */ | ||
var depth | ||
/** @type {number} */ | ||
var start | ||
/** @type {number} */ | ||
var end | ||
/** @type {Array.<Node>} */ | ||
var nodes | ||
/** @type {Array.<Node>} */ | ||
var result | ||
/** @type {Node} */ | ||
var child | ||
@@ -39,3 +73,3 @@ | ||
if (typeof test !== 'function') { | ||
throw new Error( | ||
throw new TypeError( | ||
'Expected `string`, `regexp`, or `function` for `test`, not `' + | ||
@@ -57,3 +91,5 @@ test + | ||
// @ts-ignore looks like a heading. | ||
if (!depth && test(toString(child), child)) { | ||
// @ts-ignore looks like a heading. | ||
depth = child.depth | ||
@@ -78,3 +114,4 @@ start = index | ||
nodes = callback( | ||
nodes = handler( | ||
// @ts-ignore `start` points to a heading. | ||
children[start], | ||
@@ -85,3 +122,3 @@ children.slice(start + 1, end), | ||
parent: node, | ||
start: start, | ||
start, | ||
end: children[end] ? end : null | ||
@@ -101,3 +138,3 @@ } | ||
splice.apply(children, [start, end - start + 1].concat(result)) | ||
children.splice(start, end - start + 1, ...result) | ||
} | ||
@@ -107,7 +144,15 @@ } | ||
// Wrap an expression into an assertion function. | ||
/** | ||
* Wrap an expression into an assertion function. | ||
* @param {RegExp} expression | ||
* @returns {(value: string) => boolean} | ||
*/ | ||
function wrapExpression(expression) { | ||
return assertion | ||
// Assert `value` matches the bound `expression`. | ||
/** | ||
* Assert `value` matches the bound `expression`. | ||
* @param {string} value | ||
* @returns {boolean} | ||
*/ | ||
function assertion(value) { | ||
@@ -114,0 +159,0 @@ return expression.test(value) |
{ | ||
"name": "mdast-util-heading-range", | ||
"version": "2.1.5", | ||
"version": "3.0.0", | ||
"description": "mdast utility to use headings as ranges in mdast", | ||
@@ -27,11 +27,18 @@ "license": "MIT", | ||
], | ||
"sideEffects": false, | ||
"type": "module", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"index.d.ts", | ||
"index.js" | ||
], | ||
"dependencies": { | ||
"mdast-util-to-string": "^1.0.0" | ||
"@types/mdast": "^3.0.0", | ||
"@types/unist": "^2.0.0", | ||
"mdast-util-to-string": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"browserify": "^17.0.0", | ||
"nyc": "^15.0.0", | ||
"@types/tape": "^4.0.0", | ||
"c8": "^7.0.0", | ||
"prettier": "^2.0.0", | ||
@@ -41,21 +48,16 @@ "remark": "^13.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"rimraf": "^3.0.0", | ||
"tape": "^5.0.0", | ||
"tinyify": "^3.0.0", | ||
"xo": "^0.34.0" | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.39.0" | ||
}, | ||
"scripts": { | ||
"format": "remark . -qfo && prettier . --write && xo --fix", | ||
"build-bundle": "browserify . -s mdastUtilHeadingRange > mdast-util-heading-range.js", | ||
"build-mangle": "browserify . -s mdastUtilHeadingRange -p tinyify > mdast-util-heading-range.min.js", | ||
"build": "npm run build-bundle && npm run build-mangle", | ||
"test-api": "node test", | ||
"test-coverage": "nyc --reporter lcov tape test.js", | ||
"test": "npm run format && npm run build && npm run test-coverage" | ||
"prepack": "npm run build && npm run format", | ||
"build": "rimraf \"*.d.ts\" && tsc && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"test-api": "node test.js", | ||
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js", | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
}, | ||
"nyc": { | ||
"check-coverage": true, | ||
"lines": 100, | ||
"functions": 100, | ||
"branches": 100 | ||
}, | ||
"prettier": { | ||
@@ -71,10 +73,6 @@ "tabWidth": 2, | ||
"prettier": true, | ||
"esnext": false, | ||
"rules": { | ||
"complexity": "off", | ||
"unicorn/prefer-type-error": "off" | ||
}, | ||
"ignores": [ | ||
"mdast-util-heading-range.js" | ||
] | ||
"no-var": "off", | ||
"prefer-arrow-callback": "off" | ||
} | ||
}, | ||
@@ -85,3 +83,8 @@ "remarkConfig": { | ||
] | ||
}, | ||
"typeCoverage": { | ||
"atLeast": 100, | ||
"detail": true, | ||
"strict": true | ||
} | ||
} |
@@ -15,2 +15,5 @@ # mdast-util-heading-range | ||
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,5 +40,5 @@ | ||
```js | ||
var vfile = require('to-vfile') | ||
var remark = require('remark') | ||
var heading = require('mdast-util-heading-range') | ||
import toVFile from 'to-vfile' | ||
import remark from 'remark' | ||
import {headingRange} from 'mdast-util-heading-range' | ||
@@ -53,6 +56,6 @@ remark() | ||
function transform(tree) { | ||
heading(tree, 'foo', mutate) | ||
headingRange(tree, 'foo', handler) | ||
} | ||
function mutate(start, nodes, end) { | ||
function handler(start, nodes, end) { | ||
return [ | ||
@@ -79,6 +82,9 @@ start, | ||
### `heading(tree, test|options, onrun)` | ||
This package exports the following identifiers: `headingRange`. | ||
There is no default export. | ||
### `headingRange(tree, test|options, handler)` | ||
Search `tree` ([`Node`][node]) and transform a section without affecting other | ||
parts with `onrun` ([`Function`][onrun]). | ||
parts with `handler` ([`Function`][handler]). | ||
A “section” is a heading that passes `test`, until the next heading of the same | ||
@@ -110,3 +116,3 @@ or lower depth, or the end of the document. | ||
#### `function onrun(start, nodes, end?, scope)` | ||
#### `function handler(start, nodes, end?, scope)` | ||
@@ -139,3 +145,3 @@ Callback invoked when a range is found. | ||
Improper use of `onrun` can open you up to a [cross-site scripting (XSS)][xss] | ||
Improper use of `handler` can open you up to a [cross-site scripting (XSS)][xss] | ||
attack as the value it returns is injected into the syntax tree. | ||
@@ -147,3 +153,3 @@ This can become a problem if the tree is later transformed to [**hast**][hast]. | ||
```js | ||
function onrun(start, nodes, end) { | ||
function handler(start, nodes, end) { | ||
return [start, {type: 'html', value: 'alert(1)'}, end] | ||
@@ -163,3 +169,4 @@ } | ||
Either do not use user input in `onrun` or use [`hast-util-santize`][sanitize]. | ||
Either do not use user input in `handler` or use | ||
[`hast-util-santize`][sanitize]. | ||
@@ -187,5 +194,5 @@ ## Related | ||
[build-badge]: https://img.shields.io/travis/syntax-tree/mdast-util-heading-range.svg | ||
[build-badge]: https://github.com/syntax-tree/mdast-util-heading-range/workflows/main/badge.svg | ||
[build]: https://travis-ci.org/syntax-tree/mdast-util-heading-range | ||
[build]: https://github.com/syntax-tree/mdast-util-heading-range/actions | ||
@@ -230,3 +237,3 @@ [coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/mdast-util-heading-range.svg | ||
[onrun]: #function-onrunstart-nodes-end-scope | ||
[handler]: #function-handlerstart-nodes-end-scope | ||
@@ -233,0 +240,0 @@ [heading]: https://github.com/syntax-tree/mdast#heading |
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
13409
5
171
239
Yes
3
11
+ Added@types/mdast@^3.0.0
+ Added@types/unist@^2.0.0
+ Added@types/mdast@3.0.15(transitive)
+ Added@types/unist@2.0.11(transitive)
+ Addedmdast-util-to-string@3.2.0(transitive)
- Removedmdast-util-to-string@1.1.0(transitive)
Updatedmdast-util-to-string@^3.0.0