1
1
'use strict'
2
2
3
+ var toString = require ( 'nlcst-to-string' )
3
4
var repeat = require ( 'repeat-string' )
4
- var vfileLocation = require ( 'vfile-location' )
5
5
var 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' )
13
7
14
- var newline = '\n'
8
+ module . exports = toNlcst
15
9
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 ) {
18
12
var settings = options || { }
19
13
var parser
20
14
@@ -43,36 +37,42 @@ function toNLCST(tree, file, Parser, options) {
43
37
44
38
parser = 'parse' in Parser ? Parser : new Parser ( )
45
39
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
47
41
// insert sentences, paragraphs where needed.
48
42
return parser . parse (
49
43
one (
50
44
{
51
45
doc : String ( file ) ,
52
46
location : vfileLocation ( file ) ,
53
47
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 || [ ] )
56
55
} ,
57
56
tree
58
57
)
59
58
)
60
59
}
61
60
62
- // Convert `node` into NLCST .
61
+ // Transform a single node .
63
62
function 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 ) {
73
73
return patch (
74
74
config ,
75
- [ parser . tokenizeSource ( doc . slice ( start , end ) ) ] ,
75
+ [ config . parser . tokenizeSource ( config . doc . slice ( start , end ) ) ] ,
76
76
start
77
77
)
78
78
}
@@ -81,62 +81,49 @@ function one(config, node) {
81
81
return all ( config , node )
82
82
}
83
83
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 )
86
86
}
87
87
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 )
90
90
}
91
91
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 )
94
95
}
95
96
}
96
-
97
- return null
98
97
}
99
98
100
- // Convert all nodes in `parent` (mdast) into NLCST .
99
+ // Transform all nodes in `parent`.
101
100
function all ( config , parent ) {
102
- var children = parent . children
103
- var length = children && children . length
104
- var index = - 1
105
101
var result = [ ]
102
+ var index = - 1
103
+ var lineEnding
106
104
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
123
107
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 )
127
111
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 )
130
117
131
- child = one ( config , node )
118
+ if ( lineEnding . value . length < 2 ) {
119
+ lineEnding . value = '\n\n'
120
+ }
132
121
133
- if ( child ) {
134
- result = result . concat ( child )
122
+ result . push ( lineEnding )
135
123
}
136
124
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 )
140
127
}
141
128
142
129
return result
@@ -145,25 +132,24 @@ function all(config, parent) {
145
132
// Patch a position on each node in `nodes`.
146
133
// `offset` is the offset in `file` this run of content starts at.
147
134
function patch ( config , nodes , offset ) {
148
- var position = config . location . toPosition
149
- var length = nodes . length
150
135
var index = - 1
151
136
var start = offset
152
- var children
153
- var node
154
137
var end
138
+ var node
155
139
156
- while ( ++ index < length ) {
140
+ while ( ++ index < nodes . length ) {
157
141
node = nodes [ index ]
158
- children = node . children
159
142
160
- if ( children ) {
161
- patch ( config , children , start )
143
+ if ( node . children ) {
144
+ patch ( config , node . children , start )
162
145
}
163
146
164
147
end = start + toString ( node ) . length
165
148
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
+ }
167
153
168
154
start = end
169
155
}
0 commit comments