Skip to content

Commit

Permalink
skip option to ignore blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
aborazmeh committed Jul 11, 2024
1 parent 03a8506 commit 0db049a
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,17 @@ Via `.textlintrc.json`(Recommended)
```json
{
"rules": {
"no-kasheeda": true
"no-kasheeda": {
"skip": ["Header"]
}
}
}
```

You can use skip array to define ignored blocks.

# مـقـدمـة

Via CLI

```
Expand Down
5 changes: 4 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,8 @@
"bugs": {
"url": "https://github.com/aborazmeh/textlint-rule-no-kasheeda/issues"
},
"homepage": "https://github.com/aborazmeh/textlint-rule-no-kasheeda#readme"
"homepage": "https://github.com/aborazmeh/textlint-rule-no-kasheeda#readme",
"dependencies": {
"@textlint/ast-node-types": "^14.0.4"
}
}
39 changes: 38 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,47 @@
import type { TextlintRuleModule } from "@textlint/types";
import { TxtNode } from "@textlint/ast-node-types";

const report: TextlintRuleModule = (context) => {
export interface Options {
skip?: string[];
}

function getParents(node: TxtNode) {
const result = [];
let parent = node.parent;
while (parent != null) {
result.push(parent);
parent = parent.parent;
}
return result;
}

function isChildNode(node: TxtNode, types: string[]) {
const parents = getParents(node);
const parentsTypes = parents.map(function (parent) {
return parent.type;
});
return types.some(function (type) {
return parentsTypes.some(function (parentType: string) {
return parentType === type;
});
});
}

const report: TextlintRuleModule<Options> = (context, options = {}) => {
const { Syntax, RuleError, fixer, report, getSource, locator } = context;
const skip = options.skip ?? [];
return {
[Syntax.Str](node) {
// "Str" node
if (
isChildNode(
node,
skip.map((rule) => Syntax[rule as keyof typeof Syntax])
)
) {
return;
}

const text = getSource(node); // Get text
const matches = text.matchAll(/ـ+/g);
const notLetter = (index: number) => /[^\p{Letter}]/u.test(text[index]);
Expand Down
25 changes: 25 additions & 0 deletions test/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ tester.run("rule", rule, {
"العام السابق 1445 هـ، والعام الحالي 1446 هـ",
"خَرَجتَ من المُنى مثلَ الـ ـحُمَيِّرِ غَرَّهُ وَتِدُه",
`وعندما جاء إلى الـ«بيت» دخل الـ"غرفة" ولم يجد أحداً`,
" this `كــود` is acceptable by default",
{
text: "# مـقـدمـة",
options: {
skip: ["Header"],
},
},
],
invalid: [
{
Expand Down Expand Up @@ -40,5 +47,23 @@ tester.run("rule", rule, {
},
],
},
{
text: "# مـقـدمـة",
output: "# مقدمة",
options: {
skip: [],
},
errors: [
{
range: [3, 4],
},
{
range: [5, 6],
},
{
range: [8, 9],
},
],
},
],
});

0 comments on commit 0db049a

Please sign in to comment.