|
1 | 1 | // LICENSE : MIT |
2 | 2 | "use strict"; |
3 | | -import {RuleHelper} from "textlint-rule-helper" |
4 | | -import StringSource from "textlint-util-to-string"; |
5 | | -import rousseau from "rousseau"; |
| 3 | +const {RuleHelper} = require("textlint-rule-helper"); |
| 4 | +const StringSource = require("textlint-util-to-string").default; |
| 5 | +const rousseau = require("rousseau"); |
| 6 | +const filter = require('unist-util-filter'); |
6 | 7 | const defaultOptions = { |
7 | 8 | // "suggestion", "warning", "error" |
8 | 9 | showLevels: ["suggestion", "warning", "error"], |
| 10 | + // ignore check type of https://github.com/GitbookIO/rousseau#checks |
9 | 11 | ignoreTypes: [], |
10 | | - fakeInlineCode: true |
| 12 | + // ignore textlint's node type |
| 13 | + ignoreInlineNodeTypes: undefined |
11 | 14 | }; |
12 | 15 | export default function textlintRousseau(context, options = defaultOptions) { |
13 | 16 | const helper = new RuleHelper(context); |
14 | 17 | const {Syntax, RuleError, report, getSource} = context; |
15 | 18 | const showLevels = options.showLevels || defaultOptions.showLevels; |
16 | 19 | const ignoreTypes = options.ignoreTypes || defaultOptions.ignoreTypes; |
17 | | - const fakeInlineCode = options.fakeInlineCode || defaultOptions.fakeInlineCode; |
| 20 | + const ignoreInlineNodeTypes = options.ignoreInlineNodeTypes || [Syntax.Image, Syntax.Code, Syntax.Link]; |
18 | 21 | const isShowType = (type)=> { |
19 | 22 | return ignoreTypes.indexOf(type) === -1; |
20 | 23 | }; |
@@ -74,29 +77,23 @@ export default function textlintRousseau(context, options = defaultOptions) { |
74 | 77 | }); |
75 | 78 | report(node, ruleError); |
76 | 79 | }; |
| 80 | + |
77 | 81 | return { |
78 | 82 | [Syntax.Paragraph](node){ |
79 | 83 | if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis])) { |
80 | 84 | return; |
81 | 85 | } |
82 | | - // fake `code` |
83 | | - if (fakeInlineCode) { |
84 | | - node.children = node.children.map(childNode => { |
85 | | - if (childNode.type === Syntax.Code) { |
86 | | - return { |
87 | | - type: Syntax.Str, |
88 | | - value: "code", |
89 | | - raw: "code", |
90 | | - loc: childNode.loc, |
91 | | - range: childNode.range |
92 | | - } |
93 | | - } |
94 | | - return childNode; |
95 | | - }); |
| 86 | + const filteredNode = filter(node, (node) => { |
| 87 | + return ignoreInlineNodeTypes.indexOf(node.type) === -1; |
| 88 | + }); |
| 89 | + if (!filteredNode) { |
| 90 | + return; |
96 | 91 | } |
97 | | - const source = new StringSource(node); |
| 92 | + const source = new StringSource(filteredNode); |
98 | 93 | const text = source.toString(); |
99 | | - const reportSourceError = reportError.bind(null, node, source); |
| 94 | + const reportSourceError = (results) => { |
| 95 | + reportError(node, source, results); |
| 96 | + }; |
100 | 97 | rousseau(text, function (err, results) { |
101 | 98 | if (err) { |
102 | 99 | throw err; |
|
0 commit comments