Skip to content

Commit f61466c

Browse files
committed
fix(sentence): correct handle line number for pre-node
1 parent 76cf0ad commit f61466c

File tree

5 files changed

+140
-15
lines changed

5 files changed

+140
-15
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ Split {Japanese, English} text into sentences.
66

77
npm install sentence-splitter
88

9+
**Requirements:**
10+
11+
- `Array.from`
12+
- `Array#fill`
13+
914
### CLI
1015

1116
$ npm install -g sentence-splitter

src/parser/SourceCode.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ export class SourceCode {
2626
this.startOffset = this.sourceNode.range[0];
2727
// start index is startOffset
2828
this.index = this.startOffset;
29-
const offset = Array.from(new Array(this.startOffset));
30-
this.textCharacters = offset.concat(input.raw.split(""));
31-
this.source = new StructureSource(input.raw);
29+
// before line count of Paragraph node
30+
const lineBreaks = Array.from(new Array(this.sourceNode.loc.start.line - 1)).fill("\n");
31+
const offset = Array.from(new Array(this.startOffset - lineBreaks.length));
32+
this.textCharacters = lineBreaks.concat(offset, input.raw.split(""));
33+
this.source = new StructureSource(this.textCharacters.join(""));
3234
if (this.sourceNode.children[0]) {
3335
// Header Node's children does not start with index 0
3436
// Example: # Header
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"type": "Paragraph",
3+
"children": [
4+
{
5+
"type": "Str",
6+
"value": "First, A is A.",
7+
"loc": {
8+
"start": {
9+
"line": 2,
10+
"column": 0
11+
},
12+
"end": {
13+
"line": 2,
14+
"column": 14
15+
}
16+
},
17+
"range": [
18+
1,
19+
15
20+
],
21+
"raw": "First, A is A."
22+
}
23+
],
24+
"loc": {
25+
"start": {
26+
"line": 2,
27+
"column": 0
28+
},
29+
"end": {
30+
"line": 2,
31+
"column": 14
32+
}
33+
},
34+
"range": [
35+
1,
36+
15
37+
],
38+
"raw": "First, A is A."
39+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"type": "Paragraph",
3+
"children": [
4+
{
5+
"type": "Sentence",
6+
"raw": "First, A is A.",
7+
"value": "First, A is A.",
8+
"loc": {
9+
"start": {
10+
"line": 2,
11+
"column": 0
12+
},
13+
"end": {
14+
"line": 2,
15+
"column": 14
16+
}
17+
},
18+
"range": [
19+
1,
20+
15
21+
],
22+
"children": [
23+
{
24+
"type": "Str",
25+
"raw": "First, A is A",
26+
"value": "First, A is A",
27+
"loc": {
28+
"start": {
29+
"line": 2,
30+
"column": 0
31+
},
32+
"end": {
33+
"line": 2,
34+
"column": 13
35+
}
36+
},
37+
"range": [
38+
1,
39+
14
40+
]
41+
},
42+
{
43+
"type": "Punctuation",
44+
"raw": ".",
45+
"value": ".",
46+
"loc": {
47+
"start": {
48+
"line": 2,
49+
"column": 13
50+
},
51+
"end": {
52+
"line": 2,
53+
"column": 14
54+
}
55+
},
56+
"range": [
57+
14,
58+
15
59+
]
60+
}
61+
]
62+
}
63+
],
64+
"loc": {
65+
"start": {
66+
"line": 2,
67+
"column": 0
68+
},
69+
"end": {
70+
"line": 2,
71+
"column": 14
72+
}
73+
},
74+
"range": [
75+
1,
76+
15
77+
],
78+
"raw": "First, A is A."
79+
}

test/fixtures/list-item-paragraph/output.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
"value": "text that follows a label, such as a Caution or Note.",
88
"loc": {
99
"start": {
10-
"line": 1,
11-
"column": 90
10+
"line": 5,
11+
"column": 86
1212
},
1313
"end": {
14-
"line": 1,
15-
"column": 143
14+
"line": 5,
15+
"column": 139
1616
}
1717
},
1818
"range": [
@@ -26,12 +26,12 @@
2626
"value": "text that follows a label, such as a Caution or Note",
2727
"loc": {
2828
"start": {
29-
"line": 1,
30-
"column": 90
29+
"line": 5,
30+
"column": 86
3131
},
3232
"end": {
33-
"line": 1,
34-
"column": 142
33+
"line": 5,
34+
"column": 138
3535
}
3636
},
3737
"range": [
@@ -45,12 +45,12 @@
4545
"value": ".",
4646
"loc": {
4747
"start": {
48-
"line": 1,
49-
"column": 142
48+
"line": 5,
49+
"column": 138
5050
},
5151
"end": {
52-
"line": 1,
53-
"column": 143
52+
"line": 5,
53+
"column": 139
5454
}
5555
},
5656
"range": [

0 commit comments

Comments
 (0)