mdast-util-compact
Advanced tools
Comparing version 2.0.1 to 3.0.0
40
index.js
@@ -8,3 +8,3 @@ 'use strict' | ||
// Make an mdast tree compact by merging adjacent text nodes. | ||
function compact(tree, commonmark) { | ||
function compact(tree) { | ||
visit(tree, visitor) | ||
@@ -16,16 +16,15 @@ | ||
var siblings = parent ? parent.children : [] | ||
var prev = index && siblings[index - 1] | ||
var previous = index && siblings[index - 1] | ||
if ( | ||
prev && | ||
child.type === prev.type && | ||
mergeable(prev, commonmark) && | ||
mergeable(child, commonmark) | ||
previous && | ||
child.type === previous.type && | ||
(child.type === 'text' || child.type === 'blockquote') | ||
) { | ||
if (child.value) { | ||
prev.value += child.value | ||
previous.value += child.value | ||
} | ||
if (child.children) { | ||
prev.children = prev.children.concat(child.children) | ||
previous.children = previous.children.concat(child.children) | ||
} | ||
@@ -35,4 +34,4 @@ | ||
if (prev.position && child.position) { | ||
prev.position.end = child.position.end | ||
if (previous.position && child.position) { | ||
previous.position.end = child.position.end | ||
} | ||
@@ -44,22 +43,1 @@ | ||
} | ||
function mergeable(node, commonmark) { | ||
var start | ||
var end | ||
if (node.type === 'text') { | ||
if (!node.position) { | ||
return true | ||
} | ||
start = node.position.start | ||
end = node.position.end | ||
// Only merge nodes which occupy the same size as their `value`. | ||
return ( | ||
start.line !== end.line || end.column - start.column === node.value.length | ||
) | ||
} | ||
return commonmark && node.type === 'blockquote' | ||
} |
{ | ||
"name": "mdast-util-compact", | ||
"version": "2.0.1", | ||
"version": "3.0.0", | ||
"description": "mdast utility to make a tree compact", | ||
@@ -35,12 +35,12 @@ "license": "MIT", | ||
"nyc": "^15.0.0", | ||
"prettier": "^1.0.0", | ||
"remark-cli": "^7.0.0", | ||
"remark-preset-wooorm": "^6.0.0", | ||
"tape": "^4.0.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^8.0.0", | ||
"remark-preset-wooorm": "^7.0.0", | ||
"tape": "^5.0.0", | ||
"tinyify": "^2.0.0", | ||
"unist-builder": "^2.0.0", | ||
"xo": "^0.26.0" | ||
"xo": "^0.32.0" | ||
}, | ||
"scripts": { | ||
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix", | ||
"format": "remark . -qfo && prettier . --write && xo --fix", | ||
"build-bundle": "browserify . -s mdastUtilCompact > mdast-util-compact.js", | ||
@@ -47,0 +47,0 @@ "build-mangle": "browserify . -s mdastUtilCompact -p tinyify > mdast-util-compact.min.js", |
@@ -11,4 +11,4 @@ # mdast-util-compact | ||
[**mdast**][mdast] utility to make trees compact: collapse text nodes (when | ||
possible) and blockquotes (in commonmark mode). | ||
[**mdast**][mdast] utility to make trees compact: collapse adjacent text nodes | ||
and blockquotes. | ||
@@ -45,7 +45,6 @@ ## Install | ||
### `compact(tree[, commonmark])` | ||
### `compact(tree)` | ||
Walk the [tree][] and collapse nodes. | ||
Combines adjacent [text][]s (but not when they represent entities or escapes). | ||
If `commonmark` is `true`, collapses [blockquote][]s. | ||
Combines adjacent [text][]s and collapses [blockquote][]s. | ||
@@ -63,2 +62,7 @@ Handles [positional information][position-information] properly. | ||
## Related | ||
* [`mdast-squeeze-paragraphs`](https://github.com/syntax-tree/mdast-squeeze-paragraphs) | ||
— remove empty paragraphs | ||
## Contribute | ||
@@ -102,5 +106,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 | ||
@@ -113,7 +117,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 | ||
@@ -120,0 +124,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
132
7138
29