Skip to content

Commit 1c25412

Browse files
committed
Fix handling of final abbreviations
Previously, if an abbreviation ended the paragraph (for example, if `Id.` was passed in), the full-stop was seen as the terminal marker of the sentence and not part of the word (even though it’s a recognised abbreviation). Related to retextjs/retext-contractions#6.
1 parent f9c4db8 commit 1c25412

File tree

5 files changed

+59
-317
lines changed

5 files changed

+59
-317
lines changed

lib/plugin/merge-prefix-exceptions.js

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = modifyChildren(mergePrefixExceptions);
1010
* abbreviation. */
1111
var ABBREVIATION_PREFIX = new RegExp(
1212
'^(' +
13-
'[0-9]+|' +
13+
'[0-9]{1,3}|' +
1414
'[a-z]|' +
1515

1616
/* Common Latin Abbreviations:
@@ -31,39 +31,46 @@ var ABBREVIATION_PREFIX = new RegExp(
3131
* sentence ends with a certain word. */
3232
function mergePrefixExceptions(child, index, parent) {
3333
var children = child.children;
34+
var period;
3435
var node;
3536
var next;
3637

37-
if (
38-
children &&
39-
children.length !== 0 &&
40-
index !== parent.children.length - 1
41-
) {
42-
node = children[children.length - 1];
38+
if (children && children.length > 1) {
39+
period = children[children.length - 1];
4340

44-
if (node && toString(node) === '.') {
41+
if (period && toString(period) === '.') {
4542
node = children[children.length - 2];
4643

4744
if (
4845
node &&
4946
node.type === 'WordNode' &&
50-
ABBREVIATION_PREFIX.test(
51-
toString(node).toLowerCase()
52-
)
47+
ABBREVIATION_PREFIX.test(toString(node).toLowerCase())
5348
) {
49+
/* Merge period into abbreviation. */
50+
node.children.push(period);
51+
children.pop();
52+
53+
/* Update position. */
54+
if (period.position && node.position) {
55+
node.position.end = period.position.end;
56+
}
57+
58+
/* Merge sentences. */
5459
next = parent.children[index + 1];
5560

56-
child.children = children.concat(next.children);
61+
if (next) {
62+
child.children = children.concat(next.children);
5763

58-
parent.children.splice(index + 1, 1);
64+
parent.children.splice(index + 1, 1);
5965

60-
/* Update position. */
61-
if (next.position && child.position) {
62-
child.position.end = next.position.end;
63-
}
66+
/* Update position. */
67+
if (next.position && child.position) {
68+
child.position.end = next.position.end;
69+
}
6470

65-
/* Next, iterate over the current node again. */
66-
return index - 1;
71+
/* Next, iterate over the current node again. */
72+
return index - 1;
73+
}
6774
}
6875
}
6976
}

test/fixture/combining-tie-character.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,22 @@
237237
"offset": 22
238238
}
239239
}
240+
},
241+
{
242+
"type": "PunctuationNode",
243+
"value": ".",
244+
"position": {
245+
"start": {
246+
"line": 1,
247+
"column": 23,
248+
"offset": 22
249+
},
250+
"end": {
251+
"line": 1,
252+
"column": 24,
253+
"offset": 23
254+
}
255+
}
240256
}
241257
],
242258
"position": {
@@ -245,22 +261,6 @@
245261
"column": 22,
246262
"offset": 21
247263
},
248-
"end": {
249-
"line": 1,
250-
"column": 23,
251-
"offset": 22
252-
}
253-
}
254-
},
255-
{
256-
"type": "PunctuationNode",
257-
"value": ".",
258-
"position": {
259-
"start": {
260-
"line": 1,
261-
"column": 23,
262-
"offset": 22
263-
},
264264
"end": {
265265
"line": 1,
266266
"column": 24,

test/fixture/combining-tie-under-inverted.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,22 @@
237237
"offset": 26
238238
}
239239
}
240+
},
241+
{
242+
"type": "PunctuationNode",
243+
"value": ".",
244+
"position": {
245+
"start": {
246+
"line": 1,
247+
"column": 27,
248+
"offset": 26
249+
},
250+
"end": {
251+
"line": 1,
252+
"column": 28,
253+
"offset": 27
254+
}
255+
}
240256
}
241257
],
242258
"position": {
@@ -245,22 +261,6 @@
245261
"column": 26,
246262
"offset": 25
247263
},
248-
"end": {
249-
"line": 1,
250-
"column": 27,
251-
"offset": 26
252-
}
253-
}
254-
},
255-
{
256-
"type": "PunctuationNode",
257-
"value": ".",
258-
"position": {
259-
"start": {
260-
"line": 1,
261-
"column": 27,
262-
"offset": 26
263-
},
264264
"end": {
265265
"line": 1,
266266
"column": 28,

0 commit comments

Comments
 (0)