33/* Dependencies. */
44var repeat = require ( 'repeat-string' ) ;
55var vfileLocation = require ( 'vfile-location' ) ;
6+ var position = require ( 'unist-util-position' ) ;
67var toString = require ( 'nlcst-to-string' ) ;
78
89/* Expose. */
@@ -61,9 +62,9 @@ function toNLCST(tree, file, Parser) {
6162/* Convert `node` into NLCST. */
6263function one ( node , index , parent , file , location , parser ) {
6364 var type = node . type ;
64- var pos = node . position ;
65- var start = location . toOffset ( pos . start ) ;
66- var end = location . toOffset ( pos . end ) ;
65+ var doc = String ( file ) ;
66+ var start = location . toOffset ( position . start ( node ) ) ;
67+ var end = location . toOffset ( position . end ( node ) ) ;
6768 var replacement ;
6869
6970 if ( type in IGNORE ) {
@@ -76,24 +77,16 @@ function one(node, index, parent, file, location, parser) {
7677 type === 'image' ||
7778 type === 'imageReference'
7879 ) {
79- replacement = patch (
80- parser . tokenize ( node . alt ) , location , start + 2
81- ) ;
80+ replacement = patch ( parser . tokenize ( node . alt ) , location , start + 2 ) ;
8281 } else if (
8382 type === 'text' ||
8483 type === 'escape'
8584 ) {
86- replacement = patch (
87- parser . tokenize ( node . value ) , location , start
88- ) ;
85+ replacement = patch ( parser . tokenize ( node . value ) , location , start ) ;
8986 } else if ( node . type === 'break' ) {
90- replacement = patch ( [
91- parser . tokenizeWhiteSpace ( '\n' )
92- ] , location , start ) ;
87+ replacement = patch ( [ parser . tokenizeWhiteSpace ( '\n' ) ] , location , start ) ;
9388 } else if ( node . type === 'inlineCode' ) {
94- replacement = patch ( [ parser . tokenizeSource (
95- file . toString ( ) . slice ( start , end )
96- ) ] , location , start ) ;
89+ replacement = patch ( [ parser . tokenizeSource ( doc . slice ( start , end ) ) ] , location , start ) ;
9790 }
9891
9992 return replacement || null ;
@@ -115,7 +108,7 @@ function all(parent, file, location, parser) {
115108 while ( ++ index < length ) {
116109 node = children [ index ] ;
117110 pos = node . position ;
118- endLine = pos . start . line ;
111+ endLine = position . start ( node ) . line ;
119112
120113 if ( prevEndLine && endLine !== prevEndLine ) {
121114 child = parser . tokenizeWhiteSpace (
@@ -137,8 +130,9 @@ function all(parent, file, location, parser) {
137130 result = result . concat ( child ) ;
138131 }
139132
140- prevEndLine = pos . end . line ;
141- prevOffset = pos . end . offset ;
133+ pos = position . end ( node ) ;
134+ prevEndLine = pos . line ;
135+ prevOffset = pos . offset ;
142136 }
143137
144138 return result ;
0 commit comments