@@ -7,21 +7,22 @@ var toString = require('nlcst-to-string');
7
7
8
8
module . exports = toNLCST ;
9
9
10
- /* Map of ignored mdast nodes: nodes which have no (simple)
11
- * representation in NLCST. */
12
- var IGNORE = {
13
- horizontalRule : true ,
14
- table : true ,
15
- tableRow : true ,
16
- tableCell : true
17
- } ;
10
+ var ignore = [
11
+ 'table' ,
12
+ 'tableRow' ,
13
+ 'tableCell'
14
+ ] ;
18
15
19
- var C_NEWLINE = '\n' ;
16
+ var source = [
17
+ 'inlineCode'
18
+ ] ;
19
+
20
+ var newline = '\n' ;
20
21
21
22
/* Transform `tree` into `nlcst`. */
22
- function toNLCST ( tree , file , Parser ) {
23
+ function toNLCST ( tree , file , Parser , options ) {
24
+ var settings = options || { } ;
23
25
var parser ;
24
- var location ;
25
26
26
27
/* Warn for invalid parameters. */
27
28
if ( ! tree || ! tree . type ) {
@@ -37,8 +38,6 @@ function toNLCST(tree, file, Parser) {
37
38
throw new Error ( 'mdast-util-to-nlcst expected parser' ) ;
38
39
}
39
40
40
- location = vfileLocation ( file ) ;
41
-
42
41
if (
43
42
! tree . position ||
44
43
! tree . position . start ||
@@ -53,44 +52,51 @@ function toNLCST(tree, file, Parser) {
53
52
/* Transform mdast into NLCST tokens, and pass these
54
53
* into `parser.parse` to insert sentences, paragraphs
55
54
* where needed. */
56
- return parser . parse ( one ( tree , null , null , file , location , parser ) ) ;
55
+ return parser . parse ( one ( {
56
+ doc : String ( file ) ,
57
+ location : vfileLocation ( file ) ,
58
+ parser : parser ,
59
+ ignore : ignore . concat ( settings . ignore || [ ] ) ,
60
+ source : source . concat ( settings . source || [ ] )
61
+ } , tree ) ) ;
57
62
}
58
63
59
64
/* Convert `node` into NLCST. */
60
- function one ( node , index , parent , file , location , parser ) {
65
+ function one ( config , node ) {
66
+ var offset = config . location . toOffset ;
67
+ var parser = config . parser ;
68
+ var doc = config . doc ;
61
69
var type = node . type ;
62
- var doc = String ( file ) ;
63
- var start = location . toOffset ( position . start ( node ) ) ;
64
- var end = location . toOffset ( position . end ( node ) ) ;
65
- var replacement ;
70
+ var start = offset ( position . start ( node ) ) ;
71
+ var end = offset ( position . end ( node ) ) ;
66
72
67
- if ( type in IGNORE ) {
68
- return null ;
69
- }
73
+ if ( config . ignore . indexOf ( type ) === - 1 ) {
74
+ if ( config . source . indexOf ( type ) !== - 1 ) {
75
+ return patch ( config , [ parser . tokenizeSource ( doc . slice ( start , end ) ) ] , start ) ;
76
+ }
70
77
71
- if ( node . children ) {
72
- replacement = all ( node , file , location , parser ) ;
73
- } else if (
74
- type === 'image' ||
75
- type === 'imageReference'
76
- ) {
77
- replacement = patch ( parser . tokenize ( node . alt ) , location , start + 2 ) ;
78
- } else if (
79
- type === 'text' ||
80
- type === 'escape'
81
- ) {
82
- replacement = patch ( parser . tokenize ( node . value ) , location , start ) ;
83
- } else if ( node . type === 'break' ) {
84
- replacement = patch ( [ parser . tokenizeWhiteSpace ( '\n' ) ] , location , start ) ;
85
- } else if ( node . type === 'inlineCode' ) {
86
- replacement = patch ( [ parser . tokenizeSource ( doc . slice ( start , end ) ) ] , location , start ) ;
78
+ if ( node . children ) {
79
+ return all ( config , node ) ;
80
+ }
81
+
82
+ if ( type === 'image' || type === 'imageReference' ) {
83
+ return patch ( config , parser . tokenize ( node . alt ) , start + 2 ) ;
84
+ }
85
+
86
+ if ( type === 'text' || type === 'escape' ) {
87
+ return patch ( config , parser . tokenize ( node . value ) , start ) ;
88
+ }
89
+
90
+ if ( node . type === 'break' ) {
91
+ return patch ( config , [ parser . tokenizeWhiteSpace ( '\n' ) ] , start ) ;
92
+ }
87
93
}
88
94
89
- return replacement || null ;
95
+ return null ;
90
96
}
91
97
92
98
/* Convert all nodes in `parent` (mdast) into NLCST. */
93
- function all ( parent , file , location , parser ) {
99
+ function all ( config , parent ) {
94
100
var children = parent . children ;
95
101
var length = children && children . length ;
96
102
var index = - 1 ;
@@ -108,20 +114,17 @@ function all(parent, file, location, parser) {
108
114
endLine = position . start ( node ) . line ;
109
115
110
116
if ( prevEndLine && endLine !== prevEndLine ) {
111
- child = parser . tokenizeWhiteSpace (
112
- repeat ( C_NEWLINE , endLine - prevEndLine )
113
- ) ;
114
-
115
- patch ( [ child ] , location , prevOffset ) ;
117
+ child = config . parser . tokenizeWhiteSpace ( repeat ( newline , endLine - prevEndLine ) ) ;
118
+ patch ( config , [ child ] , prevOffset ) ;
116
119
117
120
if ( child . value . length < 2 ) {
118
- child . value = repeat ( C_NEWLINE , 2 ) ;
121
+ child . value = repeat ( newline , 2 ) ;
119
122
}
120
123
121
124
result . push ( child ) ;
122
125
}
123
126
124
- child = one ( node , index , parent , file , location , parser ) ;
127
+ child = one ( config , node ) ;
125
128
126
129
if ( child ) {
127
130
result = result . concat ( child ) ;
@@ -138,7 +141,8 @@ function all(parent, file, location, parser) {
138
141
/* Patch a position on each node in `nodes`.
139
142
* `offset` is the offset in `file` this run of content
140
143
* starts at. */
141
- function patch ( nodes , location , offset ) {
144
+ function patch ( config , nodes , offset ) {
145
+ var position = config . location . toPosition ;
142
146
var length = nodes . length ;
143
147
var index = - 1 ;
144
148
var start = offset ;
@@ -151,14 +155,14 @@ function patch(nodes, location, offset) {
151
155
children = node . children ;
152
156
153
157
if ( children ) {
154
- patch ( children , location , start ) ;
158
+ patch ( config , children , start ) ;
155
159
}
156
160
157
161
end = start + toString ( node ) . length ;
158
162
159
163
node . position = {
160
- start : location . toPosition ( start ) ,
161
- end : location . toPosition ( end )
164
+ start : position ( start ) ,
165
+ end : position ( end )
162
166
} ;
163
167
164
168
start = end ;
0 commit comments