Skip to content

Commit 01cb78e

Browse files
authored
Merge pull request #7 from textlint-ja/footnode-support
fix(review): Footnote node support
2 parents 7f5ddde + 4e83968 commit 01cb78e

File tree

7 files changed

+3231
-63
lines changed

7 files changed

+3231
-63
lines changed

.babelrc

Lines changed: 0 additions & 13 deletions
This file was deleted.

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ english only
6969
- 例外として許可したい文字列を設定する
7070
- `periodMark`に指定したものは自動的に許可リストに加わる
7171
- デフォルトは空 `[]`
72-
- `allowEmojiAtEnd`(bool):
72+
- `allowEmojiAtEnd`: `boolean`
7373
- 絵文字を末尾に置くことを許可するかどうか
74-
- デフォルト: false
74+
- デフォルト: `false`
7575
- `forceAppendPeriod`: `boolean`
7676
- 句点で終わって無い場合に`periodMark`を--fix時に追加するかどうか
77-
- デフォルト: false
77+
- デフォルト: `false`
7878

7979
```json
8080
{

package.json

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,41 @@
2222
"test": "test"
2323
},
2424
"scripts": {
25-
"test": "mocha test/",
26-
"build": "NODE_ENV=production babel src --out-dir lib --source-maps",
27-
"watch": "babel src --out-dir lib --watch --source-maps",
28-
"prepublish": "npm run --if-present build"
25+
"test": "textlint-scripts test",
26+
"build": "textlint-scripts build",
27+
"watch": "textlint-scripts build --watch",
28+
"prepublish": "npm run --if-present build",
29+
"prettier": "prettier --write \"**/*.{js,jsx,ts,tsx,css}\""
2930
},
3031
"keywords": [
3132
"textlint",
3233
"textlintrule"
3334
],
3435
"devDependencies": {
35-
"babel-cli": "^6.10.1",
36-
"babel-preset-es2015": "^6.9.0",
37-
"babel-preset-jsdoc-to-assert": "^2.0.1",
38-
"babel-preset-power-assert": "^1.0.0",
39-
"babel-register": "^6.9.0",
40-
"mocha": "^2.5.3",
41-
"power-assert": "^1.4.1",
42-
"textlint-tester": "^2.0.0"
36+
"husky": "^1.1.2",
37+
"lint-staged": "^7.3.0",
38+
"prettier": "^1.8.1",
39+
"textlint-plugin-review": "^0.3.3",
40+
"textlint-scripts": "^2.1.0"
4341
},
4442
"dependencies": {
4543
"check-ends-with-period": "^1.0.1",
4644
"textlint-rule-helper": "^2.0.0"
45+
},
46+
"prettier": {
47+
"singleQuote": false,
48+
"printWidth": 120,
49+
"tabWidth": 4
50+
},
51+
"husky": {
52+
"hooks": {
53+
"precommit": "lint-staged"
54+
}
55+
},
56+
"lint-staged": {
57+
"*.{js,jsx,ts,tsx,css}": [
58+
"prettier --write",
59+
"git add"
60+
]
4761
}
48-
}
62+
}

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

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ const defaultOptions = {
2020
allowEmojiAtEnd: false,
2121
// 句点で終わって無い場合に`periodMark`を--fix時に追加するかどうか
2222
// デフォルトでは自動的に追加しない
23-
forceAppendPeriod: false
23+
forceAppendPeriod: false,
24+
// [Note] このオプションは標準外なので隠しオプション扱い
25+
// [Warning] このオプションはsemverの範囲外なのでいつでも壊れる可能性がある
26+
// 脚注はチェック対象から外すかどうか(実質Re:View向け)
27+
// デフォルトでは脚注構文(Re:View)は無視する
28+
checkFootnote: false
2429
};
2530
const reporter = (context, options = {}) => {
2631
const { Syntax, RuleError, report, fixer, getSource } = context;
@@ -29,18 +34,29 @@ const reporter = (context, options = {}) => {
2934
const preferPeriodMark = options.periodMark || defaultOptions.periodMark;
3035
// 優先する句点記号は常に句点として許可される
3136
const allowPeriodMarks = (options.allowPeriodMarks || defaultOptions.allowPeriodMarks).concat(preferPeriodMark);
32-
const allowEmojiAtEnd = options.allowEmojiAtEnd !== undefined
33-
? options.allowEmojiAtEnd
34-
: defaultOptions.allowEmojiAtEnd;
35-
const forceAppendPeriod = options.forceAppendPeriod !== undefined
36-
? options.forceAppendPeriod
37-
: defaultOptions.forceAppendPeriod;
38-
39-
const ignoredNodeTypes = [
40-
Syntax.ListItem, Syntax.Link, Syntax.Code, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis
37+
const allowEmojiAtEnd =
38+
options.allowEmojiAtEnd !== undefined ? options.allowEmojiAtEnd : defaultOptions.allowEmojiAtEnd;
39+
const forceAppendPeriod =
40+
options.forceAppendPeriod !== undefined ? options.forceAppendPeriod : defaultOptions.forceAppendPeriod;
41+
const checkFootnote = options.checkFootnote !== undefined ? options.checkFootnote : defaultOptions.checkFootnote;
42+
// 脚注のNode Typeを定義(TxtASTの定義外)
43+
const FootnoteNodes = [
44+
// https://github.com/orangain/textlint-plugin-review
45+
"Footnote",
46+
// https://github.com/textlint/textlint/blob/master/packages/%40textlint/markdown-to-ast/src/mapping/markdown-syntax-map.js
47+
// 実際にはmarkdown-to-astではこれはParagraphを含まないInlineNodeなのであまり意味はない
48+
"Definition"
4149
];
50+
const ignoredNodeTypes = [
51+
Syntax.ListItem,
52+
Syntax.Link,
53+
Syntax.Code,
54+
Syntax.Image,
55+
Syntax.BlockQuote,
56+
Syntax.Emphasis
57+
].concat(checkFootnote ? FootnoteNodes : []);
4258
return {
43-
[Syntax.Paragraph](node){
59+
[Syntax.Paragraph](node) {
4460
if (helper.isChildNode(node, ignoredNodeTypes)) {
4561
return;
4662
}
@@ -66,35 +82,47 @@ const reporter = (context, options = {}) => {
6682
}
6783
// 文末がスペースである場合はスペースを削除する
6884
if (/\s/.test(periodMark)) {
69-
report(lastNode, new RuleError(`文末が"${preferPeriodMark}"で終わっていません。末尾に不要なスペースがあります。`, {
70-
index,
71-
fix: fixer.replaceTextRange([index, index + periodMark.length], "")
72-
}));
73-
return
85+
report(
86+
lastNode,
87+
new RuleError(`文末が"${preferPeriodMark}"で終わっていません。末尾に不要なスペースがあります。`, {
88+
index,
89+
fix: fixer.replaceTextRange([index, index + periodMark.length], "")
90+
})
91+
);
92+
return;
7493
}
7594
// 典型的なパターンは自動的に`preferPeriodMark`に置き換える
7695
// 例) "." であるなら "。"に変換
7796
if (classicPeriodMarkPattern.test(periodMark)) {
78-
report(lastNode, new RuleError(`文末が"${preferPeriodMark}"で終わっていません。`, {
79-
index: index,
80-
fix: fixer.replaceTextRange([index, index + preferPeriodMark.length], preferPeriodMark)
81-
}));
97+
report(
98+
lastNode,
99+
new RuleError(`文末が"${preferPeriodMark}"で終わっていません。`, {
100+
index: index,
101+
fix: fixer.replaceTextRange([index, index + preferPeriodMark.length], preferPeriodMark)
102+
})
103+
);
82104
} else {
83105
// 句点を忘れているパターン
84106
if (forceAppendPeriod) {
85107
// `forceAppendPeriod`のオプションがtrueならば、自動で句点を追加する。
86-
report(lastNode, new RuleError(`文末が"${preferPeriodMark}"で終わっていません。`, {
87-
index: index,
88-
fix: fixer.replaceTextRange([index + 1, index + 1], preferPeriodMark)
89-
}));
108+
report(
109+
lastNode,
110+
new RuleError(`文末が"${preferPeriodMark}"で終わっていません。`, {
111+
index: index,
112+
fix: fixer.replaceTextRange([index + 1, index + 1], preferPeriodMark)
113+
})
114+
);
90115
} else {
91-
report(lastNode, new RuleError(`文末が"${preferPeriodMark}"で終わっていません。`, {
92-
index: index
93-
}));
116+
report(
117+
lastNode,
118+
new RuleError(`文末が"${preferPeriodMark}"で終わっていません。`, {
119+
index: index
120+
})
121+
);
94122
}
95123
}
96124
}
97-
}
125+
};
98126
};
99127

100128
module.exports = {

test/mocha.opts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
--compilers js:babel-register
1+
--require textlint-scripts/register

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

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
1+
import rule from "../src/textlint-rule-ja-no-mixed-period";
2+
13
const TextLintTester = require("textlint-tester");
4+
const reviewPlugin = require("textlint-plugin-review");
25
const tester = new TextLintTester();
3-
import rule from "../src/textlint-rule-ja-no-mixed-period";
6+
tester.run(
7+
"Re:view + textlint-rule-ja-no-mixed-period",
8+
{
9+
plugins: [
10+
{
11+
pluginId: "review",
12+
plugin: reviewPlugin
13+
}
14+
],
15+
rules: [
16+
{
17+
ruleId: "ja-no-mixed-period",
18+
rule: rule,
19+
options: {
20+
checkFootnote: true
21+
}
22+
}
23+
]
24+
},
25+
{
26+
valid: [
27+
{
28+
text: `//footnote[test][脚注はデフォルトで無視される]`,
29+
ext: ".re"
30+
}
31+
]
32+
}
33+
);
34+
435
tester.run("textlint-rule-ja-no-mixed-period", rule, {
536
valid: [
637
"これは問題ないです。",
@@ -22,13 +53,13 @@ tester.run("textlint-rule-ja-no-mixed-period", rule, {
2253
text: "絵文字が末尾にある。😆",
2354
options: {
2455
allowEmojiAtEnd: true
25-
},
56+
}
2657
},
2758
{
2859
text: "これはOK",
2960
options: {
3061
allowPeriodMarks: ["OK"]
31-
},
62+
}
3263
},
3364
{
3465
text: `次のコード:
@@ -38,7 +69,14 @@ tester.run("textlint-rule-ja-no-mixed-period", rule, {
3869
`,
3970
options: {
4071
allowPeriodMarks: [":"]
41-
},
72+
}
73+
},
74+
// 脚注はMarkdownでは常に無視される
75+
{
76+
text: `テストです。[^1]
77+
78+
[^1]: 脚注はデフォルトで無視される`,
79+
ext: ".md"
4280
}
4381
],
4482
invalid: [
@@ -140,6 +178,6 @@ tester.run("textlint-rule-ja-no-mixed-period", rule, {
140178
column: 11
141179
}
142180
]
143-
},
181+
}
144182
]
145183
});

0 commit comments

Comments
 (0)