Skip to content

Commit ead3b2a

Browse files
Lekoazu
authored andcommitted
Add options: checkLink, checkBlockQuote, checkEmphasis, checkHeader (#35)
Add options: checkLink, checkBlockQuote, checkEmphasis, checkHeader
1 parent dd4bb87 commit ead3b2a

File tree

3 files changed

+148
-3
lines changed

3 files changed

+148
-3
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ You can use `~` as Home directory abbreviation.
5050
}
5151
```
5252

53+
- `checkLink`(optional) : Check `Link` node type (default: `false`)
54+
- `checkBlockQuote`(optional) : Check `BlockQuote` node type (default: `false`)
55+
- `checkEmphasis`(optional) : Check `Emphasis` node type (default: `false`)
56+
- `checkHeader`(optional) : Check `Header` node type (default: `true`)
57+
58+
```json
59+
{
60+
"rules": {
61+
"prh": {
62+
"checkEmphasis": true,
63+
"checkHeader": false
64+
}
65+
}
66+
}
67+
```
5368

5469
### Fixable
5570

src/textlint-rule-prh.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ const prh = require("prh");
1717
const path = require("path");
1818
const untildify = require("untildify");
1919

20+
const defaultOptions = {
21+
checkLink: false,
22+
checkBlockQuote: false,
23+
checkEmphasis: false,
24+
checkHeader: true
25+
};
26+
2027
function createPrhEngine(rulePaths, baseDir) {
2128
if (rulePaths.length === 0) {
2229
return null;
@@ -67,6 +74,23 @@ Please set .textlinrc:
6774
}
6875
};
6976

77+
const createIgnoreNodeTypes = (options, Syntax) => {
78+
const nodeTypes = [];
79+
if (!options.checkLink) {
80+
nodeTypes.push(Syntax.Link);
81+
}
82+
if (!options.checkBlockQuote) {
83+
nodeTypes.push(Syntax.BlockQuote);
84+
}
85+
if (!options.checkEmphasis) {
86+
nodeTypes.push(Syntax.Emphasis);
87+
}
88+
if (!options.checkHeader) {
89+
nodeTypes.push(Syntax.Header);
90+
}
91+
return nodeTypes;
92+
};
93+
7094
/**
7195
* for each diff of changeSet
7296
* @param {ChangeSet} changeSet
@@ -118,8 +142,9 @@ const getConfigBaseDir = context => {
118142
return textlintRcFilePath ? path.dirname(textlintRcFilePath) : process.cwd();
119143
};
120144

121-
function reporter(context, options = {}) {
122-
assertOptions(options);
145+
function reporter(context, userOptions = {}) {
146+
assertOptions(userOptions);
147+
const options = Object.assign({}, defaultOptions, userOptions);
123148
// .textlinrc directory
124149
const textlintRCDir = getConfigBaseDir(context);
125150
// create prh config
@@ -131,9 +156,10 @@ function reporter(context, options = {}) {
131156
const prhEngine = mergePrh(prhEngineFiles, prhEngineContent);
132157
const helper = new RuleHelper(context);
133158
const { Syntax, getSource, report, fixer, RuleError } = context;
159+
const ignoreNodeTypes = createIgnoreNodeTypes(options, Syntax);
134160
return {
135161
[Syntax.Str](node) {
136-
if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis])) {
162+
if (helper.isChildNode(node, ignoreNodeTypes)) {
137163
return;
138164
}
139165
const text = getSource(node);

test/prh-rule-tester-test.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@ tester.run("prh", rule, {
1212
options: {
1313
rulePaths: [__dirname + "/fixtures/rule.yaml"]
1414
}
15+
},
16+
{
17+
text: "[jquery](jquery)\n> JQUERY\n\n*ディフォルト*",
18+
options: {
19+
rulePaths: [__dirname + "/fixtures/rule.yaml"]
20+
}
21+
},
22+
{
23+
text: "# ディフォルト設定",
24+
options: {
25+
rulePaths: [__dirname + "/fixtures/rule.yaml"],
26+
checkHeader: false
27+
}
1528
}
1629
],
1730
invalid: [
@@ -108,6 +121,97 @@ tester.run("prh", rule, {
108121
}
109122
]
110123
},
124+
{
125+
text: "[jquery](https://example.com)",
126+
output: "[jQuery](https://example.com)",
127+
options: {
128+
rulePaths: [__dirname + "/fixtures/rule.yaml"],
129+
checkLink: true
130+
},
131+
errors: [
132+
{
133+
type: "lint",
134+
ruleId: "prh",
135+
message: "jquery => jQuery",
136+
index: 1,
137+
line: 1,
138+
column: 2,
139+
severity: 2,
140+
fix: {
141+
range: [1, 7],
142+
text: "jQuery"
143+
}
144+
}
145+
]
146+
},
147+
{
148+
text: "> JQUERY",
149+
output: "> jQuery",
150+
options: {
151+
rulePaths: [__dirname + "/fixtures/rule.yaml"],
152+
checkBlockQuote: true
153+
},
154+
errors: [
155+
{
156+
type: "lint",
157+
ruleId: "prh",
158+
message: "JQUERY => jQuery",
159+
index: 2,
160+
line: 1,
161+
column: 3,
162+
severity: 2,
163+
fix: {
164+
range: [2, 8],
165+
text: "jQuery"
166+
}
167+
}
168+
]
169+
},
170+
{
171+
text: "*ディフォルト*",
172+
output: "*デフォルト*",
173+
options: {
174+
rulePaths: [__dirname + "/fixtures/rule.yaml"],
175+
checkEmphasis: true
176+
},
177+
errors: [
178+
{
179+
type: "lint",
180+
ruleId: "prh",
181+
message: "ディフォルト => デフォルト",
182+
index: 1,
183+
line: 1,
184+
column: 2,
185+
severity: 2,
186+
fix: {
187+
range: [1, 7],
188+
text: "デフォルト"
189+
}
190+
}
191+
]
192+
},
193+
{
194+
text: "# ディフォルト設定",
195+
output: "# デフォルト設定",
196+
options: {
197+
rulePaths: [__dirname + "/fixtures/rule.yaml"]
198+
},
199+
errors: [
200+
{
201+
type: "lint",
202+
ruleId: "prh",
203+
message: "ディフォルト => デフォルト",
204+
index: 2,
205+
line: 1,
206+
column: 3,
207+
severity: 2,
208+
fix: {
209+
range: [2, 8],
210+
text: "デフォルト"
211+
}
212+
}
213+
]
214+
},
111215
// example-prh.yml
112216
{
113217
text: "jqueryではクッキー。ディフォルトとハードウエアー。(そのとおり)\nサーバはサーバーサイドをjsする。",

0 commit comments

Comments
 (0)