Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options: checkLink, checkBlockQuote, checkEmphasis, checkHeader #35

Merged
merged 6 commits into from
Jul 21, 2018
Merged
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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ You can use `~` as Home directory abbreviation.
}
```

- `checkLink`(optional) : Check `Link` node type (default: `false`)
- `checkBlockQuote`(optional) : Check `BlockQuote` node type (default: `false`)
- `checkEmphasis`(optional) : Check `Emphasis` node type (default: `false`)
- `checkHeader`(optional) : Check `Header` node type (default: `true`)

```json
{
"rules": {
"prh": {
"checkEmphasis": true,
"checkHeader": false
}
}
}
```

### Fixable

Expand Down
32 changes: 29 additions & 3 deletions src/textlint-rule-prh.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ const prh = require("prh");
const path = require("path");
const untildify = require("untildify");

const defaultOptions = {
checkLink: false,
checkBlockQuote: false,
checkEmphasis: false,
checkHeader: true
};

function createPrhEngine(rulePaths, baseDir) {
if (rulePaths.length === 0) {
return null;
Expand Down Expand Up @@ -67,6 +74,23 @@ Please set .textlinrc:
}
};

const createIgnoreNodeTypes = (options, Syntax) => {
const nodeTypes = [];
if (!options.checkLink) {
nodeTypes.push(Syntax.Link);
}
if (!options.checkBlockQuote) {
nodeTypes.push(Syntax.BlockQuote);
}
if (!options.checkEmphasis) {
nodeTypes.push(Syntax.Emphasis);
}
if (!options.checkHeader) {
nodeTypes.push(Syntax.Header);
}
return nodeTypes;
};

/**
* for each diff of changeSet
* @param {ChangeSet} changeSet
Expand Down Expand Up @@ -118,8 +142,9 @@ const getConfigBaseDir = context => {
return textlintRcFilePath ? path.dirname(textlintRcFilePath) : process.cwd();
};

function reporter(context, options = {}) {
assertOptions(options);
function reporter(context, userOptions = {}) {
assertOptions(userOptions);
const options = Object.assign({}, defaultOptions, userOptions);
// .textlinrc directory
const textlintRCDir = getConfigBaseDir(context);
// create prh config
Expand All @@ -131,9 +156,10 @@ function reporter(context, options = {}) {
const prhEngine = mergePrh(prhEngineFiles, prhEngineContent);
const helper = new RuleHelper(context);
const { Syntax, getSource, report, fixer, RuleError } = context;
const ignoreNodeTypes = createIgnoreNodeTypes(options, Syntax);
return {
[Syntax.Str](node) {
if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis])) {
if (helper.isChildNode(node, ignoreNodeTypes)) {
return;
}
const text = getSource(node);
Expand Down
104 changes: 104 additions & 0 deletions test/prh-rule-tester-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ tester.run("prh", rule, {
options: {
rulePaths: [__dirname + "/fixtures/rule.yaml"]
}
},
{
text: "[jquery](jquery)\n> JQUERY\n\n*ディフォルト*",
options: {
rulePaths: [__dirname + "/fixtures/rule.yaml"]
}
},
{
text: "# ディフォルト設定",
options: {
rulePaths: [__dirname + "/fixtures/rule.yaml"],
checkHeader: false
}
}
],
invalid: [
Expand Down Expand Up @@ -108,6 +121,97 @@ tester.run("prh", rule, {
}
]
},
{
text: "[jquery](https://example.com)",
output: "[jQuery](https://example.com)",
options: {
rulePaths: [__dirname + "/fixtures/rule.yaml"],
checkLink: true
},
errors: [
{
type: "lint",
ruleId: "prh",
message: "jquery => jQuery",
index: 1,
line: 1,
column: 2,
severity: 2,
fix: {
range: [1, 7],
text: "jQuery"
}
}
]
},
{
text: "> JQUERY",
output: "> jQuery",
options: {
rulePaths: [__dirname + "/fixtures/rule.yaml"],
checkBlockQuote: true
},
errors: [
{
type: "lint",
ruleId: "prh",
message: "JQUERY => jQuery",
index: 2,
line: 1,
column: 3,
severity: 2,
fix: {
range: [2, 8],
text: "jQuery"
}
}
]
},
{
text: "*ディフォルト*",
output: "*デフォルト*",
options: {
rulePaths: [__dirname + "/fixtures/rule.yaml"],
checkEmphasis: true
},
errors: [
{
type: "lint",
ruleId: "prh",
message: "ディフォルト => デフォルト",
index: 1,
line: 1,
column: 2,
severity: 2,
fix: {
range: [1, 7],
text: "デフォルト"
}
}
]
},
{
text: "# ディフォルト設定",
output: "# デフォルト設定",
options: {
rulePaths: [__dirname + "/fixtures/rule.yaml"]
},
errors: [
{
type: "lint",
ruleId: "prh",
message: "ディフォルト => デフォルト",
index: 2,
line: 1,
column: 3,
severity: 2,
fix: {
range: [2, 8],
text: "デフォルト"
}
}
]
},
// example-prh.yml
{
text: "jqueryではクッキー。ディフォルトとハードウエアー。(そのとおり)\nサーバはサーバーサイドをjsする。",
Expand Down