-
Notifications
You must be signed in to change notification settings - Fork 3
/
textlint-rule-no-unmatched-pair.js
49 lines (46 loc) · 2.11 KB
/
textlint-rule-no-unmatched-pair.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// MIT © 2018 azu
import { splitAST, Syntax as SentenceSyntax } from "sentence-splitter";
import { PairMaker } from "./parser/PairMaker.js";
import { SourceCode } from "./parser/SourceCode.js";
import { IgnoreNodeManager } from "textlint-rule-helper";
const report = context => {
const { Syntax, report, RuleError } = context;
const ignoreNodeManager = new IgnoreNodeManager();
return {
[Syntax.Paragraph](node) {
const sentences = splitAST(node);
ignoreNodeManager.ignoreChildrenByTypes(node, [
Syntax.CodeBlock,
Syntax.Code,
Syntax.Link,
Syntax.Strong,
Syntax.Emphasis,
Syntax.BlockQuote,
Syntax.Comment
]);
sentences.children.filter(node => node.type === SentenceSyntax.Sentence).forEach(sentence => {
const source = new SourceCode(sentence.raw);
const pairMaker = new PairMaker();
const sentenceIndex = sentence.range[0];
while (source.canRead) {
// If the character is in ignored range, skip it
const characterIndex = sentenceIndex + source.index;
// console.log(characterIndex, source.text[source.index], ignoreNodeManager.isIgnoredIndex(characterIndex));
if (!ignoreNodeManager.isIgnoredIndex(characterIndex)) {
pairMaker.mark(source);
}
source.peek();
}
// Report Error for each existing context keys
source.contextLocations.forEach((contextLocation) => {
report(node, new RuleError(`Not found pair character for ${contextLocation.pairMark.start}.
You should close this sentence with ${contextLocation.pairMark.end}.
This pair mark is called ${contextLocation.pairMark.key}.`, {
index: (sentenceIndex - node.range[0]) + contextLocation.index
}));
});
});
}
};
};
module.exports = report;