This commit is contained in:
78
node_modules/unist-util-visit-parents/index.js
generated
vendored
Normal file
78
node_modules/unist-util-visit-parents/index.js
generated
vendored
Normal file
@@ -0,0 +1,78 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = visitParents
|
||||
|
||||
var convert = require('unist-util-is/convert')
|
||||
|
||||
var CONTINUE = true
|
||||
var SKIP = 'skip'
|
||||
var EXIT = false
|
||||
|
||||
visitParents.CONTINUE = CONTINUE
|
||||
visitParents.SKIP = SKIP
|
||||
visitParents.EXIT = EXIT
|
||||
|
||||
function visitParents(tree, test, visitor, reverse) {
|
||||
var is
|
||||
|
||||
if (typeof test === 'function' && typeof visitor !== 'function') {
|
||||
reverse = visitor
|
||||
visitor = test
|
||||
test = null
|
||||
}
|
||||
|
||||
is = convert(test)
|
||||
|
||||
one(tree, null, [])
|
||||
|
||||
// Visit a single node.
|
||||
function one(node, index, parents) {
|
||||
var result = []
|
||||
var subresult
|
||||
|
||||
if (!test || is(node, index, parents[parents.length - 1] || null)) {
|
||||
result = toResult(visitor(node, parents))
|
||||
|
||||
if (result[0] === EXIT) {
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
if (node.children && result[0] !== SKIP) {
|
||||
subresult = toResult(all(node.children, parents.concat(node)))
|
||||
return subresult[0] === EXIT ? subresult : result
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Visit children in `parent`.
|
||||
function all(children, parents) {
|
||||
var min = -1
|
||||
var step = reverse ? -1 : 1
|
||||
var index = (reverse ? children.length : min) + step
|
||||
var result
|
||||
|
||||
while (index > min && index < children.length) {
|
||||
result = one(children[index], index, parents)
|
||||
|
||||
if (result[0] === EXIT) {
|
||||
return result
|
||||
}
|
||||
|
||||
index = typeof result[1] === 'number' ? result[1] : index + step
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function toResult(value) {
|
||||
if (value !== null && typeof value === 'object' && 'length' in value) {
|
||||
return value
|
||||
}
|
||||
|
||||
if (typeof value === 'number') {
|
||||
return [CONTINUE, value]
|
||||
}
|
||||
|
||||
return [value]
|
||||
}
|
||||
22
node_modules/unist-util-visit-parents/license
generated
vendored
Normal file
22
node_modules/unist-util-visit-parents/license
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
(The MIT License)
|
||||
|
||||
Copyright (c) 2016 Titus Wormer <tituswormer@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
'Software'), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
87
node_modules/unist-util-visit-parents/node_modules/unist-util-is/convert.js
generated
vendored
Normal file
87
node_modules/unist-util-visit-parents/node_modules/unist-util-is/convert.js
generated
vendored
Normal file
@@ -0,0 +1,87 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = convert
|
||||
|
||||
function convert(test) {
|
||||
if (typeof test === 'string') {
|
||||
return typeFactory(test)
|
||||
}
|
||||
|
||||
if (test === null || test === undefined) {
|
||||
return ok
|
||||
}
|
||||
|
||||
if (typeof test === 'object') {
|
||||
return ('length' in test ? anyFactory : matchesFactory)(test)
|
||||
}
|
||||
|
||||
if (typeof test === 'function') {
|
||||
return test
|
||||
}
|
||||
|
||||
throw new Error('Expected function, string, or object as test')
|
||||
}
|
||||
|
||||
function convertAll(tests) {
|
||||
var results = []
|
||||
var length = tests.length
|
||||
var index = -1
|
||||
|
||||
while (++index < length) {
|
||||
results[index] = convert(tests[index])
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
// Utility assert each property in `test` is represented in `node`, and each
|
||||
// values are strictly equal.
|
||||
function matchesFactory(test) {
|
||||
return matches
|
||||
|
||||
function matches(node) {
|
||||
var key
|
||||
|
||||
for (key in test) {
|
||||
if (node[key] !== test[key]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
function anyFactory(tests) {
|
||||
var checks = convertAll(tests)
|
||||
var length = checks.length
|
||||
|
||||
return matches
|
||||
|
||||
function matches() {
|
||||
var index = -1
|
||||
|
||||
while (++index < length) {
|
||||
if (checks[index].apply(this, arguments)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// Utility to convert a string into a function which checks a given node’s type
|
||||
// for said string.
|
||||
function typeFactory(test) {
|
||||
return type
|
||||
|
||||
function type(node) {
|
||||
return Boolean(node && node.type === test)
|
||||
}
|
||||
}
|
||||
|
||||
// Utility to return true.
|
||||
function ok() {
|
||||
return true
|
||||
}
|
||||
37
node_modules/unist-util-visit-parents/node_modules/unist-util-is/index.js
generated
vendored
Normal file
37
node_modules/unist-util-visit-parents/node_modules/unist-util-is/index.js
generated
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
'use strict'
|
||||
|
||||
var convert = require('./convert')
|
||||
|
||||
module.exports = is
|
||||
|
||||
is.convert = convert
|
||||
|
||||
// Assert if `test` passes for `node`.
|
||||
// When a `parent` node is known the `index` of node should also be given.
|
||||
// eslint-disable-next-line max-params
|
||||
function is(node, test, index, parent, context) {
|
||||
var hasParent = parent !== null && parent !== undefined
|
||||
var hasIndex = index !== null && index !== undefined
|
||||
var check = convert(test)
|
||||
|
||||
if (
|
||||
hasIndex &&
|
||||
(typeof index !== 'number' || index < 0 || index === Infinity)
|
||||
) {
|
||||
throw new Error('Expected positive finite index or child node')
|
||||
}
|
||||
|
||||
if (hasParent && (!is(parent) || !parent.children)) {
|
||||
throw new Error('Expected parent node')
|
||||
}
|
||||
|
||||
if (!node || !node.type || typeof node.type !== 'string') {
|
||||
return false
|
||||
}
|
||||
|
||||
if (hasParent !== hasIndex) {
|
||||
throw new Error('Expected both parent and index')
|
||||
}
|
||||
|
||||
return Boolean(check.call(context, node, index, parent))
|
||||
}
|
||||
22
node_modules/unist-util-visit-parents/node_modules/unist-util-is/license
generated
vendored
Normal file
22
node_modules/unist-util-visit-parents/node_modules/unist-util-is/license
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
(The MIT license)
|
||||
|
||||
Copyright (c) 2015 Titus Wormer <tituswormer@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
'Software'), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
75
node_modules/unist-util-visit-parents/node_modules/unist-util-is/package.json
generated
vendored
Normal file
75
node_modules/unist-util-visit-parents/node_modules/unist-util-is/package.json
generated
vendored
Normal file
@@ -0,0 +1,75 @@
|
||||
{
|
||||
"name": "unist-util-is",
|
||||
"version": "3.0.0",
|
||||
"description": "Utility to check if a node passes a test",
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
"unist",
|
||||
"node",
|
||||
"is",
|
||||
"equal",
|
||||
"test",
|
||||
"type",
|
||||
"util",
|
||||
"utility"
|
||||
],
|
||||
"repository": "syntax-tree/unist-util-is",
|
||||
"bugs": "https://github.com/syntax-tree/unist-util-is/issues",
|
||||
"author": "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
|
||||
"contributors": [
|
||||
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
|
||||
],
|
||||
"files": [
|
||||
"index.js",
|
||||
"convert.js"
|
||||
],
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"browserify": "^16.0.0",
|
||||
"nyc": "^14.0.0",
|
||||
"prettier": "^1.0.0",
|
||||
"remark-cli": "^6.0.0",
|
||||
"remark-preset-wooorm": "^5.0.0",
|
||||
"tape": "^4.0.0",
|
||||
"tinyify": "^2.0.0",
|
||||
"xo": "^0.24.0"
|
||||
},
|
||||
"scripts": {
|
||||
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
|
||||
"build-bundle": "browserify . -s unistUtilIs > unist-util-is.js",
|
||||
"build-mangle": "browserify . -s unistUtilIs -p tinyify > unist-util-is.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"
|
||||
},
|
||||
"prettier": {
|
||||
"tabWidth": 2,
|
||||
"useTabs": false,
|
||||
"singleQuote": true,
|
||||
"bracketSpacing": false,
|
||||
"semi": false,
|
||||
"trailingComma": "none"
|
||||
},
|
||||
"xo": {
|
||||
"prettier": true,
|
||||
"esnext": false,
|
||||
"rules": {
|
||||
"unicorn/prefer-type-error": "off"
|
||||
},
|
||||
"ignore": [
|
||||
"unist-util-is.js"
|
||||
]
|
||||
},
|
||||
"nyc": {
|
||||
"check-coverage": true,
|
||||
"lines": 100,
|
||||
"functions": 100,
|
||||
"branches": 100
|
||||
},
|
||||
"remarkConfig": {
|
||||
"plugins": [
|
||||
"preset-wooorm"
|
||||
]
|
||||
}
|
||||
}
|
||||
202
node_modules/unist-util-visit-parents/node_modules/unist-util-is/readme.md
generated
vendored
Normal file
202
node_modules/unist-util-visit-parents/node_modules/unist-util-is/readme.md
generated
vendored
Normal file
@@ -0,0 +1,202 @@
|
||||
# unist-util-is
|
||||
|
||||
[![Build][build-badge]][build]
|
||||
[![Coverage][coverage-badge]][coverage]
|
||||
[![Downloads][downloads-badge]][downloads]
|
||||
[![Size][size-badge]][size]
|
||||
[![Sponsors][sponsors-badge]][collective]
|
||||
[![Backers][backers-badge]][collective]
|
||||
[![Chat][chat-badge]][chat]
|
||||
|
||||
[**unist**][unist] utility to check if a node passes a test.
|
||||
|
||||
## Install
|
||||
|
||||
[npm][]:
|
||||
|
||||
```sh
|
||||
npm install unist-util-is
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
var is = require('unist-util-is')
|
||||
|
||||
var node = {type: 'strong'}
|
||||
var parent = {type: 'paragraph', children: [node]}
|
||||
|
||||
function test(node, n) {
|
||||
return n === 5
|
||||
}
|
||||
|
||||
is() // => false
|
||||
is({children: []}) // => false
|
||||
is(node) // => true
|
||||
is(node, 'strong') // => true
|
||||
is(node, 'emphasis') // => false
|
||||
|
||||
is(node, node) // => true
|
||||
is(parent, {type: 'paragraph'}) // => true
|
||||
is(parent, {type: 'strong'}) // => false
|
||||
|
||||
is(node, test) // => false
|
||||
is(node, test, 4, parent) // => false
|
||||
is(node, test, 5, parent) // => true
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### `is(node[, test[, index, parent[, context]]])`
|
||||
|
||||
###### Parameters
|
||||
|
||||
* `node` ([`Node`][node]) — Node to check.
|
||||
* `test` ([`Function`][test], `string`, `Object`, or `Array.<Test>`, optional)
|
||||
— When not given, checks if `node` is a [`Node`][node].
|
||||
When `string`, works like passing `node => node.type === test`.
|
||||
When `array`, checks if any one of the subtests pass.
|
||||
When `object`, checks that all keys in `test` are in `node`,
|
||||
and that they have strictly equal values
|
||||
* `index` (`number`, optional) — [Index][] of `node` in `parent`
|
||||
* `parent` ([`Node`][node], optional) — [Parent][] of `node`
|
||||
* `context` (`*`, optional) — Context object to invoke `test` with
|
||||
|
||||
###### Returns
|
||||
|
||||
`boolean` — Whether `test` passed *and* `node` is a [`Node`][node] (object with
|
||||
`type` set to a non-empty `string`).
|
||||
|
||||
#### `function test(node[, index, parent])`
|
||||
|
||||
###### Parameters
|
||||
|
||||
* `node` ([`Node`][node]) — Node to check
|
||||
* `index` (`number?`) — [Index][] of `node` in `parent`
|
||||
* `parent` ([`Node?`][node]) — [Parent][] of `node`
|
||||
|
||||
###### Context
|
||||
|
||||
`*` — The to `is` given `context`.
|
||||
|
||||
###### Returns
|
||||
|
||||
`boolean?` — Whether `node` matches.
|
||||
|
||||
### `is.convert(test)`
|
||||
|
||||
Create a test function from `test`, that can later be called with a `node`,
|
||||
`index`, and `parent`.
|
||||
Useful if you’re going to test many nodes, for example when creating a utility
|
||||
where something else passes an is-compatible test.
|
||||
|
||||
Can also be accessed with `require('unist-util-is/convert')`.
|
||||
|
||||
For example:
|
||||
|
||||
```js
|
||||
var u = require('unist-builder')
|
||||
var convert = require('unist-util-is/convert')
|
||||
|
||||
var test = convert('leaf')
|
||||
|
||||
var tree = u('tree', [
|
||||
u('node', [u('leaf', '1')]),
|
||||
u('leaf', '2'),
|
||||
u('node', [u('leaf', '3'), u('leaf', '4')]),
|
||||
u('leaf', '5')
|
||||
])
|
||||
|
||||
var leafs = tree.children.filter((child, index) => test(child, index, tree))
|
||||
|
||||
console.log(leafs)
|
||||
```
|
||||
|
||||
Yields:
|
||||
|
||||
```js
|
||||
[({type: 'leaf', value: '2'}, {type: 'leaf', value: '5'})]
|
||||
```
|
||||
|
||||
## Related
|
||||
|
||||
* [`unist-util-find-after`](https://github.com/syntax-tree/unist-util-find-after)
|
||||
— Find a node after another node
|
||||
* [`unist-util-find-before`](https://github.com/syntax-tree/unist-util-find-before)
|
||||
— Find a node before another node
|
||||
* [`unist-util-find-all-after`](https://github.com/syntax-tree/unist-util-find-all-after)
|
||||
— Find all nodes after another node
|
||||
* [`unist-util-find-all-before`](https://github.com/syntax-tree/unist-util-find-all-before)
|
||||
— Find all nodes before another node
|
||||
* [`unist-util-find-all-between`](https://github.com/mrzmmr/unist-util-find-all-between)
|
||||
— Find all nodes between two nodes
|
||||
* [`unist-util-find`](https://github.com/blahah/unist-util-find)
|
||||
— Find nodes matching a predicate
|
||||
* [`unist-util-filter`](https://github.com/eush77/unist-util-filter)
|
||||
— Create a new tree with nodes that pass a check
|
||||
* [`unist-util-remove`](https://github.com/eush77/unist-util-remove)
|
||||
— Remove nodes from tree
|
||||
|
||||
## Contribute
|
||||
|
||||
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
|
||||
started.
|
||||
See [`support.md`][support] for ways to get help.
|
||||
|
||||
This project has a [Code of Conduct][coc].
|
||||
By interacting with this repository, organisation, or community you agree to
|
||||
abide by its terms.
|
||||
|
||||
## License
|
||||
|
||||
[MIT][license] © [Titus Wormer][author]
|
||||
|
||||
<!-- Definitions -->
|
||||
|
||||
[build-badge]: https://img.shields.io/travis/syntax-tree/unist-util-is.svg
|
||||
|
||||
[build]: https://travis-ci.org/syntax-tree/unist-util-is
|
||||
|
||||
[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-is.svg
|
||||
|
||||
[coverage]: https://codecov.io/github/syntax-tree/unist-util-is
|
||||
|
||||
[downloads-badge]: https://img.shields.io/npm/dm/unist-util-is.svg
|
||||
|
||||
[downloads]: https://www.npmjs.com/package/unist-util-is
|
||||
|
||||
[size-badge]: https://img.shields.io/bundlephobia/minzip/unist-util-is.svg
|
||||
|
||||
[size]: https://bundlephobia.com/result?p=unist-util-is
|
||||
|
||||
[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg
|
||||
|
||||
[backers-badge]: https://opencollective.com/unified/backers/badge.svg
|
||||
|
||||
[collective]: https://opencollective.com/unified
|
||||
|
||||
[chat-badge]: https://img.shields.io/badge/join%20the%20community-on%20spectrum-7b16ff.svg
|
||||
|
||||
[chat]: https://spectrum.chat/unified/syntax-tree
|
||||
|
||||
[npm]: https://docs.npmjs.com/cli/install
|
||||
|
||||
[license]: license
|
||||
|
||||
[author]: https://wooorm.com
|
||||
|
||||
[contributing]: https://github.com/syntax-tree/.github/blob/master/contributing.md
|
||||
|
||||
[support]: https://github.com/syntax-tree/.github/blob/master/support.md
|
||||
|
||||
[coc]: https://github.com/syntax-tree/.github/blob/master/code-of-conduct.md
|
||||
|
||||
[unist]: https://github.com/syntax-tree/unist
|
||||
|
||||
[node]: https://github.com/syntax-tree/unist#node
|
||||
|
||||
[parent]: https://github.com/syntax-tree/unist#parent-1
|
||||
|
||||
[index]: https://github.com/syntax-tree/unist#index
|
||||
|
||||
[test]: #function-testnode-index-parent
|
||||
70
node_modules/unist-util-visit-parents/package.json
generated
vendored
Normal file
70
node_modules/unist-util-visit-parents/package.json
generated
vendored
Normal file
@@ -0,0 +1,70 @@
|
||||
{
|
||||
"name": "unist-util-visit-parents",
|
||||
"version": "2.1.2",
|
||||
"description": "Recursively walk over unist nodes, with ancestral information",
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
"unist",
|
||||
"walk",
|
||||
"util",
|
||||
"utility"
|
||||
],
|
||||
"repository": "syntax-tree/unist-util-visit-parents",
|
||||
"bugs": "https://github.com/syntax-tree/unist-util-visit-parents/issues",
|
||||
"author": "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
|
||||
"contributors": [
|
||||
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
|
||||
],
|
||||
"files": [
|
||||
"index.js"
|
||||
],
|
||||
"dependencies": {
|
||||
"unist-util-is": "^3.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"browserify": "^16.0.0",
|
||||
"nyc": "^14.0.0",
|
||||
"prettier": "^1.0.0",
|
||||
"remark": "^10.0.0",
|
||||
"remark-cli": "^6.0.0",
|
||||
"remark-preset-wooorm": "^5.0.0",
|
||||
"tape": "^4.0.0",
|
||||
"tinyify": "^2.0.0",
|
||||
"xo": "^0.24.0"
|
||||
},
|
||||
"scripts": {
|
||||
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
|
||||
"build-bundle": "browserify index.js -s unistUtilVisitParents > unist-util-visit-parents.js",
|
||||
"build-mangle": "browserify index.js -s unistUtilVisitParents -p tinyify > unist-util-visit-parents.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"
|
||||
},
|
||||
"nyc": {
|
||||
"check-coverage": true,
|
||||
"lines": 100,
|
||||
"functions": 100,
|
||||
"branches": 100
|
||||
},
|
||||
"prettier": {
|
||||
"tabWidth": 2,
|
||||
"useTabs": false,
|
||||
"singleQuote": true,
|
||||
"bracketSpacing": false,
|
||||
"semi": false,
|
||||
"trailingComma": "none"
|
||||
},
|
||||
"xo": {
|
||||
"prettier": true,
|
||||
"esnext": false,
|
||||
"ignores": [
|
||||
"unist-util-visit-parents.js"
|
||||
]
|
||||
},
|
||||
"remarkConfig": {
|
||||
"plugins": [
|
||||
"preset-wooorm"
|
||||
]
|
||||
}
|
||||
}
|
||||
218
node_modules/unist-util-visit-parents/readme.md
generated
vendored
Normal file
218
node_modules/unist-util-visit-parents/readme.md
generated
vendored
Normal file
@@ -0,0 +1,218 @@
|
||||
# unist-util-visit-parents
|
||||
|
||||
[![Build][build-badge]][build]
|
||||
[![Coverage][coverage-badge]][coverage]
|
||||
[![Downloads][downloads-badge]][downloads]
|
||||
[![Size][size-badge]][size]
|
||||
[![Sponsors][sponsors-badge]][collective]
|
||||
[![Backers][backers-badge]][collective]
|
||||
[![Chat][chat-badge]][chat]
|
||||
|
||||
[**unist**][unist] utility to visit nodes, with ancestral information.
|
||||
|
||||
## Install
|
||||
|
||||
[npm][]:
|
||||
|
||||
```sh
|
||||
npm install unist-util-visit-parents
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
var remark = require('remark')
|
||||
var visit = require('unist-util-visit-parents')
|
||||
|
||||
var tree = remark.parse('Some _emphasis_, **importance**, and `code`.')
|
||||
|
||||
visit(tree, 'strong', visitor)
|
||||
|
||||
function visitor(node, ancestors) {
|
||||
console.log(ancestors)
|
||||
}
|
||||
```
|
||||
|
||||
Yields:
|
||||
|
||||
```js
|
||||
[ { type: 'root', children: [ [Object] ] },
|
||||
{ type: 'paragraph',
|
||||
children:
|
||||
[ [Object],
|
||||
[Object],
|
||||
[Object],
|
||||
[Object],
|
||||
[Object],
|
||||
[Object],
|
||||
[Object] ] } ]
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### `visit(tree[, test], visitor[, reverse])`
|
||||
|
||||
Visit nodes ([**inclusive descendants**][descendant] of [`tree`][tree]), with
|
||||
ancestral information. Optionally filtering nodes. Optionally in reverse.
|
||||
|
||||
###### Parameters
|
||||
|
||||
* `tree` ([`Node`][node]) — [Tree][] to traverse
|
||||
* `test` ([`Test`][is], optional) — [`is`][is]-compatible test (such as a
|
||||
[type][])
|
||||
* `visitor` ([Function][visitor]) — Function invoked when a node is found
|
||||
that passes `test`
|
||||
* `reverse` (`boolean`, default: `false`) — The tree is walked in [preorder][]
|
||||
(NLR), visiting the node itself, then its [head][], etc.
|
||||
When `reverse` is passed, the tree is stilled walked in preorder, but now
|
||||
in NRL (the node itself, then its [tail][], etc.)
|
||||
|
||||
#### `next? = visitor(node, ancestors)`
|
||||
|
||||
Invoked when a node (matching `test`, if given) is found.
|
||||
|
||||
Visitors are free to transform `node`.
|
||||
They can also transform the [parent][] of node (the last of `ancestors`).
|
||||
Replacing `node` itself, if `visit.SKIP` is not returned, still causes its
|
||||
[descendant][]s to be visited.
|
||||
If adding or removing previous [sibling][]s (or next siblings, in case of
|
||||
`reverse`) of `node`, `visitor` should return a new [`index`][index] (`number`)
|
||||
to specify the sibling to traverse after `node` is traversed.
|
||||
Adding or removing next siblings of `node` (or previous siblings, in case of
|
||||
reverse) is handled as expected without needing to return a new `index`.
|
||||
Removing the `children` property of parent still results in them being
|
||||
traversed.
|
||||
|
||||
###### Parameters
|
||||
|
||||
* `node` ([`Node`][node]) — Found node
|
||||
* `ancestors` (`Array.<Node>`) — [Ancestor][]s of `node`
|
||||
|
||||
##### Returns
|
||||
|
||||
The return value can have the following forms:
|
||||
|
||||
* [`index`][index] (`number`) — Treated as a tuple of `[CONTINUE, index]`
|
||||
* `action` (`*`) — Treated as a tuple of `[action]`
|
||||
* `tuple` (`Array.<*>`) — List with one or two values, the first an `action`,
|
||||
the second and `index`.
|
||||
Note that passing a tuple only makes sense if the `action` is `SKIP`.
|
||||
If the `action` is `EXIT`, that action can be returned.
|
||||
If the `action` is `CONTINUE`, `index` can be returned.
|
||||
|
||||
###### `action`
|
||||
|
||||
An action can have the following values:
|
||||
|
||||
* `visit.EXIT` (`false`) — Stop traversing immediately
|
||||
* `visit.CONTINUE` (`true`) — Continue traversing as normal (same behaviour
|
||||
as not returning anything)
|
||||
* `visit.SKIP` (`'skip'`) — Do not traverse this node’s children; continue
|
||||
with the specified index
|
||||
|
||||
###### `index`
|
||||
|
||||
[`index`][index] (`number`) — Move to the sibling at `index` next (after `node`
|
||||
itself is completely traversed).
|
||||
Useful if mutating the tree, such as removing the node the visitor is currently
|
||||
on, or any of its previous siblings (or next siblings, in case of `reverse`)
|
||||
Results less than `0` or greater than or equal to `children.length` stop
|
||||
traversing the parent
|
||||
|
||||
## Related
|
||||
|
||||
* [`unist-util-visit`](https://github.com/syntax-tree/unist-util-visit)
|
||||
— Like `visit-parents`, but with one parent
|
||||
* [`unist-util-filter`](https://github.com/eush77/unist-util-filter)
|
||||
— Create a new tree with all nodes that pass a test
|
||||
* [`unist-util-map`](https://github.com/syntax-tree/unist-util-map)
|
||||
— Create a new tree with all nodes mapped by a given function
|
||||
* [`unist-util-flatmap`](https://gitlab.com/staltz/unist-util-flatmap)
|
||||
— Create a new tree by mapping (to an array) with the provided function and
|
||||
then flattening
|
||||
* [`unist-util-remove`](https://github.com/eush77/unist-util-remove)
|
||||
— Remove nodes from a tree that pass a test
|
||||
* [`unist-util-select`](https://github.com/eush77/unist-util-select)
|
||||
— Select nodes with CSS-like selectors
|
||||
|
||||
## Contribute
|
||||
|
||||
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
|
||||
started.
|
||||
See [`support.md`][support] for ways to get help.
|
||||
|
||||
This project has a [Code of Conduct][coc].
|
||||
By interacting with this repository, organisation, or community you agree to
|
||||
abide by its terms.
|
||||
|
||||
## License
|
||||
|
||||
[MIT][license] © [Titus Wormer][author]
|
||||
|
||||
<!-- Definition -->
|
||||
|
||||
[build-badge]: https://img.shields.io/travis/syntax-tree/unist-util-visit-parents.svg
|
||||
|
||||
[build]: https://travis-ci.org/syntax-tree/unist-util-visit-parents
|
||||
|
||||
[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-visit-parents.svg
|
||||
|
||||
[coverage]: https://codecov.io/github/syntax-tree/unist-util-visit-parents
|
||||
|
||||
[downloads-badge]: https://img.shields.io/npm/dm/unist-util-visit-parents.svg
|
||||
|
||||
[downloads]: https://www.npmjs.com/package/unist-util-visit-parents
|
||||
|
||||
[size-badge]: https://img.shields.io/bundlephobia/minzip/unist-util-visit-parents.svg
|
||||
|
||||
[size]: https://bundlephobia.com/result?p=unist-util-visit-parents
|
||||
|
||||
[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg
|
||||
|
||||
[backers-badge]: https://opencollective.com/unified/backers/badge.svg
|
||||
|
||||
[collective]: https://opencollective.com/unified
|
||||
|
||||
[chat-badge]: https://img.shields.io/badge/join%20the%20community-on%20spectrum-7b16ff.svg
|
||||
|
||||
[chat]: https://spectrum.chat/unified/syntax-tree
|
||||
|
||||
[npm]: https://docs.npmjs.com/cli/install
|
||||
|
||||
[license]: license
|
||||
|
||||
[author]: https://wooorm.com
|
||||
|
||||
[unist]: https://github.com/syntax-tree/unist
|
||||
|
||||
[node]: https://github.com/syntax-tree/unist#node
|
||||
|
||||
[visitor]: #next--visitornode-ancestors
|
||||
|
||||
[contributing]: https://github.com/syntax-tree/.github/blob/master/contributing.md
|
||||
|
||||
[support]: https://github.com/syntax-tree/.github/blob/master/support.md
|
||||
|
||||
[coc]: https://github.com/syntax-tree/.github/blob/master/code-of-conduct.md
|
||||
|
||||
[is]: https://github.com/syntax-tree/unist-util-is
|
||||
|
||||
[preorder]: https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/
|
||||
|
||||
[descendant]: https://github.com/syntax-tree/unist#descendant
|
||||
|
||||
[head]: https://github.com/syntax-tree/unist#head
|
||||
|
||||
[tail]: https://github.com/syntax-tree/unist#tail
|
||||
|
||||
[parent]: https://github.com/syntax-tree/unist#parent-1
|
||||
|
||||
[sibling]: https://github.com/syntax-tree/unist#sibling
|
||||
|
||||
[index]: https://github.com/syntax-tree/unist#index
|
||||
|
||||
[ancestor]: https://github.com/syntax-tree/unist#ancestor
|
||||
|
||||
[tree]: https://github.com/syntax-tree/unist#tree
|
||||
|
||||
[type]: https://github.com/syntax-tree/unist#type
|
||||
Reference in New Issue
Block a user