Skip to content

Commit 1969f3b

Browse files
committed
fix(src): fix handling of end index value
1 parent 2ff4caf commit 1969f3b

File tree

2 files changed

+49
-30
lines changed

2 files changed

+49
-30
lines changed

src/StringSource.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,17 @@ export default class StringSource {
5555
* @returns {number|undefined} original
5656
*/
5757
originalIndexFromIndex(generatedIndex) {
58-
let hitTokenMaps = this.tokenMaps.filter(tokenMap => {
59-
let generated = tokenMap.generated;
60-
if (generated[0] <= generatedIndex && generatedIndex < generated[1]) {
61-
return true;
58+
let hitTokenMaps = this.tokenMaps.filter((tokenMap, index) => {
59+
const generated = tokenMap.generated;
60+
const nextGenerated = this.tokenMaps[index + 1];
61+
if (nextGenerated) {
62+
if (generated[0] <= generatedIndex && generatedIndex < nextGenerated[0]) {
63+
return true;
64+
}
65+
} else {
66+
if (generated[0] <= generatedIndex && generatedIndex <= generated[1]) {
67+
return true;
68+
}
6269
}
6370
});
6471
if (hitTokenMaps.length === 0) {

test/StringSource-test.js

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ import assert from "power-assert"
44
import {parse} from "markdown-to-ast";
55
import StringSource from "../src/StringSource";
66
import {split as sentenceSplitter} from "sentence-splitter";
7-
describe("StringSource", function () {
8-
describe("#toString", function () {
9-
it("should concat string", function () {
7+
describe("StringSource", function() {
8+
describe("#toString", function() {
9+
it("should concat string", function() {
1010
let AST = parse("**str**");
1111
let source = new StringSource(AST);
1212
assert.equal(source + "!!", "str!!");
1313
});
1414
});
15-
context("Each Pattern", function () {
16-
it("should return relative position of the node", function () {
15+
context("Each Pattern", function() {
16+
it("should return relative position of the node", function() {
1717
let AST = parse(`1st P
1818
1919
**2nd** P`);
@@ -45,7 +45,7 @@ describe("StringSource", function () {
4545
value: " P"
4646
});
4747
});
48-
it("Str", function () {
48+
it("Str", function() {
4949
let AST = parse("**str**");
5050
let source = new StringSource(AST);
5151
let result = source.toString();
@@ -59,7 +59,7 @@ describe("StringSource", function () {
5959
value: "str"
6060
});
6161
});
62-
it("Str that contain break line", function () {
62+
it("Str that contain break line", function() {
6363
let AST = parse("**st\nr**");
6464
let source = new StringSource(AST);
6565
let result = source.toString();
@@ -73,7 +73,7 @@ describe("StringSource", function () {
7373
value: "st\nr"
7474
});
7575
});
76-
it("Link", function () {
76+
it("Link", function() {
7777
let AST = parse("_[link](http://example)");
7878
let source = new StringSource(AST);
7979
let result = source.toString();
@@ -94,7 +94,7 @@ describe("StringSource", function () {
9494
value: "link"
9595
});
9696
});
97-
it("Str + `Code` + Str", function () {
97+
it("Str + `Code` + Str", function() {
9898
let AST = parse("text`code`text");
9999
let source = new StringSource(AST);
100100
let result = source.toString();
@@ -119,7 +119,7 @@ describe("StringSource", function () {
119119
value: "text"
120120
});
121121
});
122-
it("Header", function () {
122+
it("Header", function() {
123123
let AST = parse("# Header");
124124
let source = new StringSource(AST);
125125
let result = source.toString();
@@ -132,7 +132,7 @@ describe("StringSource", function () {
132132
value: "Header"
133133
});
134134
});
135-
it("Image + Str", function () {
135+
it("Image + Str", function() {
136136
let AST = parse("![alt](http://example.png) text");
137137
let source = new StringSource(AST);
138138
let result = source.toString();
@@ -151,7 +151,7 @@ describe("StringSource", function () {
151151
value: " text"
152152
});
153153
});
154-
it("confuse pattern", function () {
154+
it("confuse pattern", function() {
155155
let AST = parse("![!](http://example.com)");
156156
let source = new StringSource(AST);
157157
let result = source.toString();
@@ -164,7 +164,7 @@ describe("StringSource", function () {
164164
value: "!"
165165
});
166166
});
167-
it("Empty", function () {
167+
it("Empty", function() {
168168
let AST = parse("");
169169
let source = new StringSource(AST);
170170
let result = source.toString();
@@ -173,8 +173,8 @@ describe("StringSource", function () {
173173
});
174174
});
175175

176-
describe("#originalIndexFromIndex", function () {
177-
it("Str + Link", function () {
176+
describe("#originalIndexFromIndex", function() {
177+
it("Str + Link", function() {
178178
var originalText = "This is [Example!?](http://example.com/)";
179179
let AST = parse(originalText);
180180
let source = new StringSource(AST);
@@ -187,7 +187,7 @@ describe("StringSource", function () {
187187
assert.equal(index2, 15);
188188
assert.equal(source.originalIndexFromIndex(index2), 16);
189189
});
190-
it("should return original position for index", function () {
190+
it("should return original position for index", function() {
191191
var originalText = "![alt](http://example.png) text";
192192
let AST = parse(originalText);
193193
let source = new StringSource(AST);
@@ -198,15 +198,27 @@ describe("StringSource", function () {
198198
assert.deepEqual(source.originalIndexFromIndex(indexOf), 27);
199199
assert.equal(originalText.slice(27), "text");
200200
});
201-
it("should return null when not found position for index", function () {
201+
it("should return original position for index - end match", function() {
202+
var originalText = "![alt](http://example.png) text";
203+
let AST = parse(originalText);
204+
let source = new StringSource(AST);
205+
let result = source.toString();
206+
const indexOf = result.indexOf("text");
207+
assert.equal(indexOf, 4);
208+
assert.equal(indexOf + ("text".length - 1), 7);
209+
assert.equal(originalText[30], "t");
210+
assert.equal(source.originalIndexFromIndex(indexOf + ("text".length - 1)), 30);
211+
assert.equal(originalText[source.originalIndexFromIndex(indexOf + ("text".length - 1))], "t");
212+
});
213+
it("should return null when not found position for index", function() {
202214
var originalText = "![alt](http://example.png) text";
203215
let AST = parse(originalText);
204216
let source = new StringSource(AST);
205217
let result = source.toString();
206218
assert.equal(result, "alt text");
207219
assert.equal(source.originalIndexFromIndex(1000), null);
208220
});
209-
it("should return null when -1", function () {
221+
it("should return null when -1", function() {
210222
var originalText = "![alt](http://example.png) text";
211223
let AST = parse(originalText);
212224
let source = new StringSource(AST);
@@ -215,8 +227,8 @@ describe("StringSource", function () {
215227
assert.equal(source.originalIndexFromIndex(-1), null);
216228
});
217229
});
218-
describe("#originalPositionFromPosition", function () {
219-
it("Str + Link", function () {
230+
describe("#originalPositionFromPosition", function() {
231+
it("Str + Link", function() {
220232
var originalText = "This is [Example!?](http://example.com/)";
221233
let AST = parse(originalText);
222234
let source = new StringSource(AST);
@@ -237,7 +249,7 @@ describe("StringSource", function () {
237249
column: 16
238250
});
239251
});
240-
it("should return original position for index", function () {
252+
it("should return original position for index", function() {
241253
var originalText = "First\n![alt](http://example.png) text";
242254
let AST = parse(originalText);
243255
let source = new StringSource(AST);
@@ -254,7 +266,7 @@ describe("StringSource", function () {
254266
column: 27
255267
});
256268
});
257-
it("should return null when not found position for index", function () {
269+
it("should return null when not found position for index", function() {
258270
var originalText = "![alt](http://example.png) text";
259271
let AST = parse(originalText);
260272
let source = new StringSource(AST);
@@ -265,17 +277,17 @@ describe("StringSource", function () {
265277
column: -1
266278
}), null);
267279
});
268-
it("should throw error when position is not object", function () {
280+
it("should throw error when position is not object", function() {
269281
var originalText = "![alt](http://example.png) text";
270282
let AST = parse(originalText);
271283
let source = new StringSource(AST);
272284
let result = source.toString();
273285
assert.equal(result, "alt text");
274-
assert.throws(function () {
286+
assert.throws(function() {
275287
source.originalPositionFromPosition();
276288
});
277289
});
278-
it("with sentenceSplitter", function () {
290+
it("with sentenceSplitter", function() {
279291
var originalText = "`1`st.\n" +
280292
"``2`nd.`\n" +
281293
"`3`rd Text.";
@@ -307,5 +319,5 @@ describe("StringSource", function () {
307319
});
308320
});
309321
});
310-
322+
311323
});

0 commit comments

Comments
 (0)