Skip to content

Commit 30abe94

Browse files
committed
Add local textlint rules regarding spaces
1 parent ee3fc41 commit 30abe94

File tree

1 file changed

+70
-7
lines changed

1 file changed

+70
-7
lines changed

textlint/jp-reactjs-org-lint.js

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,82 @@
1-
/**
2-
* @param {RuleContext} context
3-
*/
1+
const toString = require('mdast-util-to-string');
2+
43
module.exports = context => {
54
return {
5+
// [context.Syntax.Header]: node => {},
6+
7+
[context.Syntax.Paragraph]: node => {
8+
// Checks against paragraph-based rules.
9+
10+
// Convert a paragraph to plain text, stripping any markups
11+
const text = toString(node);
12+
13+
enforceZenHanSpace(node, text, context);
14+
noHanPunctSpace(node, text, context);
15+
enforceQuestionSpace(node, text, context);
16+
noLineEndSpace(node, text, context);
17+
},
18+
619
[context.Syntax.Str]: node => {
7-
const text = context.getSource(node);
8-
const match = //.exec(text);
9-
if (match) {
20+
const text = toString(node);
21+
const index = text.indexOf(' ');
22+
if (index >= 0) {
1023
context.report(
1124
node,
1225
new context.RuleError(
13-
`「下さい」の代わりに「ください」を使ってください。`,
26+
'いかなる場合も全角スペースは使用しないでください。',
27+
{index},
1428
),
1529
);
1630
}
1731
},
1832
};
1933
};
34+
35+
const enforceQuestionSpace = (node, text, context) => {
36+
const markReg = /[](\u3000|[A-Za-z0-9]|[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[--])/;
37+
if (markReg.exec(text)) {
38+
context.report(
39+
node,
40+
new context.RuleError(
41+
'全角の「!」「?」と次の文の間には半角スペースを挿入してください。',
42+
),
43+
);
44+
}
45+
};
46+
47+
const noHanPunctSpace = (node, text, context) => {
48+
if (/[]( |\u3000)/.exec(text)) {
49+
context.report(
50+
node,
51+
new context.RuleError(
52+
'全角の句読点の直後にスペースを入れてはいけません。',
53+
),
54+
);
55+
}
56+
};
57+
58+
const enforceZenHanSpace = (node, text, context) => {
59+
const hanZen = /[A-Za-z0-9](?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[--])/;
60+
const zenHan = /(?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[--])[A-Za-z0-9]/;
61+
62+
if (hanZen.exec(text) || zenHan.exec(text)) {
63+
context.report(
64+
node,
65+
new context.RuleError(
66+
'全角文字と半角英数字とが隣接しています。半角スペースを挿入してください。',
67+
),
68+
);
69+
}
70+
};
71+
72+
const noLineEndSpace = (node, text, context) => {
73+
// This detects a line-end space *only when* the line contains Japanese characters.
74+
const reg = /(?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[--]).?(\s|\u3000)$/;
75+
76+
if (reg.exec(text)) {
77+
context.report(
78+
node,
79+
new context.RuleError('行末にスペース文字を入れないでください。'),
80+
);
81+
}
82+
};

0 commit comments

Comments
 (0)