3 var visit = require('unist-util-visit')
5 module.exports = compact
7 // Make an mdast tree compact by merging adjacent text nodes.
8 function compact(tree, commonmark) {
13 function visitor(child, index, parent) {
14 var siblings = parent ? parent.children : []
15 var prev = index && siblings[index - 1]
19 child.type === prev.type &&
20 mergeable(prev, commonmark) &&
21 mergeable(child, commonmark)
24 prev.value += child.value
28 prev.children = prev.children.concat(child.children)
31 siblings.splice(index, 1)
33 if (prev.position && child.position) {
34 prev.position.end = child.position.end
42 function mergeable(node, commonmark) {
46 if (node.type === 'text') {
51 start = node.position.start
52 end = node.position.end
54 // Only merge nodes which occupy the same size as their `value`.
56 start.line !== end.line || end.column - start.column === node.value.length
60 return commonmark && node.type === 'blockquote'