Skip to content

Latest commit

 

History

History
225 lines (178 loc) · 5.72 KB

README.md

File metadata and controls

225 lines (178 loc) · 5.72 KB

@textlint-rule/textlint-rule-pattern

A textlint rule that checks by RegExp patterns.

Features

  • Support User Defined Patterns
  • Support RegExp patterns
  • Support Replacement texts

Install

Install with npm:

npm install @textlint-rule/textlint-rule-pattern

Usage

Via .textlintrc(Recommended)

{
    "rules": {
        "@textlint-rule/pattern": {
          "patterns": [
            {
              "message": "No todo",
              "pattern": "/TODO:/i"
            },
            {
              "message": "Must to use YYYY-MM-DD instead of YYYY/MM/DD",
              "pattern": "/(\\d{4})/(\\d{2})/(\\d{2})/i",
              "replace": "$1-$2-$3",
              "allows": ["/1000/01/01/"],
              "allowNodeTypes": ["BlockQuote"]
            }
          ]
        }
    }
}

Options

  • patterns: an array of pattern object
{
    "rules": {
        "@textlint-rule/pattern": {
          "patterns": [
            /* Your rules is here! */
          ]
        }
    }
}

Pattern

The pattern object has the following properties.

export type Pattern = {
    /**
     * This error message will be shown when match the pattern
     */
    message: string;
    /**
     * Match pattern string.
     * You can use RegExp-like string.
     * https://github.com/textlint/regexp-string-matcher#regexp-like-string
     */
    pattern: string;
    /**
     * Replace string
     * You can use capture pattern like $1, $2. ($0 will be ignored)
     */
    replace?: string;
    /**
     * An array of excludes pattern.
     * If the text is matched this pattern, suppress the error.
     * You can use RegExp-like string
     * https://github.com/textlint/regexp-string-matcher#regexp-like-string
     */
    allows?: string[];
    /**
     * An array for excludes node type.
     * If the text is in the node type, suppress the error.
     * https://github.com/textlint/textlint/blob/master/docs/txtnode.md#type
     * For example, if you want to ignore the text in block quote and link
     * "allowNodeTypes": ["Link", "BlockQuote"]
     */
    allowNodeTypes?: string[];

    /**
     * This rule ignore Code and CodeBlock by default.
     * If you want to check the code, please put this true
     */
    forceCode?: boolean;
};

RegExp-like String

This textlint rule use RegExp-like string for option value. :memo: g(global) flag and u(unicode) is added by default.

Input Ouput Note
"str" /str/gu convert string to regexp with global
"/str/" /str/gu
"/str/g" /str/gu Duplicated g is just ignored
"/str/i" /str/igu
"/str/u" /str/ug
"/str/m" /str/mgu
"/str/y" /str/ygu
--- --- ---
"/\\d+/" /\d+/gu You should escape meta character like \d
"/(\\d+)/" /\d+/gu You can use capture. replace $1 with the capture

For more information, please see textlint/regexp-string-matcher README.

Examples

Match by RegExp

If you want to found TODO: text, you can write following:

{
    "rules": {
        "@textlint-rule/pattern": {
          "patterns": [
            {
              "message": "No todo",
              "pattern": "/TODO:/i"
            }
          ]
        }
    }
}

Replace the match

If you want to replace YYYY/DD/MM to YYYY-DD-MM format, you can write following:

e.g. 2000/01/01 will be 2000-01-01 by textlint --fix.

📝 replace mark is starts with $1.

{
    "rules": {
        "@textlint-rule/pattern": {
          "patterns": [
            {
              "message": "Must to use YYYY-MM-DD instead of YYYY/MM/DD",
              "pattern": "/(\\d{4})/(\\d{2})/(\\d{2})/i",
              "replace": "$1-$2-$3"
            }
          ]
        }
    }
}

Check code texts

If you want to check text in a CodeBlock, you need to enable forceCode flag.

{
    "rules": {
        "@textlint-rule/pattern": {
          "patterns": [
            {
              "message": "No todo in code",
              "pattern": "/TODO \\[Issue #\\d+\\]:/i",
              "forceCode": true
            }
          ]
        }
    }
}

Changelog

See Releases page.

Related

Running tests

Install devDependencies and Run npm test:

npm test

Contributing

Pull requests and stars are always welcome.

For bugs and feature requests, please create an issue.

  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

Author

License

MIT © azu