Actualizacion maquina principal
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / mdast-util-compact / index.js
1 'use strict'
2
3 var visit = require('unist-util-visit')
4
5 module.exports = compact
6
7 // Make an mdast tree compact by merging adjacent text nodes.
8 function compact(tree, commonmark) {
9   visit(tree, visitor)
10
11   return tree
12
13   function visitor(child, index, parent) {
14     var siblings = parent ? parent.children : []
15     var prev = index && siblings[index - 1]
16
17     if (
18       prev &&
19       child.type === prev.type &&
20       mergeable(prev, commonmark) &&
21       mergeable(child, commonmark)
22     ) {
23       if (child.value) {
24         prev.value += child.value
25       }
26
27       if (child.children) {
28         prev.children = prev.children.concat(child.children)
29       }
30
31       siblings.splice(index, 1)
32
33       if (prev.position && child.position) {
34         prev.position.end = child.position.end
35       }
36
37       return index
38     }
39   }
40 }
41
42 function mergeable(node, commonmark) {
43   var start
44   var end
45
46   if (node.type === 'text') {
47     if (!node.position) {
48       return true
49     }
50
51     start = node.position.start
52     end = node.position.end
53
54     // Only merge nodes which occupy the same size as their `value`.
55     return (
56       start.line !== end.line || end.column - start.column === node.value.length
57     )
58   }
59
60   return commonmark && node.type === 'blockquote'
61 }