|
3 | 3 | import {RuleHelper, IgnoreNodeManger} from "textlint-rule-helper"; |
4 | 4 | const StringSource = require("textlint-util-to-string").default; |
5 | 5 | const rousseau = require("rousseau"); |
| 6 | +const map = require("unist-util-map"); |
6 | 7 | const ObjectAssign = require("object-assign"); |
7 | 8 | const defaultOptions = { |
8 | 9 | // "suggestion", "warning", "error" |
9 | 10 | showLevels: ["suggestion", "warning", "error"], |
10 | 11 | // ignore check type of https://github.com/GitbookIO/rousseau#checks |
11 | 12 | ignoreTypes: [], |
12 | 13 | // ignore textlint's node type |
13 | | - ignoreInlineNodeTypes: undefined |
| 14 | + ignoreInlineNodeTypes: ["Code"] |
14 | 15 | }; |
15 | 16 |
|
16 | 17 | const mapNode = function (ast, mapFn) { |
@@ -39,30 +40,30 @@ export default function textlintRousseau(context, options = defaultOptions) { |
39 | 40 | return showLevels.indexOf(level) !== -1; |
40 | 41 | }; |
41 | 42 | /* |
42 | | - { |
43 | | - // Type of check that output this suggestion |
44 | | - type: "so", |
| 43 | + { |
| 44 | + // Type of check that output this suggestion |
| 45 | + type: "so", |
45 | 46 |
|
46 | | - // Level of importance |
47 | | - // "suggestion", "warning", "error" |
48 | | - level: "warning", |
| 47 | + // Level of importance |
| 48 | + // "suggestion", "warning", "error" |
| 49 | + level: "warning", |
49 | 50 |
|
50 | | - // Index in the text |
51 | | - index: 10, |
| 51 | + // Index in the text |
| 52 | + index: 10, |
52 | 53 |
|
53 | | - // Size of the section in the text |
54 | | - offset: 2, |
| 54 | + // Size of the section in the text |
| 55 | + offset: 2, |
55 | 56 |
|
56 | | - // Message to describe the suggestion |
57 | | - message: "omit 'So' from the beginning of sentences", |
| 57 | + // Message to describe the suggestion |
| 58 | + message: "omit 'So' from the beginning of sentences", |
58 | 59 |
|
59 | | - // Replacements suggestion |
60 | | - replacements: [ |
61 | | - { |
62 | | - value: "" |
63 | | - } |
64 | | - ] |
65 | | - } |
| 60 | + // Replacements suggestion |
| 61 | + replacements: [ |
| 62 | + { |
| 63 | + value: "" |
| 64 | + } |
| 65 | + ] |
| 66 | + } |
66 | 67 | */ |
67 | 68 | const createSuggest = (replacements) => { |
68 | 69 | if (replacements.length === 0) { |
@@ -104,8 +105,21 @@ export default function textlintRousseau(context, options = defaultOptions) { |
104 | 105 | // ignore if contain child node types |
105 | 106 | ignoreNodeManager.ignoreChildrenByTypes(node, ignoreInlineNodeTypes); |
106 | 107 | // check |
107 | | - |
108 | | - const source = new StringSource(node); |
| 108 | + // replace code with dummy code |
| 109 | + // if you want to filter(remove) code, use https://github.com/eush77/unist-util-filter |
| 110 | + const filteredNode = map(node, (node) => { |
| 111 | + if (node.type === Syntax.Code) { |
| 112 | + // only change `value` to dummy |
| 113 | + return ObjectAssign({}, node, { |
| 114 | + value: new Array(node.value.length + 1).join("x") |
| 115 | + }); |
| 116 | + } |
| 117 | + return node; |
| 118 | + }); |
| 119 | + if (!filteredNode) { |
| 120 | + return; |
| 121 | + } |
| 122 | + const source = new StringSource(filteredNode); |
109 | 123 | const text = source.toString(); |
110 | 124 | const reportSourceError = (results) => { |
111 | 125 | reportError(node, source, results); |
|
0 commit comments