Skip to content

Commit aab5aa1

Browse files
committed
feat(rule): add checkImage option
1 parent 10e378f commit aab5aa1

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ textlint --rule @textlint-rule/no-invalid-control-character README.md
4949
- `checkCode`: `boolean`
5050
- Default: `false`
5151
- Check code if it is `true`
52+
- `checkImage`: `boolean`
53+
- Default: `false`
54+
- Check image title and alt texts if it is `true`
5255

5356
```json
5457
{

src/textlint-rule-no-invalid-control-character.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@ const DEFAULT_OPTION = {
2727
// Define allow char code like `\u0019`
2828
allow: [],
2929
// Check code if it is true
30-
checkCode: false
30+
checkCode: false,
31+
// Check image title and alt text if it is true
32+
checkImage: false
3133
};
3234

3335
const reporter = (context, options = {}) => {
3436
const { Syntax, RuleError, getSource, fixer, report } = context;
3537
const allow = options.allow || DEFAULT_OPTION.allow;
3638
const checkCode = options.checkCode !== undefined ? options.checkCode : DEFAULT_OPTION.checkCode;
39+
const checkImage = options.checkImage !== undefined ? options.checkImage : DEFAULT_OPTION.checkImage;
3740
const checkNode = node => {
3841
const text = getSource(node);
3942
// Ignore \r \n \t
@@ -70,6 +73,11 @@ const reporter = (context, options = {}) => {
7073
if (checkCode) {
7174
checkNode(node);
7275
}
76+
},
77+
[Syntax.Image](node) {
78+
if (checkImage) {
79+
checkNode(node);
80+
}
7381
}
7482
};
7583
};

test/textlint-rule-no-invalid-control-character-test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,29 @@ tester.run("textlint-rule-no-invalid-control-character", rule, {
3939
options: {
4040
checkCode: false
4141
}
42+
},
43+
{
44+
text: `![textlint logo\u0008](https://textlint.github.io/img/textlint-icon_256x256.png "logo\u0019")`,
45+
options: {
46+
checkImage: false
47+
}
48+
},
49+
{
50+
text: `![textlint logo\u0008](https://textlint.github.io/img/textlint-icon_256x256.png "logo\u0019")`,
51+
options: {
52+
checkImage: true,
53+
allow: ["\u0008", "\u0019"]
54+
},
55+
errors: [
56+
{
57+
message: "Found invalid control character(BACKSPACE \\u0008)",
58+
index: 15
59+
},
60+
{
61+
message: "Found invalid control character(END OF MEDIUM \\u0019)",
62+
index: 80
63+
}
64+
]
4265
}
4366
],
4467
invalid: [
@@ -79,6 +102,35 @@ var value = "\u0008"
79102
}
80103
]
81104
},
105+
{
106+
text: `![textlint logo\u0008](https://textlint.github.io/img/textlint-icon_256x256.png "logo\u0019")`,
107+
options: {
108+
checkImage: true
109+
},
110+
errors: [
111+
{
112+
message: "Found invalid control character(BACKSPACE \\u0008)",
113+
index: 15
114+
},
115+
{
116+
message: "Found invalid control character(END OF MEDIUM \\u0019)",
117+
index: 80
118+
}
119+
]
120+
},
121+
{
122+
text: `![textlint logo\u0008](https://textlint.github.io/img/textlint-icon_256x256.png "logo\u0019")`,
123+
options: {
124+
checkImage: true,
125+
allow: ["\u0019"]
126+
},
127+
errors: [
128+
{
129+
message: "Found invalid control character(BACKSPACE \\u0008)",
130+
index: 15
131+
}
132+
]
133+
},
82134
...invalid
83135
]
84136
});

0 commit comments

Comments
 (0)