Skip to content

Commit 64032f9

Browse files
authored
refactor: Move to TS and update sentence-splitter/to-string (#10)
* refactor: Move to TS and update sentence-splitter/to-string * Update package.json * CI: add .github/release.yml * chore: remove peerDeps * fix: move to devDeps
1 parent e94309c commit 64032f9

10 files changed

+1136
-1552
lines changed

.github/release.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
changelog:
2+
exclude:
3+
labels:
4+
- 'Type: Meta'
5+
- 'Type: Question'
6+
- 'Type: Release'
7+
8+
categories:
9+
- title: Security Fixes
10+
labels: ['Type: Security']
11+
- title: Breaking Changes
12+
labels: ['Type: Breaking Change']
13+
- title: Features
14+
labels: ['Type: Feature']
15+
- title: Bug Fixes
16+
labels: ['Type: Bug']
17+
- title: Documentation
18+
labels: ['Type: Documentation']
19+
- title: Refactoring
20+
labels: ['Type: Refactoring']
21+
- title: Testing
22+
labels: ['Type: Testing']
23+
- title: Maintenance
24+
labels: ['Type: Maintenance']
25+
- title: CI
26+
labels: ['Type: CI']
27+
- title: Dependency Updates
28+
labels: ['Type: Dependencies', "dependencies"]
29+
- title: Other Changes
30+
labels: ['*']

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# textlint-rule-en-max-word-count [![Actions Status: test](https://github.com/azu/textlint-rule-en-max-word-count/workflows/test/badge.svg)](https://github.com/azu/textlint-rule-en-max-word-count/actions?query=workflow%3A"test")
1+
# textlint-rule-en-max-word-count [![Actions Status: test](https://github.com/textlint-rule/textlint-rule-en-max-word-count/workflows/test/badge.svg)](https://github.com/textlint-rule/textlint-rule-en-max-word-count/actions?query=workflow%3A"test")
22

33
[textlint](https://github.com/textlint/textlint "textlint") rule that specify the maximum word count of a sentence.
44

package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
"name": "textlint-rule-en-max-word-count",
33
"repository": {
44
"type": "git",
5-
"url": "git+https://github.com/azu/textlint-rule-en-max-word-count.git"
5+
"url": "git+https://github.com/textlint-rule/textlint-rule-en-max-word-count.git"
66
},
77
"author": "azu",
88
"email": "[email protected]",
9-
"homepage": "https://github.com/azu/textlint-rule-en-max-word-count",
9+
"homepage": "https://github.com/textlint-rule/textlint-rule-en-max-word-count",
1010
"license": "MIT",
1111
"files": [
1212
"src/",
1313
"lib/"
1414
],
1515
"bugs": {
16-
"url": "https://github.com/azu/textlint-rule-en-max-word-count/issues"
16+
"url": "https://github.com/textlint-rule/textlint-rule-en-max-word-count/issues"
1717
},
1818
"version": "2.0.0",
1919
"description": "textlint rule that specify the maximum word count of a sentence.",
@@ -22,16 +22,16 @@
2222
"test": "test"
2323
},
2424
"dependencies": {
25-
"sentence-splitter": "^3.2.2",
26-
"textlint-tester": "^12.2.0",
27-
"textlint-util-to-string": "^3.1.1",
28-
"unist-util-map": "^1.0.1"
25+
"sentence-splitter": "^4.1.0",
26+
"textlint-util-to-string": "^3.3.2"
2927
},
3028
"devDependencies": {
31-
"textlint-scripts": "^12.2.1"
32-
},
33-
"peerDependencies": {
34-
"textlint": ">= 12.2.0"
29+
"@types/node": "^18.13.0",
30+
"@textlint/types": "^13.3.0",
31+
"textlint-scripts": "^13.3.0",
32+
"textlint-tester": "^13.3.0",
33+
"ts-node": "^10.9.1",
34+
"typescript": "^4.9.5"
3535
},
3636
"scripts": {
3737
"build": "textlint-scripts build",

src/split-words.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/split-words.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export function splitWords(string: string) {
2+
if (!string) return [];
3+
4+
const match = string.match(/"(?:\\"|[^"])+"|[^\s]+/g);
5+
if (!match) {
6+
return [];
7+
}
8+
// Remove quotes from start and end of quoted strings matched above
9+
return match
10+
.map(function (word) {
11+
return word.replace(/^\"|\"$/g, "");
12+
});
13+
}

src/textlint-rule-en-max-word-count.js

Lines changed: 0 additions & 85 deletions
This file was deleted.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Helper for converting plain text from Syntax-ed text(markdown AST
2+
// https://github.com/azu/textlint-util-to-string
3+
import { StringSource } from "textlint-util-to-string";
4+
// Helper for splitting text to sentences
5+
// https://github.com/azu/sentence-splitter
6+
import { splitAST, SentenceSplitterSyntax } from "sentence-splitter";
7+
// Helper for splitting text to words
8+
// https://github.com/timjrobinson/split-string-words
9+
import { splitWords } from "./split-words";
10+
import { TextlintRuleModule } from "@textlint/types";
11+
import type { TxtNode } from "@textlint/ast-node-types";
12+
13+
// Default options
14+
const defaultOptions = {
15+
// max count of words >
16+
max: 50
17+
};
18+
export type Options = {
19+
max?: number;
20+
}
21+
const report: TextlintRuleModule<Options> = (context, options = {}) => {
22+
const { Syntax, RuleError, report } = context;
23+
const maxWordCount = options.max ? options.max : defaultOptions.max;
24+
return {
25+
[Syntax.Paragraph](node) {
26+
const sentenceRootNode = splitAST(node);
27+
// get sentences from Paragraph
28+
const sentences = sentenceRootNode.children.filter(node => {
29+
return node.type === SentenceSplitterSyntax.Sentence;
30+
});
31+
sentences.forEach(sentence => {
32+
const source = new StringSource(sentence, {
33+
replacer({ node, maskValue }) {
34+
// replace code with dummy code because code will include any words, but we don't want to count it
35+
if (node.type === Syntax.Code) {
36+
return maskValue("_");
37+
}
38+
return;
39+
},
40+
});
41+
const sentenceText = source.toString();
42+
/* sentence object is a node
43+
{
44+
type: "Sentence",
45+
raw: text,
46+
value: text,
47+
loc: loc,
48+
range: range
49+
};
50+
*/
51+
// words in a sentence
52+
const words = splitWords(sentenceText);
53+
// over count of word, then report error
54+
if (words.length > maxWordCount) {
55+
// get original index value of sentence.loc.start
56+
const sentenceFragment = `${words.slice(0, 3).join(' ')} ...`;
57+
const ruleError = new RuleError(`Maximum word count (${maxWordCount}) exceeded (${words.length}) by "${sentenceFragment}".`);
58+
report(sentence as TxtNode, ruleError);
59+
}
60+
});
61+
}
62+
};
63+
}
64+
65+
export default report

test/textlint-rule-en-max-word-count-test.js renamed to test/textlint-rule-en-max-word-count-test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import TextLintTester from "textlint-tester"
2-
32
const tester = new TextLintTester();
43
// rule
54
import rule from "../src/textlint-rule-en-max-word-count";

tsconfig.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"compilerOptions": {
3+
/* Basic Options */
4+
"module": "commonjs",
5+
"moduleResolution": "node",
6+
"esModuleInterop": true,
7+
"newLine": "LF",
8+
"noEmit": true,
9+
"target": "ES2018",
10+
"sourceMap": true,
11+
"declaration": true,
12+
"declarationMap": true,
13+
"jsx": "preserve",
14+
"lib": [
15+
"esnext"
16+
],
17+
"strict": true,
18+
"noUnusedLocals": true,
19+
"noUnusedParameters": true,
20+
"noImplicitReturns": true,
21+
"noFallthroughCasesInSwitch": true
22+
},
23+
"include": [
24+
"**/*"
25+
],
26+
"exclude": [
27+
".git",
28+
"node_modules"
29+
]
30+
}

0 commit comments

Comments
 (0)