1- 'use strict' ;
1+ 'use strict'
22
3- var repeat = require ( 'repeat-string' ) ;
4- var vfileLocation = require ( 'vfile-location' ) ;
5- var position = require ( 'unist-util-position' ) ;
6- var toString = require ( 'nlcst-to-string' ) ;
3+ var repeat = require ( 'repeat-string' )
4+ var vfileLocation = require ( 'vfile-location' )
5+ var position = require ( 'unist-util-position' )
6+ var toString = require ( 'nlcst-to-string' )
77
8- module . exports = toNLCST ;
8+ module . exports = toNLCST
99
10- var ignore = [
11- 'table' ,
12- 'tableRow' ,
13- 'tableCell'
14- ] ;
10+ var ignore = [ 'table' , 'tableRow' , 'tableCell' ]
1511
16- var source = [
17- 'inlineCode'
18- ] ;
12+ var source = [ 'inlineCode' ]
1913
20- var newline = '\n' ;
14+ var newline = '\n'
2115
22- /* Transform `tree` into `nlcst`. */
16+ // Transform `tree` into `nlcst`.
2317function toNLCST ( tree , file , Parser , options ) {
24- var settings = options || { } ;
25- var parser ;
18+ var settings = options || { }
19+ var parser
2620
27- /* Warn for invalid parameters. */
21+ // Warn for invalid parameters.
2822 if ( ! tree || ! tree . type ) {
29- throw new Error ( 'mdast-util-to-nlcst expected node' ) ;
23+ throw new Error ( 'mdast-util-to-nlcst expected node' )
3024 }
3125
3226 if ( ! file || ! file . messages ) {
33- throw new Error ( 'mdast-util-to-nlcst expected file' ) ;
27+ throw new Error ( 'mdast-util-to-nlcst expected file' )
3428 }
3529
36- /* Construct parser. */
30+ // Construct parser.
3731 if ( ! Parser ) {
38- throw new Error ( 'mdast-util-to-nlcst expected parser' ) ;
32+ throw new Error ( 'mdast-util-to-nlcst expected parser' )
3933 }
4034
4135 if (
@@ -44,129 +38,135 @@ function toNLCST(tree, file, Parser, options) {
4438 ! tree . position . start . column ||
4539 ! tree . position . start . line
4640 ) {
47- throw new Error ( 'mdast-util-to-nlcst expected position on nodes' ) ;
41+ throw new Error ( 'mdast-util-to-nlcst expected position on nodes' )
4842 }
4943
50- parser = 'parse' in Parser ? Parser : new Parser ( ) ;
51-
52- /* Transform mdast into NLCST tokens, and pass these
53- * into `parser.parse` to insert sentences, paragraphs
54- * where needed. */
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 ) ) ;
44+ parser = 'parse' in Parser ? Parser : new Parser ( )
45+
46+ // Transform mdast into NLCST tokens, and pass these into `parser.parse` to
47+ // insert sentences, paragraphs where needed.
48+ return parser . parse (
49+ one (
50+ {
51+ doc : String ( file ) ,
52+ location : vfileLocation ( file ) ,
53+ parser : parser ,
54+ ignore : ignore . concat ( settings . ignore || [ ] ) ,
55+ source : source . concat ( settings . source || [ ] )
56+ } ,
57+ tree
58+ )
59+ )
6260}
6361
64- /* Convert `node` into NLCST. */
62+ // Convert `node` into NLCST.
6563function one ( config , node ) {
66- var offset = config . location . toOffset ;
67- var parser = config . parser ;
68- var doc = config . doc ;
69- var type = node . type ;
70- var start = offset ( position . start ( node ) ) ;
71- var end = offset ( position . end ( 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 ) )
7270
7371 if ( config . ignore . indexOf ( type ) === - 1 ) {
7472 if ( config . source . indexOf ( type ) !== - 1 ) {
75- return patch ( config , [ parser . tokenizeSource ( doc . slice ( start , end ) ) ] , start ) ;
73+ return patch (
74+ config ,
75+ [ parser . tokenizeSource ( doc . slice ( start , end ) ) ] ,
76+ start
77+ )
7678 }
7779
7880 if ( node . children ) {
79- return all ( config , node ) ;
81+ return all ( config , node )
8082 }
8183
8284 if ( type === 'image' || type === 'imageReference' ) {
83- return patch ( config , parser . tokenize ( node . alt ) , start + 2 ) ;
85+ return patch ( config , parser . tokenize ( node . alt ) , start + 2 )
8486 }
8587
8688 if ( type === 'text' || type === 'escape' ) {
87- return patch ( config , parser . tokenize ( node . value ) , start ) ;
89+ return patch ( config , parser . tokenize ( node . value ) , start )
8890 }
8991
9092 if ( node . type === 'break' ) {
91- return patch ( config , [ parser . tokenizeWhiteSpace ( '\n' ) ] , start ) ;
93+ return patch ( config , [ parser . tokenizeWhiteSpace ( '\n' ) ] , start )
9294 }
9395 }
9496
95- return null ;
97+ return null
9698}
9799
98- /* Convert all nodes in `parent` (mdast) into NLCST. */
100+ // Convert all nodes in `parent` (mdast) into NLCST.
99101function all ( config , parent ) {
100- var children = parent . children ;
101- var length = children && children . length ;
102- var index = - 1 ;
103- var result = [ ] ;
104- var child ;
105- var node ;
106- var pos ;
107- var prevEndLine ;
108- var prevOffset ;
109- var endLine ;
102+ var children = parent . children
103+ var length = children && children . length
104+ var index = - 1
105+ var result = [ ]
106+ var child
107+ var node
108+ var pos
109+ var prevEndLine
110+ var prevOffset
111+ var endLine
110112
111113 while ( ++ index < length ) {
112- node = children [ index ] ;
113- pos = node . position ;
114- endLine = position . start ( node ) . line ;
114+ node = children [ index ]
115+ pos = node . position
116+ endLine = position . start ( node ) . line
115117
116118 if ( prevEndLine && endLine !== prevEndLine ) {
117- child = config . parser . tokenizeWhiteSpace ( repeat ( newline , endLine - prevEndLine ) ) ;
118- patch ( config , [ child ] , prevOffset ) ;
119+ child = config . parser . tokenizeWhiteSpace (
120+ repeat ( newline , endLine - prevEndLine )
121+ )
122+ patch ( config , [ child ] , prevOffset )
119123
120124 if ( child . value . length < 2 ) {
121- child . value = repeat ( newline , 2 ) ;
125+ child . value = repeat ( newline , 2 )
122126 }
123127
124- result . push ( child ) ;
128+ result . push ( child )
125129 }
126130
127- child = one ( config , node ) ;
131+ child = one ( config , node )
128132
129133 if ( child ) {
130- result = result . concat ( child ) ;
134+ result = result . concat ( child )
131135 }
132136
133- pos = position . end ( node ) ;
134- prevEndLine = pos . line ;
135- prevOffset = pos . offset ;
137+ pos = position . end ( node )
138+ prevEndLine = pos . line
139+ prevOffset = pos . offset
136140 }
137141
138- return result ;
142+ return result
139143}
140144
141- /* Patch a position on each node in `nodes`.
142- * `offset` is the offset in `file` this run of content
143- * starts at. */
145+ // Patch a position on each node in `nodes`.
146+ // `offset` is the offset in `file` this run of content starts at.
144147function patch ( config , nodes , offset ) {
145- var position = config . location . toPosition ;
146- var length = nodes . length ;
147- var index = - 1 ;
148- var start = offset ;
149- var children ;
150- var node ;
151- var end ;
148+ var position = config . location . toPosition
149+ var length = nodes . length
150+ var index = - 1
151+ var start = offset
152+ var children
153+ var node
154+ var end
152155
153156 while ( ++ index < length ) {
154- node = nodes [ index ] ;
155- children = node . children ;
157+ node = nodes [ index ]
158+ children = node . children
156159
157160 if ( children ) {
158- patch ( config , children , start ) ;
161+ patch ( config , children , start )
159162 }
160163
161- end = start + toString ( node ) . length ;
164+ end = start + toString ( node ) . length
162165
163- node . position = {
164- start : position ( start ) ,
165- end : position ( end )
166- } ;
166+ node . position = { start : position ( start ) , end : position ( end ) }
167167
168- start = end ;
168+ start = end
169169 }
170170
171- return nodes ;
171+ return nodes
172172}
0 commit comments