11'use strict'
22
3+ var toString = require ( 'nlcst-to-string' )
34var repeat = require ( 'repeat-string' )
4- var vfileLocation = require ( 'vfile-location' )
55var position = require ( 'unist-util-position' )
6- var toString = require ( 'nlcst-to-string' )
7-
8- module . exports = toNLCST
9-
10- var ignore = [ 'table' , 'tableRow' , 'tableCell' ]
11-
12- var source = [ 'inlineCode' ]
6+ var vfileLocation = require ( 'vfile-location' )
137
14- var newline = '\n'
8+ module . exports = toNlcst
159
16- // Transform `tree` into ` nlcst` .
17- function toNLCST ( tree , file , Parser , options ) {
10+ // Transform a `tree` in mdast to nlcst.
11+ function toNlcst ( tree , file , Parser , options ) {
1812 var settings = options || { }
1913 var parser
2014
@@ -43,36 +37,42 @@ function toNLCST(tree, file, Parser, options) {
4337
4438 parser = 'parse' in Parser ? Parser : new Parser ( )
4539
46- // Transform mdast into NLCST tokens, and pass these into `parser.parse` to
40+ // Transform mdast into nlcst tokens, and pass these into `parser.parse` to
4741 // insert sentences, paragraphs where needed.
4842 return parser . parse (
4943 one (
5044 {
5145 doc : String ( file ) ,
5246 location : vfileLocation ( file ) ,
5347 parser : parser ,
54- ignore : ignore . concat ( settings . ignore || [ ] ) ,
55- source : source . concat ( settings . source || [ ] )
48+ ignore : [ ] . concat (
49+ 'table' ,
50+ 'tableRow' ,
51+ 'tableCell' ,
52+ settings . ignore || [ ]
53+ ) ,
54+ source : [ ] . concat ( 'inlineCode' , settings . source || [ ] )
5655 } ,
5756 tree
5857 )
5958 )
6059}
6160
62- // Convert `node` into NLCST .
61+ // Transform a single node .
6362function one ( config , node ) {
64- var offset = config . location . toOffset
65- var parser = config . parser
66- var doc = config . doc
67- var type = node . type
68- var start = offset ( position . start ( node ) )
69- var end = offset ( position . end ( node ) )
70-
71- if ( config . ignore . indexOf ( type ) === - 1 ) {
72- if ( config . source . indexOf ( type ) !== - 1 ) {
63+ var start
64+ var end
65+
66+ if ( config . ignore . indexOf ( node . type ) < 0 ) {
67+ // To do: next major — nodes should have offsets, so
68+ // `config.location.toOffset` should be superfluous.
69+ start = config . location . toOffset ( position . start ( node ) )
70+ end = config . location . toOffset ( position . end ( node ) )
71+
72+ if ( config . source . indexOf ( node . type ) > - 1 ) {
7373 return patch (
7474 config ,
75- [ parser . tokenizeSource ( doc . slice ( start , end ) ) ] ,
75+ [ config . parser . tokenizeSource ( config . doc . slice ( start , end ) ) ] ,
7676 start
7777 )
7878 }
@@ -81,62 +81,49 @@ function one(config, node) {
8181 return all ( config , node )
8282 }
8383
84- if ( type === 'image' || type === 'imageReference' ) {
85- return patch ( config , parser . tokenize ( node . alt ) , start + 2 )
84+ if ( node . type === 'image' || node . type === 'imageReference' ) {
85+ return patch ( config , config . parser . tokenize ( node . alt ) , start + 2 )
8686 }
8787
88- if ( type === 'text' || type === 'escape ') {
89- return patch ( config , parser . tokenize ( node . value ) , start )
88+ if ( node . type === 'break ' ) {
89+ return patch ( config , [ config . parser . tokenizeWhiteSpace ( '\n' ) ] , start )
9090 }
9191
92- if ( node . type === 'break' ) {
93- return patch ( config , [ parser . tokenizeWhiteSpace ( '\n' ) ] , start )
92+ // To do: next major — remove `escape`.
93+ if ( node . type === 'text' || node . type === 'escape' ) {
94+ return patch ( config , config . parser . tokenize ( node . value ) , start )
9495 }
9596 }
96-
97- return null
9897}
9998
100- // Convert all nodes in `parent` (mdast) into NLCST .
99+ // Transform all nodes in `parent`.
101100function all ( config , parent ) {
102- var children = parent . children
103- var length = children && children . length
104- var index = - 1
105101 var result = [ ]
102+ var index = - 1
103+ var lineEnding
106104 var child
107- var node
108- var pos
109- var previousEndLine
110- var previousOffset
111- var endLine
112-
113- while ( ++ index < length ) {
114- node = children [ index ]
115- pos = node . position
116- endLine = position . start ( node ) . line
117-
118- if ( previousEndLine && endLine !== previousEndLine ) {
119- child = config . parser . tokenizeWhiteSpace (
120- repeat ( newline , endLine - previousEndLine )
121- )
122- patch ( config , [ child ] , previousOffset )
105+ var end
106+ var start
123107
124- if ( child . value . length < 2 ) {
125- child . value = repeat ( newline , 2 )
126- }
108+ while ( ++ index < parent . children . length ) {
109+ child = parent . children [ index ]
110+ start = position . start ( child )
127111
128- result . push ( child )
129- }
112+ if ( end && start . line !== end . line ) {
113+ lineEnding = config . parser . tokenizeWhiteSpace (
114+ repeat ( '\n' , start . line - end . line )
115+ )
116+ patch ( config , [ lineEnding ] , end . offset )
130117
131- child = one ( config , node )
118+ if ( lineEnding . value . length < 2 ) {
119+ lineEnding . value = '\n\n'
120+ }
132121
133- if ( child ) {
134- result = result . concat ( child )
122+ result . push ( lineEnding )
135123 }
136124
137- pos = position . end ( node )
138- previousEndLine = pos . line
139- previousOffset = pos . offset
125+ result = result . concat ( one ( config , child ) || [ ] )
126+ end = position . end ( child )
140127 }
141128
142129 return result
@@ -145,25 +132,24 @@ function all(config, parent) {
145132// Patch a position on each node in `nodes`.
146133// `offset` is the offset in `file` this run of content starts at.
147134function patch ( config , nodes , offset ) {
148- var position = config . location . toPosition
149- var length = nodes . length
150135 var index = - 1
151136 var start = offset
152- var children
153- var node
154137 var end
138+ var node
155139
156- while ( ++ index < length ) {
140+ while ( ++ index < nodes . length ) {
157141 node = nodes [ index ]
158- children = node . children
159142
160- if ( children ) {
161- patch ( config , children , start )
143+ if ( node . children ) {
144+ patch ( config , node . children , start )
162145 }
163146
164147 end = start + toString ( node ) . length
165148
166- node . position = { start : position ( start ) , end : position ( end ) }
149+ node . position = {
150+ start : config . location . toPoint ( start ) ,
151+ end : config . location . toPoint ( end )
152+ }
167153
168154 start = end
169155 }
0 commit comments