A helper library for textlint rule.
npm install textlint-rule-helperThis is helper library for creating textlint rule.
```
npm install textlint-rule-helper
- ~2.2: textlint 12>=
- ~2.3: textlint 13>=
Helper for traversing TxtAST.
#### ruleHelper.getParents(node) : TxtNode[]
Get parents of node.
The parent nodes are returned in order from the closest parent to the outer ones.
node is not contained in the results.
Params
- node TxtNode - the node is start point.
#### ruleHelper.isChildNode(node, types): boolean
Return true if node is wrapped any one of node types.
Params
- node TxtNode - is target nodeArray.
- types - are wrapped target node
#### ruleHelper.isPlainStrNode(node): boolean
isPlainStrNode() return true if the node is Str node and fill following conditions:
- the node is Str node
- the node is under the Paragraph node
- the node is not under the BlockQuote
Params
- node TxtNode - is target node
This function is useful for the common use case.
If you want to lint Str node, but you do not want to lint styled node, this function is useful.
The styled node is Link, Strong, BlockQuote, Header, and it may be written by other people.
For example, you have added a link to your document, the link's title is written by other people.
Opposite of it, The plain Str node is just under the Paragraph node, and it was written by you.
Examples
Return true
`markdown`
str str str
- list text
Return false
`markdown`Header
!alt text
link title
> BlockQuote text
Strong text
[linkReference][]
[^footnote text]
use case
You can manager ignoring range in texts.
#### ignore(node): void
Add the range of node to ignoring range list.
Params
- node TxtNode - target node
#### ignoreRange(range): void
Add the range to ignoring range list
Params
- range [number, number]
#### ignoreChildrenByTypes(targetNode, ignoredNodeTypes): void
if the children node has the type that is included in ignoredNodeTypes,node
Add range of children node of to ignoring range list,
Params
- targetNode TxtNode - target nodeArray.
- ignoredNodeTypes - are node types for ignoring
#### isIgnoredIndex(index): boolean
If the index is included in ignoring range list, return true.index should be absolute position.
Params
- index number - index value start with 0
#### isIgnoredRange(range): boolean
If the range is included in ignoring range list, return true.range should includes absolute positions.
Params
- range [number, number]
#### isIgnored(node): boolean
If the range of node is included in ignoring range list, return true.
Params
- node TxtNode - target node
A rule for textlint.
`jsCode
import { RuleHelper } from "textlint-rule-helper";
import { IgnoreNodeManager } from "textlint-rule-helper";
export default function(context) {
var helper = new RuleHelper(context);
var ignoreNodeManager = new IgnoreNodeManager();
var exports = {};
var reportingErrors = [];
exports[context.Syntax.Paragraph] = function(node) {
// Add node to ignoring list`
ignoreNodeManager.ignoreChildrenByTypes(node, [context.Syntax.Code]);
// do something
reportingErrors.push(node, ruleError);
};
exports[context.Syntax.Str] = function(node) {
// parent nodes is any one Link or Image.
if (helper.isChildNode(node, [context.Syntax.Link, context.Syntax.Image])) {
return;
}
// get Parents
var parents = helper.getParents(node);
};
exports[Syntax.Document + ":exit"] = function(node) {
reportingErrors.forEach(function(node, ruleError) {
// if the error is ignored, don't report
if (ignoreNodeManager.isIgnored(node)) {
return;
}
// report actual
});
};
return exports;
};
Params
- context TextlintRuleContent - rule context object{{ignoreNodeTypes: TxtNodeType[]}}
- options - options(report: (node: AnyTxtNode, ruleError: TextlintRuleError) => void) => any
- handler - handler should return a object
wrapReportHandler is high level API that use RuleHelper and IgnoreNodeManager.
It aim to easy to ignore some Node type for preventing unnecessary error report.
Example: ignore BlockQuote and Code node.
`jscode
import { wrapReportHandler } from "textlint-rule-helper";
const reporter = function (context) {
const { Syntax, getSource } = context;
return wrapReportHandler(context, {
ignoreNodeTypes: [Syntax.BlockQuote, Syntax.Code]
},report => { // <= wrap version of context.report
// handler should return a rule handler object
return {
Syntax.Paragraph {
const text = getSource(node);
const index = text.search("code");
/*
* Following text is matched, but it will not reported.
* ----
* This is .`
* > code
* ----
*/
if(index === -1){
return;
}
report(node, new context.RuleError(item.name, {
index
}));
}
}
});
};
export default reporter;
The Mechanism of wrapReportHandler:
- Ignore all parent nodes that are matched with ignoreNodeTypes.
- Ignore all children nodes that are matched with ignoreNodeTypes.
- wrapReportHandler create custom report function that ignore matched node
You can see real use-case of this helper library.
- textlint/rule-advanced.md at master · textlint/textlint
- textlint-rule-no-mix-dearu-desumasu
- Releases · textlint/textlint-rule-helper
```
pnpm i --frozen-lockfilewatch
pnpm run watchbuild
pnpm run buildtest
pnpm run test
1. Fork it!
2. Create your feature branch: git checkout -b my-new-featuregit commit -am 'Add some feature'
3. Commit your changes: git push origin my-new-feature`
4. Push to the branch:
5. Submit a pull request :D
MIT