mdast-util-heading-range
Advanced tools
Comparing version 2.1.4 to 2.1.5
118
index.js
@@ -12,15 +12,24 @@ 'use strict' | ||
var test = options | ||
var ignoreFinalDefinitions = false | ||
var children = node.children | ||
var index = -1 | ||
var ignoreFinalDefinitions | ||
var depth | ||
var start | ||
var end | ||
var nodes | ||
var result | ||
var child | ||
// Object, not regex. | ||
if (test && typeof test === 'object' && !('exec' in test)) { | ||
ignoreFinalDefinitions = test.ignoreFinalDefinitions === true | ||
ignoreFinalDefinitions = test.ignoreFinalDefinitions | ||
test = test.test | ||
} | ||
// Transform a string into an applicable expression. | ||
if (typeof test === 'string') { | ||
test = toExpression(test) | ||
test = new RegExp('^(' + test + ')$', 'i') | ||
} | ||
// Regex | ||
// Regex. | ||
if (test && 'exec' in test) { | ||
@@ -38,44 +47,28 @@ test = wrapExpression(test) | ||
search(node, test, ignoreFinalDefinitions, callback) | ||
} | ||
// Search a node for heading range. | ||
function search(root, test, skip, callback) { | ||
var index = -1 | ||
var children = root.children | ||
var length = children.length | ||
var depth = null | ||
var start = null | ||
var end = null | ||
var nodes | ||
var clean | ||
var child | ||
while (++index < length) { | ||
// Find the range. | ||
while (++index < children.length) { | ||
child = children[index] | ||
if (closing(child, depth)) { | ||
end = index | ||
break | ||
} | ||
if (child.type === 'heading') { | ||
if (depth && child.depth <= depth) { | ||
end = index | ||
break | ||
} | ||
if (opening(child, depth, test)) { | ||
start = index | ||
depth = child.depth | ||
if (!depth && test(toString(child), child)) { | ||
depth = child.depth | ||
start = index | ||
// Assume no end heading is found. | ||
end = children.length | ||
} | ||
} | ||
} | ||
if (start !== null) { | ||
if (end === null) { | ||
end = length | ||
} | ||
if (skip) { | ||
while (end > start) { | ||
child = children[end - 1] | ||
if (!definition(child)) { | ||
break | ||
} | ||
// When we have a starting heading. | ||
if (depth) { | ||
if (ignoreFinalDefinitions) { | ||
while ( | ||
children[end - 1].type === 'definition' || | ||
children[end - 1].type === 'footnoteDefinition' | ||
) { | ||
end-- | ||
@@ -90,3 +83,3 @@ } | ||
{ | ||
parent: root, | ||
parent: node, | ||
start: start, | ||
@@ -97,16 +90,13 @@ end: children[end] ? end : null | ||
clean = [] | ||
index = -1 | ||
length = nodes && nodes.length | ||
if (nodes) { | ||
// Ensure no empty nodes are inserted. | ||
// This could be the case if `end` is in `nodes` but no `end` node exists. | ||
result = [] | ||
index = -1 | ||
// Ensure no empty nodes are inserted. This could be the case if `end` is | ||
// in `nodes` but no `end` node exists. | ||
while (++index < length) { | ||
if (nodes[index]) { | ||
clean.push(nodes[index]) | ||
while (++index < nodes.length) { | ||
if (nodes[index]) result.push(nodes[index]) | ||
} | ||
} | ||
if (nodes) { | ||
splice.apply(children, [start, end - start + 1].concat(clean)) | ||
splice.apply(children, [start, end - start + 1].concat(result)) | ||
} | ||
@@ -116,7 +106,2 @@ } | ||
// Transform a string into an applicable expression. | ||
function toExpression(value) { | ||
return new RegExp('^(' + value + ')$', 'i') | ||
} | ||
// Wrap an expression into an assertion function. | ||
@@ -131,20 +116,1 @@ function wrapExpression(expression) { | ||
} | ||
// Check if `node` is a heading. | ||
function heading(node) { | ||
return node && node.type === 'heading' | ||
} | ||
// Check if `node` is the main heading. | ||
function opening(node, depth, test) { | ||
return depth === null && heading(node) && test(toString(node), node) | ||
} | ||
// Check if `node` is the next heading. | ||
function closing(node, depth) { | ||
return depth && heading(node) && node.depth <= depth | ||
} | ||
function definition(node) { | ||
return node.type === 'definition' || node.type === 'footnoteDefinition' | ||
} |
{ | ||
"name": "mdast-util-heading-range", | ||
"version": "2.1.4", | ||
"version": "2.1.5", | ||
"description": "mdast utility to use headings as ranges in mdast", | ||
@@ -34,14 +34,14 @@ "license": "MIT", | ||
"devDependencies": { | ||
"browserify": "^16.0.0", | ||
"browserify": "^17.0.0", | ||
"nyc": "^15.0.0", | ||
"prettier": "^1.0.0", | ||
"remark": "^11.0.0", | ||
"remark-cli": "^7.0.0", | ||
"remark-preset-wooorm": "^6.0.0", | ||
"tape": "^4.0.0", | ||
"tinyify": "^2.0.0", | ||
"xo": "^0.26.0" | ||
"prettier": "^2.0.0", | ||
"remark": "^13.0.0", | ||
"remark-cli": "^9.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"tape": "^5.0.0", | ||
"tinyify": "^3.0.0", | ||
"xo": "^0.34.0" | ||
}, | ||
"scripts": { | ||
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix", | ||
"format": "remark . -qfo && prettier . --write && xo --fix", | ||
"build-bundle": "browserify . -s mdastUtilHeadingRange > mdast-util-heading-range.js", | ||
@@ -72,2 +72,3 @@ "build-mangle": "browserify . -s mdastUtilHeadingRange -p tinyify > mdast-util-heading-range.min.js", | ||
"rules": { | ||
"complexity": "off", | ||
"unicorn/prefer-type-error": "off" | ||
@@ -74,0 +75,0 @@ }, |
@@ -158,2 +158,7 @@ # mdast-util-heading-range | ||
## Related | ||
* [`mdast-zone`](https://github.com/syntax-tree/mdast-zone) | ||
— comments as ranges or markers instead of headings | ||
## Contribute | ||
@@ -197,5 +202,5 @@ | ||
[chat-badge]: https://img.shields.io/badge/chat-spectrum-7b16ff.svg | ||
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg | ||
[chat]: https://spectrum.chat/unified/syntax-tree | ||
[chat]: https://github.com/syntax-tree/unist/discussions | ||
@@ -208,7 +213,7 @@ [npm]: https://docs.npmjs.com/cli/install | ||
[contributing]: https://github.com/syntax-tree/.github/blob/master/contributing.md | ||
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md | ||
[support]: https://github.com/syntax-tree/.github/blob/master/support.md | ||
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md | ||
[coc]: https://github.com/syntax-tree/.github/blob/master/code-of-conduct.md | ||
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md | ||
@@ -215,0 +220,0 @@ [mdast]: https://github.com/syntax-tree/mdast |
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
232
11055
92