diff --git a/README.md b/README.md index de10026..f83aaf9 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,31 @@ Via CLI textlint --rule @textlint-rule/no-duplicate-abbr README.md ``` +## Options + +```ts +export type Options = { + /** + * A list for ignoring Acronyms or Abbreviations. + * e.g. you can allow "NPO organization" by following setting. + * "allowAbbrList": ["NPO"] + */ + allowAbbrList: string[]; +} +``` + +Example setting: + +```json5 +{ + "rules": { + "@textlint-rule/no-duplicate-abbr": { + "allowAbbrList": ["NPO"] // Allow to use "NPO organization" + } + } +} +``` + ## Changelog See [Releases page](https://github.com/textlint-rule/textlint-rule-no-duplicate-abbr/releases). diff --git a/src/textlint-rule-no-duplicate-abbr.ts b/src/textlint-rule-no-duplicate-abbr.ts index 65c829b..95de145 100644 --- a/src/textlint-rule-no-duplicate-abbr.ts +++ b/src/textlint-rule-no-duplicate-abbr.ts @@ -1,16 +1,29 @@ import { TextlintRuleReporter } from "@textlint/types"; import dataset from "./dataset.json"; -const reporter: TextlintRuleReporter = (context) => { +export type Options = { + /** + * A list for ignoring Acronyms or Abbreviations. + * e.g. you can allow "NPO organization" by following setting. + * "allowAbbrList": ["NPO"] + */ + allowAbbrList: string[]; +}; +const reporter: TextlintRuleReporter = (context, options) => { const { Syntax, getSource, report, RuleError } = context; const abbrDataSet = new Map( dataset as [string, { suffixes: string[]; definition: string }][] ); + const allowAbbrList = options?.allowAbbrList ?? []; return { [Syntax.Str](node) { const source = getSource(node); const words = source.split(/\b/); words.forEach((word, index) => { + // Skip the word + if (allowAbbrList.includes(word)) { + return; + } const hasSpace = /\s/.test(words[index + 1]); const nextWord = hasSpace ? words[index + 2] : words[index + 1]; if (!nextWord) { @@ -31,7 +44,7 @@ const reporter: TextlintRuleReporter = (context) => { report( node, new RuleError( - `"${word}${spacer}${nextWord}" uses duplicated suffix word. "${word}" stands for "${matchData.definition}".`, + `"${word}${spacer}${nextWord}" has duplicated suffix word. "${word}" stands for "${matchData.definition}".`, { index: startIndexOfWord } diff --git a/test/textlint-rule-no-duplicate-abbr.test.ts b/test/textlint-rule-no-duplicate-abbr.test.ts index 20420e2..1b94a68 100644 --- a/test/textlint-rule-no-duplicate-abbr.test.ts +++ b/test/textlint-rule-no-duplicate-abbr.test.ts @@ -1,14 +1,24 @@ import TextLintTester from "textlint-tester"; import rule from "../src/textlint-rule-no-duplicate-abbr"; + const tester = new TextLintTester(); tester.run("textlint-rule-no-duplicate-abbr", rule, { - valid: ["BGP is Border Gateway Protocol."], + valid: ["BGP is Border Gateway Protocol.", "npm module is installed by Node package manager"], invalid: [ + { + text: "NPO organization is duplicated", + errors: [ + { + message: `"NPO organization" has duplicated suffix word. "NPO" stands for "Non-Profit Organization".`, + index: 0 + } + ] + }, { text: "DAO object is Data Access Object", errors: [ { - message: `"DAO object" uses duplicated suffix word. "DAO" stands for "Data Access Object".`, + message: `"DAO object" has duplicated suffix word. "DAO" stands for "Data Access Object".`, index: 0 } ] @@ -17,7 +27,7 @@ tester.run("textlint-rule-no-duplicate-abbr", rule, { text: "This is BGP protocol", errors: [ { - message: `"BGP protocol" uses duplicated suffix word. "BGP" stands for "Border Gateway Protocol".`, + message: `"BGP protocol" has duplicated suffix word. "BGP" stands for "Border Gateway Protocol".`, index: 8 } ] @@ -26,7 +36,7 @@ tester.run("textlint-rule-no-duplicate-abbr", rule, { text: "This is BGP protocol", errors: [ { - message: `"BGP protocol" uses duplicated suffix word. "BGP" stands for "Border Gateway Protocol".`, + message: `"BGP protocol" has duplicated suffix word. "BGP" stands for "Border Gateway Protocol".`, index: 8 } ] @@ -35,7 +45,7 @@ tester.run("textlint-rule-no-duplicate-abbr", rule, { text: "これはBGPプロトコルです。", errors: [ { - message: `"BGPプロトコルです。" uses duplicated suffix word. "BGP" stands for "Border Gateway Protocol".`, + message: `"BGPプロトコルです。" has duplicated suffix word. "BGP" stands for "Border Gateway Protocol".`, index: 3 } ]