2.3.1 • Published 1 year ago

textlint-rule-helper v2.3.1

Weekly downloads
29,680
License
MIT
Repository
github
Last release
1 year ago

textlint-rule-helper Actions Status: test

This is helper library for creating textlint rule.

Installation

npm install textlint-rule-helper

Support textlint version

  • ~2.2: textlint 12>=
  • ~2.3: textlint 13>=

API

class RuleHelper

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 node
  • types Array.<string> - 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

str str str
- list text

Return false

# Header
![alt text](https://example.com)
[link title](https://example.com)
> BlockQuote text
**Strong text**
[linkReference][]
[^footnote text]

use case

class IgnoreNodeManager

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, Add range of children node of node to ignoring range list,

Params

  • targetNode TxtNode - target node
  • ignoredNodeTypes Array.<string> - 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

RuleHelper and IgnoreNodeManager Example

A rule for textlint.

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 `Code` 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;
};

wrapReportHandler(context, options, handler): TextlintRuleReportHandler

Params

  • context TextlintRuleContent - rule context object
  • options {{ignoreNodeTypes: TxtNodeType[]}} - options
  • handler (report: (node: AnyTxtNode, ruleError: TextlintRuleError) => void) => any - 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.

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](node) {
               const text = getSource(node);
               const index = text.search("code");
               /*
                * Following text is matched, but it will not reported.
                * ----
                * This is `code`.
                * > 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

Use-Case

You can see real use-case of this helper library.

ChangeLog

Development

# watch
npm run watch
# build
npm run build
# test
npm run test

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

License

MIT

@infinitebrahmanuniverse/nolb-textl@everything-registry/sub-chunk-2931spellcheck-tech-word-textlint-ruletextlint-plugin-jtf-styletextlint-rule-alextextlint-rule-alive-linktextlint-rule-ja-nakaguro-or-halfwidth-space-between-katakanatextlint-rule-ja-no-mixed-periodtextlint-rule-ja-no-redundant-expressiontextlint-rule-ja-no-space-around-parenthesestextlint-rule-ja-no-space-between-full-widthtextlint-rule-ja-no-space-between-half-and-full-widthtextlint-rule-ja-space-after-exclamationtextlint-rule-ja-space-after-questiontextlint-rule-ja-space-around-codetextlint-rule-ja-space-around-linktextlint-rule-ja-space-between-half-and-full-widthtextlint-rule-bittermelontextlint-rule-common-misspellingstextlint-rule-en-capitalizationtextlint-rule-first-sentence-lengthtextlint-rule-general-novel-style-jatextlint-rule-gingertextlint-rule-hex-numbertextlint-rule-no-brackettextlint-rule-no-bracket-nightlytextlint-rule-no-dead-image-linktextlint-rule-no-dead-linktextlint-rule-no-dead-relative-linktextlint-rule-no-doubled-conjunctiontextlint-rule-no-doubled-conjunctive-particle-gatextlint-rule-max-kanji-continuous-lentextlint-rule-max-ruby-lentextlint-rule-max-tentextlint-rule-ms-japanese-style-guide-modokitextlint-rule-ja-use-comma-instead-of-toutentextlint-rule-no-doubled-joshitextlint-rule-no-dropping-the-ratextlint-rule-no-dropping-the-ra-resolatextlint-rule-aws-spellchecktextlint-rule-proofdicttextlint-rule-readable-kanji-balance-jatextlint-rule-rousseautextlint-rule-sentence-lengthtextlint-rule-spellcheck-growthbeat-wordtextlint-rule-spellcheck-tech-wordtextlint-rule-spellcheckertextlint-rule-standardizertextlint-rule-stop-wordstextlint-rule-structuretextlint-rule-preset-ja-engineering-papertextlint-rule-preset-jtf-styletextlint-rule-prhtextlint-util-zhtextlint-rule-no-start-duplicated-conjunctiontextlint-rule-no-synonyms-resolatextlint-rule-no-tbdtextlint-rule-no-todotextlint-rule-one-white-space-between-zenkaku-and-hankaku-eijitextlint-rule-prefer-tari-taritextlint-rule-no-exclamation-question-marktextlint-rule-no-hankaku-kanatextlint-rule-no-mix-dearu-desumasutextlint-rule-no-mixed-zenkaku-and-hankaku-alphabettextlint-rule-no-mixed-zenkaku-and-hankaku-numbertextlint-rule-no-nfdtextlint-rule-no-pairtextlint-rule-no-pair-nightlytextlint-rule-terminologytextlint-rule-terminology-zhtextlint-rule-use-si-unitstextlint-rule-various-nested-pairstextlint-rule-volore-getting-startedtextlint-rule-write-goodtextlint-rule-zh-half-and-full-width-brackettextstat-plugin-ja@anchan828/textlint-rule-decorators@anolilab/textlint-config@elasticpath/textlint-rule-no-dead-relative-link@proofdict/textlint-rule-proofdict@package-tucker/textlint-linkcheck-relative-ids@package-tucker/textlint-markdown-linkcheck@textstat/textstat-rule-tateish-level@textlint-ja/textlint-rule-no-dropping-i@textlint-ja/textlint-rule-no-insert-re@textlint-ja/textlint-rule-no-synonyms@textlint-rule/textlint-report-helper-for-google-preset@textlint-rule/textlint-rule-google-dashes@textlint-rule/textlint-rule-proselint@textlint-rule/textlint-rule-no-unmatched-pair@textlint-rule/textlint-rule-pattern@textlint-rule/textlint-rule-no-dead-link@textlint-rule/textlint-rule-google-sentence-spacing@textlint-rule/textlint-rule-google-slashes
2.3.1

1 year ago

2.3.0

2 years ago

2.2.3

2 years ago

2.2.2

2 years ago

2.2.4

2 years ago

2.2.1

3 years ago

2.2.0

4 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.1

6 years ago

2.0.0

9 years ago

1.2.0

9 years ago

1.1.5

9 years ago

1.1.4

9 years ago

1.1.3

10 years ago

1.1.2

10 years ago

1.1.1

10 years ago

1.1.0

10 years ago