33module . exports = toMdast
44
55var minify = require ( 'rehype-minify-whitespace' )
6+ var convert = require ( 'unist-util-is/convert' )
67var visit = require ( 'unist-util-visit' )
78var xtend = require ( 'xtend' )
89var one = require ( './lib/one' )
910var handlers = require ( './lib/handlers' )
1011
12+ var block = convert ( [ 'heading' , 'paragraph' , 'root' ] )
13+
1114function toMdast ( tree , options ) {
1215 var settings = options || { }
1316 var byId = { }
17+ var mdast
1418
1519 h . nodeById = byId
1620 h . baseFound = false
@@ -26,11 +30,15 @@ function toMdast(tree, options) {
2630 h . unchecked = settings . unchecked || '[ ]'
2731 h . quotes = settings . quotes || [ '"' ]
2832
29- visit ( tree , onvisit )
33+ visit ( tree , 'element' , onelement )
3034
3135 minify ( { newlines : settings . newlines === true } ) ( tree )
3236
33- return one ( h , tree , null )
37+ mdast = one ( h , tree , null )
38+
39+ visit ( mdast , 'text' , ontext )
40+
41+ return mdast
3442
3543 function h ( node , type , props , children ) {
3644 var result
@@ -65,12 +73,47 @@ function toMdast(tree, options) {
6573 return right
6674 }
6775
68- function onvisit ( node ) {
69- var props = node . properties || { }
70- var id = props . id
76+ function onelement ( node ) {
77+ var id = node . properties . id
7178
72- if ( id && ! ( id in node ) ) {
79+ if ( id && ! ( id in byId ) ) {
7380 byId [ id ] = node
7481 }
7582 }
83+
84+ // Collapse text nodes, and fix whitespace.
85+ // Most of this is taken care of by `rehype-minify-whitespace`, but
86+ // we’re generating some whitespace too, and some nodes are in the end
87+ // ignored.
88+ // So clean up:
89+ function ontext ( node , index , parent ) {
90+ var previous = parent . children [ index - 1 ]
91+
92+ if ( previous && node . type === previous . type ) {
93+ previous . value += node . value
94+
95+ parent . children . splice ( index , 1 )
96+
97+ if ( previous . position && node . position ) {
98+ previous . position . end = node . position . end
99+ }
100+
101+ // Iterate over the previous node again, to handle its total value.
102+ return index - 1
103+ }
104+
105+ node . value = node . value . replace ( / [ \t ] * ( \r ? \n | \r ) [ \t ] * / , '$1' )
106+
107+ // We don’t care about other phrasing nodes in between (e.g., `[ asd ]()`),
108+ // as there the whitespace matters.
109+ if ( block ( parent ) ) {
110+ if ( ! index ) {
111+ node . value = node . value . replace ( / ^ [ \t ] + / , '' )
112+ }
113+
114+ if ( index === parent . children . length - 1 ) {
115+ node . value = node . value . replace ( / [ \t ] + $ / , '' )
116+ }
117+ }
118+ }
76119}
0 commit comments