Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ You can use `~` as Home directory abbreviation.
```

- `checkLink`(optional) : Check `Link` node type (default: `false`)
- `checkImage`(optional) : Check `Image` node type (default: `false`)
- Only alt text of image is scoped
- `checkBlockQuote`(optional) : Check `BlockQuote` node type (default: `false`)
- `checkEmphasis`(optional) : Check `Emphasis` node type (default: `false`)
- `checkHeader`(optional) : Check `Header` node type (default: `true`)
Expand Down
31 changes: 31 additions & 0 deletions src/textlint-rule-prh.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const untildify = (filePath) => {
};
const defaultOptions = {
checkLink: false,
checkImage: false,
checkBlockQuote: false,
checkEmphasis: false,
checkHeader: true,
Expand Down Expand Up @@ -82,6 +83,9 @@ const createIgnoreNodeTypes = (options, Syntax) => {
if (!options.checkLink) {
nodeTypes.push(Syntax.Link);
}
if (!options.checkImage) {
nodeTypes.push(Syntax.Image);
}
if (!options.checkBlockQuote) {
nodeTypes.push(Syntax.BlockQuote);
}
Expand Down Expand Up @@ -290,6 +294,33 @@ function reporter(context, userOptions = {}) {
report(node, new RuleError(error.message));
}
}
},
[Syntax.Image](node) {
if (!options.checkImage) {
return;
}
const text = node.alt;
const n_index_offset = 2;
const dummyFilePath = "";
const makeChangeSet = prhEngine.makeChangeSet(dummyFilePath, text);
forEachChange(makeChangeSet, text, ({ matchStartIndex, matchEndIndex, actual, expected, prh }) => {
// If result is not changed, should not report
if (actual === expected) {
return;
}
const suffix = prh !== null ? "\n" + prh : "";
const messages = actual + " => " + expected + suffix;
report(
node,
new RuleError(messages, {
index: matchStartIndex + n_index_offset,
fix: fixer.replaceTextRange(
[matchStartIndex + n_index_offset, matchEndIndex + n_index_offset],
expected
)
})
);
});
}
};
}
Expand Down
30 changes: 30 additions & 0 deletions test/prh-rule-tester-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ tester.run("prh", rule, {
rulePaths: [__dirname + "/fixtures/rule.yaml"]
}
},
{
text: "![jQuery](/path/to/logo.png)",
options: {
rulePaths: [__dirname + "/fixtures/rule.yaml"],
checkImage: true
}
},
{
text: "# ディフォルト設定",
options: {
Expand Down Expand Up @@ -184,6 +191,29 @@ ${CODE_START_JS}`,
}
]
},
{
text: "![jquery](/path/to/logo.png)",
output: "![jQuery](/path/to/logo.png)",
options: {
rulePaths: [__dirname + "/fixtures/rule.yaml"],
checkImage: true
},
errors: [
{
type: "lint",
ruleId: "prh",
message: "jquery => jQuery",
index: 2,
line: 1,
column: 3,
severity: 2,
fix: {
range: [2, 10],
text: "jQuery"
}
}
]
},
{
text: "> JQUERY",
output: "> jQuery",
Expand Down