Skip to content

Commit 831eb1b

Browse files
committed
Fix errors for generated nodes
1 parent 9bbfcaa commit 831eb1b

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

index.js

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/* Dependencies. */
44
var repeat = require('repeat-string');
55
var vfileLocation = require('vfile-location');
6+
var position = require('unist-util-position');
67
var toString = require('nlcst-to-string');
78

89
/* Expose. */
@@ -61,9 +62,9 @@ function toNLCST(tree, file, Parser) {
6162
/* Convert `node` into NLCST. */
6263
function 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;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"dependencies": {
2525
"nlcst-to-string": "^2.0.0",
2626
"repeat-string": "^1.5.2",
27+
"unist-util-position": "^3.0.0",
2728
"vfile-location": "^2.0.0"
2829
},
2930
"devDependencies": {

0 commit comments

Comments
 (0)