Skip to content

Commit 6159d75

Browse files
committed
Merge pull request #4 from azu/fix_3
feat(rule): Support browser
2 parents ce80312 + a3de55a commit 6159d75

File tree

3 files changed

+87
-16
lines changed

3 files changed

+87
-16
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[textlint](https://github.com/azu/textlint "textlint") rule for [vvakame/prh](https://github.com/vvakame/prh "vvakame/prh").
44

5-
This rule check the spell by used with `rule.yaml`.
5+
This rule check the spell by used with `prh.yml`.
66

77
## Installation
88

@@ -16,13 +16,15 @@ This rule check the spell by used with `rule.yaml`.
1616
{
1717
"rules": {
1818
"prh": {
19-
"rulePaths" :["path/to/rule.yaml"]
19+
"rulePaths" :["path/to/prh.yml"]
2020
}
2121
}
2222
}
2323
```
2424

25-
### What is rule.yaml?
25+
`rulePaths` : path to YAML file.
26+
27+
### What is prh.yml?
2628

2729
Please See [vvakame/prh](https://github.com/vvakame/prh "vvakame/prh").
2830

src/prh-rule.js

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,38 @@ import {RuleHelper} from "textlint-rule-helper";
44
import StructuredSource from "structured-source";
55
import prh from "prh";
66
import path from "path";
7-
export default function (context, options) {
8-
if (typeof options === "undefined" || typeof options.rulePaths === "undefined") {
7+
function createPrhEngine(rulePaths, baseDir) {
8+
if (rulePaths.length === 0) {
9+
return null;
10+
}
11+
const prhEngine = prh.fromYAMLFilePath(path.resolve(baseDir, rulePaths[0]));
12+
rulePaths.slice(1).forEach(ruleFilePath => {
13+
const config = prh.fromYAMLFilePath(path.resolve(baseDir, ruleFilePath));
14+
prhEngine.merge(config);
15+
});
16+
return prhEngine;
17+
}
18+
function createPrhEngineFromContents(yamlContents) {
19+
if (yamlContents.length === 0) {
20+
return null;
21+
}
22+
const prhEngine = prh.fromYAML(null, yamlContents[0]);
23+
yamlContents.slice(1).forEach(content => {
24+
const config = prh.fromYAML(null, content);
25+
prhEngine.merge(config);
26+
});
27+
return prhEngine;
28+
}
29+
function mergePrh(...engines) {
30+
const engines_ = engines.filter(engine => !!engine);
31+
const mainEngine = engines_[0];
32+
engines_.slice(1).forEach(engine => {
33+
mainEngine.merge(engine);
34+
});
35+
return mainEngine;
36+
}
37+
export default function (context, options = {}) {
38+
if (typeof options.ruleContents === "undefined" && typeof options.rulePaths === "undefined") {
939
throw new Error(`textlint-rule-prh require Rule Options.
1040
Please set .textlinrc:
1141
{
@@ -17,17 +47,18 @@ Please set .textlinrc:
1747
}
1848
`);
1949
}
20-
var configFilePath = context.config ? context.config.configFile : null;
50+
const textlintRcFilePath = context.config ? context.config.configFile : null;
2151
// .textlinrc directory
22-
var textlintRCDir = configFilePath ? path.dirname(configFilePath) : process.cwd();
23-
let helper = new RuleHelper(context);
24-
let {Syntax, getSource, report, RuleError} = context;
25-
var rulePaths = options.rulePaths.slice();
26-
var config = prh.fromYAMLFilePath(path.resolve(textlintRCDir, rulePaths[0]));
27-
rulePaths.slice(1).forEach(function (rulePath) {
28-
var c = prh.fromYAMLFilePath(path.resolve(textlintRCDir, rulePath));
29-
config.merge(c);
30-
});
52+
const textlintRCDir = textlintRcFilePath ? path.dirname(textlintRcFilePath) : process.cwd();
53+
// create prh config
54+
const rulePaths = options.rulePaths || [];
55+
const ruleContents = options.ruleContents || [];
56+
// yaml file + yaml contents
57+
const prhEngineContent = createPrhEngineFromContents(ruleContents);
58+
const prhEngineFiles = createPrhEngine(rulePaths, textlintRCDir);
59+
const prhEngine = mergePrh(prhEngineFiles, prhEngineContent);
60+
const helper = new RuleHelper(context);
61+
const {Syntax, getSource, report, RuleError} = context;
3162
return {
3263
[Syntax.Str](node){
3364
if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis])) {
@@ -36,7 +67,7 @@ Please set .textlinrc:
3667
let text = getSource(node);
3768
// to get position from index
3869
let src = new StructuredSource(text);
39-
let makeChangeSet = config.makeChangeSet(null, text);
70+
let makeChangeSet = prhEngine.makeChangeSet(null, text);
4071
makeChangeSet.diffs.forEach(function (changeSet) {
4172
// | ----[match]------
4273
var slicedText = text.slice(changeSet.index);

test/textlintrc-test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// LICENSE : MIT
22
"use strict";
33
import assert from "power-assert";
4+
import fs from "fs";
45
import {TextLintCore} from "textlint";
56
import rule from "../src/prh-rule";
67
import path from "path";
@@ -34,6 +35,43 @@ describe(".textlinrc test", function () {
3435
assert(result.messages[0].line === 1);
3536
assert(result.messages[0].column === 1);
3637
});
38+
it("should resolve yaml content", function () {
39+
var textlint = new TextLintCore();
40+
var content = fs.readFileSync(path.join(__dirname, "fixtures", "rule.yaml"), "utf-8");
41+
textlint.setupRules({
42+
"prh": rule
43+
}, {
44+
"prh": {
45+
"ruleContents": [content]
46+
}
47+
});
48+
var result = textlint.lintMarkdown("jquery");
49+
assert(result.messages.length === 1);
50+
assert(result.messages[0].line === 1);
51+
assert(result.messages[0].column === 1);
52+
});
53+
it("should resolve yaml file and content", function () {
54+
var textlint = new TextLintCore();
55+
var content = fs.readFileSync(path.join(__dirname, "fixtures", "rule.yaml"), "utf-8");
56+
textlint.setupRules({
57+
"prh": rule
58+
}, {
59+
"prh": {
60+
// path support prh's `imports` feature
61+
"rulePaths": [path.join(__dirname, "fixtures", "imports.yml")],
62+
// content doesn't support `import`
63+
"ruleContents": [content]
64+
}
65+
});
66+
var result = textlint.lintMarkdown("jquery A");
67+
assert(result.messages.length === 2);
68+
assert(result.messages[0].line === 1);
69+
assert(result.messages[0].column === 1);
70+
assert(result.messages[1].line === 1);
71+
assert(result.messages[1].column === 8);
72+
73+
});
74+
3775
});
3876
context("prh features", function () {
3977
describe("import", function () {

0 commit comments

Comments
 (0)