Skip to content

Commit ab3f8f6

Browse files
mt-caretazu
authored andcommitted
feat(option): add emoji support (#3)
* add emoji support * improve handling of emoji option * reuse unicode workaround string
1 parent 954bbfc commit ab3f8f6

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ textlint --rule ja-no-mixed-period README.md
4040
- 文末に使用する句点文字
4141
- デフォルト: "。"
4242

43+
- `allowEmojiAtEnd`(bool):
44+
- 絵文字を末尾に置くことを許可するかどうか
45+
- デフォルト: false
46+
4347
```json
4448
{
4549
"rules": {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"textlint-tester": "^2.0.0"
4343
},
4444
"dependencies": {
45+
"emoji-regex": "^6.1.0",
4546
"textlint-rule-helper": "^2.0.0"
4647
}
4748
}

src/textlint-rule-ja-no-mixed-period.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
// LICENSE : MIT
22
"use strict";
33
const RuleHelper = require("textlint-rule-helper").RuleHelper;
4+
const emojiRegExp = require("emoji-regex")();
45
const japaneseRegExp = /(?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[--])/;
56
const exceptionMarkRegExp = /[!?\)]/;
67
const defaultPeriodMark = /[\.]/;
78
const defaultOptions = {
89
// 優先する句点文字
9-
periodMark: "。"
10+
periodMark: "。",
11+
// 末尾に絵文字を置くことを許可するか
12+
allowEmojiAtEnd: false
1013
};
1114
const reporter = (context, options = {}) => {
1215
const {Syntax, RuleError, report, fixer, getSource} = context;
1316
const helper = new RuleHelper(context);
1417
const periodMark = options.periodMark || defaultOptions.periodMark;
18+
const allowEmojiAtEnd = options.allowEmojiAtEnd !== undefined ? options.allowEmojiAtEnd : defaultOptions.allowEmojiAtEnd;
1519
const ignoredNodeTypes = [Syntax.ListItem, Syntax.Link, Syntax.Code, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis];
1620
return {
1721
[Syntax.Paragraph](node){
@@ -27,8 +31,10 @@ const reporter = (context, options = {}) => {
2731
if (!japaneseRegExp.test(lastStrText)) {
2832
return;
2933
}
30-
const lastIndex = lastStrText.length - 1;
31-
const lastChar = lastStrText[lastIndex];
34+
// サロゲートペアを考慮した文字列長・文字アクセス
35+
const characters = [...lastStrText];
36+
const lastIndex = characters.length - 1;
37+
const lastChar = characters[lastIndex];
3238
if (lastChar === undefined) {
3339
return;
3440
}
@@ -48,6 +54,9 @@ const reporter = (context, options = {}) => {
4854
if (exceptionMarkRegExp.test(lastChar)) {
4955
return;
5056
}
57+
if (allowEmojiAtEnd && emojiRegExp.test(lastChar)) {
58+
return;
59+
}
5160
if (lastChar === periodMark) {
5261
return;
5362
}
@@ -71,4 +80,4 @@ const reporter = (context, options = {}) => {
7180
module.exports = {
7281
linter: reporter,
7382
fixer: reporter
74-
};
83+
};

test/textlint-rule-ja-no-mixed-period-test.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ tester.run("textlint-rule-ja-no-mixed-period", rule, {
1717
`[リンクリファレンスも][]`,
1818
`__強調表示も同じく__`,
1919
`> 引用も無視される`,
20+
{
21+
text: "絵文字が末尾にある。😆",
22+
options: {
23+
allowEmojiAtEnd: true
24+
},
25+
},
2026
],
2127
invalid: [
2228
// single match
@@ -84,6 +90,17 @@ tester.run("textlint-rule-ja-no-mixed-period", rule, {
8490
column: 5
8591
}
8692
]
87-
}
93+
},
94+
// emojis are not allowed by default
95+
{
96+
text: "絵文字が末尾にある。😆",
97+
errors: [
98+
{
99+
message: `文末が"。"で終わっていません。`,
100+
line: 1,
101+
column: 11
102+
}
103+
]
104+
},
88105
]
89-
});
106+
});

0 commit comments

Comments
 (0)