@termdock/twlint 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +185 -0
- package/TRADEMARK.md +39 -0
- package/bin/twlint +6 -0
- package/dist/cli.d.ts +10 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +142 -0
- package/dist/cli.js.map +1 -0
- package/dist/core/config-loader.d.ts +3 -0
- package/dist/core/config-loader.d.ts.map +1 -0
- package/dist/core/config-loader.js +54 -0
- package/dist/core/config-loader.js.map +1 -0
- package/dist/core/config-schema.d.ts +12 -0
- package/dist/core/config-schema.d.ts.map +1 -0
- package/dist/core/config-schema.js +64 -0
- package/dist/core/config-schema.js.map +1 -0
- package/dist/core/dictionary-manager.d.ts +14 -0
- package/dist/core/dictionary-manager.d.ts.map +1 -0
- package/dist/core/dictionary-manager.js +94 -0
- package/dist/core/dictionary-manager.js.map +1 -0
- package/dist/core/linter.d.ts +31 -0
- package/dist/core/linter.d.ts.map +1 -0
- package/dist/core/linter.js +182 -0
- package/dist/core/linter.js.map +1 -0
- package/dist/core/matching/match-strategies.d.ts +26 -0
- package/dist/core/matching/match-strategies.d.ts.map +1 -0
- package/dist/core/matching/match-strategies.js +170 -0
- package/dist/core/matching/match-strategies.js.map +1 -0
- package/dist/core/position-mapper.d.ts +31 -0
- package/dist/core/position-mapper.d.ts.map +1 -0
- package/dist/core/position-mapper.js +101 -0
- package/dist/core/position-mapper.js.map +1 -0
- package/dist/core/rules/context-sensitive.d.ts +17 -0
- package/dist/core/rules/context-sensitive.d.ts.map +1 -0
- package/dist/core/rules/context-sensitive.js +154 -0
- package/dist/core/rules/context-sensitive.js.map +1 -0
- package/dist/core/rules/mainland-terms.d.ts +11 -0
- package/dist/core/rules/mainland-terms.d.ts.map +1 -0
- package/dist/core/rules/mainland-terms.js +45 -0
- package/dist/core/rules/mainland-terms.js.map +1 -0
- package/dist/core/rules/simplified-chars.d.ts +10 -0
- package/dist/core/rules/simplified-chars.d.ts.map +1 -0
- package/dist/core/rules/simplified-chars.js +51 -0
- package/dist/core/rules/simplified-chars.js.map +1 -0
- package/dist/dictionaries/academic.json +141 -0
- package/dist/dictionaries/core.json +1491 -0
- package/dist/dictionaries/extended.json +141 -0
- package/dist/dictionaries/index.json +21 -0
- package/dist/formatters/json.d.ts +5 -0
- package/dist/formatters/json.d.ts.map +1 -0
- package/dist/formatters/json.js +18 -0
- package/dist/formatters/json.js.map +1 -0
- package/dist/formatters/stylish.d.ts +5 -0
- package/dist/formatters/stylish.d.ts.map +1 -0
- package/dist/formatters/stylish.js +45 -0
- package/dist/formatters/stylish.js.map +1 -0
- package/dist/types.d.ts +73 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +77 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity granting the License.
|
|
13
|
+
|
|
14
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
15
|
+
other entities that control, are controlled by, or are under common
|
|
16
|
+
control with that entity. For the purposes of this definition,
|
|
17
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
18
|
+
direction or management of such entity, whether by contract or
|
|
19
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
20
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
21
|
+
|
|
22
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
23
|
+
exercising permissions granted by this License.
|
|
24
|
+
|
|
25
|
+
"Source" shall mean the preferred form for making modifications,
|
|
26
|
+
including but not limited to software source code, documentation
|
|
27
|
+
source, and configuration files.
|
|
28
|
+
|
|
29
|
+
"Object" form shall mean any form resulting from mechanical
|
|
30
|
+
transformation or translation of a Source form, including but
|
|
31
|
+
not limited to compiled object code, generated documentation,
|
|
32
|
+
and conversions to other media types.
|
|
33
|
+
|
|
34
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
35
|
+
Object form, made available under the License, as indicated by a
|
|
36
|
+
copyright notice that is included in or attached to the work
|
|
37
|
+
(which shall not be construed as modifying the License).
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based upon (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
43
|
+
of this License, Derivative Works shall not include works that remain
|
|
44
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
45
|
+
the Work and derivative works thereof.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean any work of authorship, including
|
|
48
|
+
the original version of the Work and any modifications or additions
|
|
49
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
50
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
51
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
52
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
53
|
+
means any form of electronic, verbal, or written communication sent
|
|
54
|
+
to the Licensor or its representatives, including but not limited to
|
|
55
|
+
communication on electronic mailing lists, source code control
|
|
56
|
+
systems, and issue tracking systems that are managed by, or on behalf
|
|
57
|
+
of, the Licensor for the purpose of discussing and improving the Work,
|
|
58
|
+
but excluding communication that is conspicuously marked or otherwise
|
|
59
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
60
|
+
|
|
61
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
62
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
63
|
+
subsequently incorporated within the Work.
|
|
64
|
+
|
|
65
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
66
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
67
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
68
|
+
copyright license to use, reproduce, modify, merge, publish,
|
|
69
|
+
distribute, sublicense, and/or sell copies of the Work, and to
|
|
70
|
+
permit persons to whom the Work is furnished to do so, subject to
|
|
71
|
+
the following conditions:
|
|
72
|
+
|
|
73
|
+
The above copyright notice and this permission notice shall be
|
|
74
|
+
included in all copies or substantial portions of the Work.
|
|
75
|
+
|
|
76
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
77
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
78
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
79
|
+
(except as stated in this section) patent license to make, have made,
|
|
80
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
81
|
+
where such license applies only to those patent claims licensable
|
|
82
|
+
by such Contributor that are necessarily infringed by their
|
|
83
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
84
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
85
|
+
institute patent litigation against any entity (including a
|
|
86
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
87
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
88
|
+
or contributory patent infringement, then any patent licenses
|
|
89
|
+
granted to You under this License for that Work shall terminate
|
|
90
|
+
as of the date such litigation is filed.
|
|
91
|
+
|
|
92
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
93
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
94
|
+
modifications, and in Source or Object form, provided that You
|
|
95
|
+
meet the following conditions:
|
|
96
|
+
|
|
97
|
+
(a) You must give any other recipients of the Work or
|
|
98
|
+
Derivative Works a copy of this License;
|
|
99
|
+
|
|
100
|
+
(b) You must cause any modified files to carry prominent notices
|
|
101
|
+
stating that You changed the files;
|
|
102
|
+
|
|
103
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
104
|
+
that You distribute, all copyright, patent, trademark, and
|
|
105
|
+
attribution notices from the Source form of the Work,
|
|
106
|
+
excluding those notices that do not pertain to any part of
|
|
107
|
+
the Derivative Works; and
|
|
108
|
+
|
|
109
|
+
(d) If the Work includes a "NOTICE" file as part of its
|
|
110
|
+
distribution, then any Derivative Works that You distribute must
|
|
111
|
+
include a readable copy of the attribution notices contained
|
|
112
|
+
within such NOTICE file, excluding those notices that do not
|
|
113
|
+
pertain to any part of the Derivative Works, in at least one
|
|
114
|
+
of the following places: within a NOTICE file distributed
|
|
115
|
+
as part of the Derivative Works; within the Source form or
|
|
116
|
+
documentation, if provided along with the Derivative Works; or,
|
|
117
|
+
within a display generated by the Derivative Works, if and
|
|
118
|
+
wherever such third-party notices normally appear. The contents
|
|
119
|
+
of the NOTICE file are for informational purposes only and
|
|
120
|
+
do not modify the License. You may add Your own attribution
|
|
121
|
+
notices within Derivative Works that You distribute, alongside
|
|
122
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
123
|
+
that such additional attribution notices cannot be construed
|
|
124
|
+
as modifying the License.
|
|
125
|
+
|
|
126
|
+
You may add Your own copyright notice and may provide additional or
|
|
127
|
+
different license terms and conditions for use, reproduction, or
|
|
128
|
+
distribution of Your Derivative Works as a whole, provided Your use,
|
|
129
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
130
|
+
the conditions stated in this License.
|
|
131
|
+
|
|
132
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
133
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
134
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
135
|
+
this License, without any additional terms or conditions.
|
|
136
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
137
|
+
the terms of any separate contributor license agreement you may have
|
|
138
|
+
executed with Licensor regarding such Contributions.
|
|
139
|
+
|
|
140
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
141
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
142
|
+
except as required for reasonable and customary use in describing the
|
|
143
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
144
|
+
|
|
145
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
146
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
147
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
148
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
149
|
+
implied, including, without limitation, any warranties or conditions
|
|
150
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
151
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
152
|
+
appropriateness of using or redistributing the Work and assume any
|
|
153
|
+
risks associated with Your exercise of permissions under this License.
|
|
154
|
+
|
|
155
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
156
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
157
|
+
unless required by applicable law (such as deliberate and grossly
|
|
158
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
159
|
+
liable to You for damages, including any direct, indirect, special,
|
|
160
|
+
incidental, or consequential damages of any character arising as a
|
|
161
|
+
result of this License or out of the use or inability to use the
|
|
162
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
163
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
164
|
+
other commercial damages or losses), even if such Contributor
|
|
165
|
+
has been advised of the possibility of such damages.
|
|
166
|
+
|
|
167
|
+
9. Accepting Warranty or Support. You may choose to offer, and to
|
|
168
|
+
charge a fee for, warranty, support, indemnity, or other liability
|
|
169
|
+
obligations and/or rights consistent with this License. However, in
|
|
170
|
+
accepting such obligations, You may act only on Your own behalf and on
|
|
171
|
+
Your sole responsibility, not on behalf of any other Contributor, and
|
|
172
|
+
only if You agree to indemnify, defend, and hold each Contributor
|
|
173
|
+
harmless for any liability incurred by, or claims asserted against,
|
|
174
|
+
such Contributor by reason of your accepting any such warranty or support.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same page as the copyright notice for easier identification within
|
|
187
|
+
third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2025 Huang Chung YI
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
# TWLint
|
|
2
|
+
|
|
3
|
+
> 檢測簡體中文用語並建議臺灣繁體替代方案的 CLI 工具
|
|
4
|
+
|
|
5
|
+
TWLint 幫助臺灣開發者和內容創作者檢測並修正意外使用的大陸用語,確保文本符合臺灣慣用的表達方式。
|
|
6
|
+
|
|
7
|
+
## 特色功能
|
|
8
|
+
|
|
9
|
+
- **自動檢測簡體字**:使用 OpenCC 引擎進行簡繁轉換
|
|
10
|
+
- **大陸用語檢測**:識別大陸特有用語並提供臺灣替代建議
|
|
11
|
+
- **自動修復**:支援 `--fix` 參數自動修正可修復的問題
|
|
12
|
+
- **多種輸出格式**:支援 stylish、json 等格式
|
|
13
|
+
- **靈活配置**:支援專案級配置檔案
|
|
14
|
+
|
|
15
|
+
## 安裝
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install -g twlint
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## 快速開始
|
|
22
|
+
|
|
23
|
+
### 基本檢查
|
|
24
|
+
```bash
|
|
25
|
+
twlint check README.md
|
|
26
|
+
twlint check "src/**/*.md"
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### 自動修復
|
|
30
|
+
```bash
|
|
31
|
+
twlint check README.md --fix
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### 初始化配置檔案
|
|
35
|
+
```bash
|
|
36
|
+
twlint init
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## 使用範例
|
|
40
|
+
|
|
41
|
+
假設有以下檔案內容(包含大陸用語):
|
|
42
|
+
```markdown
|
|
43
|
+
# 项目介绍
|
|
44
|
+
这个软件的质量很好,我们使用了先进的算法。
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
執行檢查:
|
|
48
|
+
```bash
|
|
49
|
+
$ twlint check example.md
|
|
50
|
+
|
|
51
|
+
example.md
|
|
52
|
+
1:3 error 簡體字 '项' 建議使用繁體字 '項' simplified-chars
|
|
53
|
+
1:4 error 簡體字 '目' 建議使用繁體字 '目' simplified-chars
|
|
54
|
+
1:5 error 簡體字 '介' 建議使用繁體字 '介' simplified-chars
|
|
55
|
+
1:6 error 簡體字 '绍' 建議使用繁體字 '紹' simplified-chars
|
|
56
|
+
2:1 error 簡體字 '这' 建議使用繁體字 '這' simplified-chars
|
|
57
|
+
2:2 error 簡體字 '个' 建議使用繁體字 '個' simplified-chars
|
|
58
|
+
2:3 error 簡體字 '软' 建議使用繁體字 '軟' simplified-chars
|
|
59
|
+
2:4 error 簡體字 '件' 建議使用繁體字 '件' simplified-chars
|
|
60
|
+
1:3 warning 大陸用語 '項目' 建議使用臺灣用語 '專案' mainland-terms
|
|
61
|
+
2:3 warning 大陸用語 '軟件' 建議使用臺灣用語 '軟體' mainland-terms
|
|
62
|
+
2:7 warning 大陸用語 '質量' 建議使用臺灣用語 '品質' mainland-terms
|
|
63
|
+
2:18 warning 大陸用語 '算法' 建議使用臺灣用語 '演算法' mainland-terms
|
|
64
|
+
|
|
65
|
+
✖ 12 problems (8 errors, 4 warnings)
|
|
66
|
+
12 problems potentially fixable with the `--fix` option.
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
自動修復後:
|
|
70
|
+
```bash
|
|
71
|
+
$ twlint check example.md --fix
|
|
72
|
+
|
|
73
|
+
✓ Fixed: example.md
|
|
74
|
+
|
|
75
|
+
# 專案介紹
|
|
76
|
+
這個軟體的品質很好,我們使用了先進的演算法。
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## 配置
|
|
80
|
+
|
|
81
|
+
### 專案配置檔案
|
|
82
|
+
|
|
83
|
+
建立 `twlint.config.js`:
|
|
84
|
+
|
|
85
|
+
```javascript
|
|
86
|
+
export default [
|
|
87
|
+
{
|
|
88
|
+
// 檢查的檔案類型
|
|
89
|
+
files: ["**/*.md", "**/*.txt"],
|
|
90
|
+
|
|
91
|
+
// 使用的詞庫
|
|
92
|
+
dictionaries: ["core"],
|
|
93
|
+
|
|
94
|
+
// 規則配置
|
|
95
|
+
rules: {
|
|
96
|
+
"simplified-chars": "error", // 簡體字檢測(自動修復)
|
|
97
|
+
"mainland-terms": "warning" // 大陸用語檢測(提供建議)
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
]
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### CLI 選項
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
twlint check <files...> [options]
|
|
107
|
+
|
|
108
|
+
Options:
|
|
109
|
+
--fix 自動修復可修復的問題
|
|
110
|
+
--format <type> 輸出格式 (stylish, json)
|
|
111
|
+
--dict <names...> 指定使用的詞庫
|
|
112
|
+
--config <path> 配置檔案路徑
|
|
113
|
+
--verbose 顯示詳細輸出
|
|
114
|
+
--deep 深度模式(載入所有詞庫)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## 詞庫
|
|
118
|
+
|
|
119
|
+
TWLint 包含以下內建詞庫:
|
|
120
|
+
|
|
121
|
+
- **core**:核心技術用語(預設載入)
|
|
122
|
+
- **academic**:學術用語
|
|
123
|
+
- **extended**:擴充功能用語集
|
|
124
|
+
|
|
125
|
+
使用特定詞庫:
|
|
126
|
+
```bash
|
|
127
|
+
twlint check file.md --dict core academic
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## 開發
|
|
131
|
+
|
|
132
|
+
### 環境需求
|
|
133
|
+
- Node.js 18+
|
|
134
|
+
- TypeScript 5.0+
|
|
135
|
+
|
|
136
|
+
### 本機開發
|
|
137
|
+
```bash
|
|
138
|
+
git clone https://github.com/username/twlint.git
|
|
139
|
+
cd twlint
|
|
140
|
+
npm install
|
|
141
|
+
npm run build
|
|
142
|
+
npm run test
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### 建構詞庫
|
|
146
|
+
```bash
|
|
147
|
+
npm run dict:build
|
|
148
|
+
npm run dict:validate
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## 規則說明
|
|
152
|
+
|
|
153
|
+
### simplified-chars
|
|
154
|
+
檢測簡體字並自動轉換為繁體字。
|
|
155
|
+
- **嚴重度**:error
|
|
156
|
+
- **可修復**:是
|
|
157
|
+
|
|
158
|
+
### mainland-terms
|
|
159
|
+
檢測大陸特有用語並建議臺灣慣用詞彙。
|
|
160
|
+
- **嚴重度**:warning
|
|
161
|
+
- **可修復**:部分
|
|
162
|
+
|
|
163
|
+
## 授權
|
|
164
|
+
|
|
165
|
+
Apache License 2.0
|
|
166
|
+
|
|
167
|
+
詳見 [LICENSE](LICENSE) 檔案。
|
|
168
|
+
|
|
169
|
+
## 商標
|
|
170
|
+
|
|
171
|
+
"TWLint" 名稱和標誌為本專案商標。使用前請參閱 [商標使用指南](TRADEMARK.md)。
|
|
172
|
+
|
|
173
|
+
## 貢獻
|
|
174
|
+
|
|
175
|
+
歡迎提交 Pull Request 或回報問題!
|
|
176
|
+
|
|
177
|
+
1. Fork 本專案
|
|
178
|
+
2. 建立功能分支 (`git checkout -b feature/amazing-feature`)
|
|
179
|
+
3. 提交變更 (`git commit -m 'Add amazing feature'`)
|
|
180
|
+
4. 推送到分支 (`git push origin feature/amazing-feature`)
|
|
181
|
+
5. 開啟 Pull Request
|
|
182
|
+
|
|
183
|
+
---
|
|
184
|
+
|
|
185
|
+
**讓我們一起維護臺灣中文的純正性!**
|
package/TRADEMARK.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# TWLint 商標使用指南
|
|
2
|
+
|
|
3
|
+
## 商標聲明
|
|
4
|
+
|
|
5
|
+
"TWLint" 名稱和標誌是 TWLint 專案的商標。© 2025 Huang Chung YI
|
|
6
|
+
|
|
7
|
+
## 允許使用
|
|
8
|
+
|
|
9
|
+
以下情況**無需許可**即可使用 TWLint 商標:
|
|
10
|
+
|
|
11
|
+
- 在技術文件中提及 TWLint
|
|
12
|
+
- 描述與 TWLint 的相容性
|
|
13
|
+
- 新聞報導和評論
|
|
14
|
+
- 非商業性教學和研究
|
|
15
|
+
|
|
16
|
+
## 需要許可的使用
|
|
17
|
+
|
|
18
|
+
以下情況**必須獲得書面許可**:
|
|
19
|
+
|
|
20
|
+
- 在產品名稱中使用 "TWLint"
|
|
21
|
+
- 將 TWLint 商標用於商業產品或服務
|
|
22
|
+
- 創建可能造成混淆的衍生名稱
|
|
23
|
+
- 在域名中使用 TWLint
|
|
24
|
+
|
|
25
|
+
## 禁止使用
|
|
26
|
+
|
|
27
|
+
以下使用方式**明確禁止**:
|
|
28
|
+
|
|
29
|
+
- 暗示獲得 TWLint 專案官方認可
|
|
30
|
+
- 以可能損害 TWLint 聲譽的方式使用
|
|
31
|
+
- 在違法或不當內容中使用
|
|
32
|
+
|
|
33
|
+
## 聯絡方式
|
|
34
|
+
|
|
35
|
+
商標使用許可請聯絡:[你的聯絡方式]
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
本指南遵循 [Model Trademark Guidelines](https://www.linuxfoundation.org/trademark-usage/) 制定。
|
package/bin/twlint
ADDED
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AAWA,MAAM,WAAW,UAAU;IACzB,GAAG,CAAC,EAAE,OAAO,CAAA;IACb,IAAI,CAAC,EAAE,OAAO,CAAA;IACd,MAAM,CAAC,EAAE,SAAS,GAAG,MAAM,CAAA;IAC3B,IAAI,CAAC,EAAE,MAAM,EAAE,CAAA;IACf,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB"}
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { Command } from 'commander';
|
|
3
|
+
import chalk from 'chalk';
|
|
4
|
+
import { writeFile, access } from 'fs/promises';
|
|
5
|
+
import { TWLinter } from './core/linter.js';
|
|
6
|
+
import { loadConfig } from './core/config-loader.js';
|
|
7
|
+
import { createSampleConfig } from './core/config-schema.js';
|
|
8
|
+
import { StylishFormatter } from './formatters/stylish.js';
|
|
9
|
+
import { JsonFormatter } from './formatters/json.js';
|
|
10
|
+
async function performCheck(linter, files, options) {
|
|
11
|
+
const results = await linter.lintFiles(files);
|
|
12
|
+
const formatter = options.format === 'json'
|
|
13
|
+
? new JsonFormatter()
|
|
14
|
+
: new StylishFormatter();
|
|
15
|
+
const output = formatter.format(results);
|
|
16
|
+
console.log(output);
|
|
17
|
+
const errorCount = results.reduce((sum, result) => sum + result.messages.filter(msg => msg.severity === 'error').length, 0);
|
|
18
|
+
if (errorCount > 0) {
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
async function performAutoFix(linter, files, options) {
|
|
23
|
+
const { readFile } = await import('fs/promises');
|
|
24
|
+
let totalFixed = 0;
|
|
25
|
+
for (const filePath of files) {
|
|
26
|
+
try {
|
|
27
|
+
const originalContent = await readFile(filePath, 'utf-8');
|
|
28
|
+
const fixedContent = await linter.fixText(originalContent);
|
|
29
|
+
if (originalContent !== fixedContent) {
|
|
30
|
+
await writeFile(filePath, fixedContent, 'utf-8');
|
|
31
|
+
totalFixed++;
|
|
32
|
+
if (options.verbose) {
|
|
33
|
+
console.log(chalk.green(`✓ Fixed: ${filePath}`));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else if (options.verbose) {
|
|
37
|
+
console.log(chalk.dim(`○ No changes: ${filePath}`));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
42
|
+
console.error(chalk.red(`✗ Error fixing ${filePath}:`), message);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
console.log(chalk.green(`\n🎉 Fixed ${totalFixed} file(s)`));
|
|
46
|
+
// 修復後重新檢查,顯示剩餘問題
|
|
47
|
+
if (totalFixed > 0) {
|
|
48
|
+
console.log(chalk.dim('\n--- Remaining issues after fix ---'));
|
|
49
|
+
await performCheck(linter, files, { ...options, fix: false });
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async function performInit(force) {
|
|
53
|
+
const configFile = 'twlint.config.js';
|
|
54
|
+
// 檢查是否已存在配置檔案
|
|
55
|
+
try {
|
|
56
|
+
await access(configFile);
|
|
57
|
+
if (!force) {
|
|
58
|
+
console.log(chalk.yellow(`⚠️ Configuration file ${configFile} already exists.`));
|
|
59
|
+
console.log(chalk.dim('Use --force to overwrite it.'));
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
// 檔案不存在,可以繼續建立
|
|
65
|
+
}
|
|
66
|
+
// 建立配置檔案
|
|
67
|
+
const configContent = createSampleConfig();
|
|
68
|
+
await writeFile(configFile, configContent, 'utf-8');
|
|
69
|
+
console.log(chalk.green(`✅ Created ${configFile}`));
|
|
70
|
+
console.log(chalk.dim('\nNext steps:'));
|
|
71
|
+
console.log(chalk.dim('1. Customize the configuration to fit your needs'));
|
|
72
|
+
console.log(chalk.dim('2. Run: twlint check **/*.md'));
|
|
73
|
+
console.log(chalk.dim('3. Use --fix to automatically fix issues'));
|
|
74
|
+
}
|
|
75
|
+
async function main() {
|
|
76
|
+
const program = new Command();
|
|
77
|
+
program
|
|
78
|
+
.name('twlint')
|
|
79
|
+
.description('A CLI tool for detecting simplified Chinese terms and suggesting Taiwan traditional alternatives')
|
|
80
|
+
.version('1.0.0');
|
|
81
|
+
program
|
|
82
|
+
.command('check')
|
|
83
|
+
.description('Check files for Chinese term issues')
|
|
84
|
+
.argument('<files...>', 'Files or patterns to check')
|
|
85
|
+
.option('--fix', 'Automatically fix issues where possible')
|
|
86
|
+
.option('--deep', 'Enable deep checking with all dictionaries')
|
|
87
|
+
.option('--format <type>', 'Output format (stylish, json)', 'stylish')
|
|
88
|
+
.option('--dict <names...>', 'Specify dictionaries to use')
|
|
89
|
+
.option('--config <path>', 'Path to configuration file')
|
|
90
|
+
.option('--verbose', 'Enable verbose output')
|
|
91
|
+
.action(async (files, options) => {
|
|
92
|
+
try {
|
|
93
|
+
const config = await loadConfig(options.config);
|
|
94
|
+
// 如果指定了 --dict 選項,覆蓋配置中的詞庫設定
|
|
95
|
+
if (options.dict) {
|
|
96
|
+
config.dictionaries = options.dict;
|
|
97
|
+
}
|
|
98
|
+
const linter = new TWLinter(config, { deep: options.deep });
|
|
99
|
+
if (options.deep && options.verbose) {
|
|
100
|
+
console.log(chalk.dim('🔍 Deep mode enabled - loading all available dictionaries'));
|
|
101
|
+
}
|
|
102
|
+
if (options.fix) {
|
|
103
|
+
// 自動修復模式
|
|
104
|
+
await performAutoFix(linter, files, options);
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
// 檢查模式
|
|
108
|
+
await performCheck(linter, files, options);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
113
|
+
console.error(chalk.red('Error:'), message);
|
|
114
|
+
process.exit(1);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
program
|
|
118
|
+
.command('init')
|
|
119
|
+
.description('Initialize a TWLint configuration file')
|
|
120
|
+
.option('--force', 'Overwrite existing configuration file')
|
|
121
|
+
.action(async (options) => {
|
|
122
|
+
try {
|
|
123
|
+
await performInit(options.force);
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
127
|
+
console.error(chalk.red('Error:'), message);
|
|
128
|
+
process.exit(1);
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
await program.parseAsync();
|
|
132
|
+
}
|
|
133
|
+
// 當作為主模組執行時啟動 CLI
|
|
134
|
+
if (import.meta.url === `file://${process.argv[1]}` ||
|
|
135
|
+
process.argv[1]?.endsWith('cli.js')) {
|
|
136
|
+
main().catch(error => {
|
|
137
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
138
|
+
console.error(chalk.red('Fatal error:'), message);
|
|
139
|
+
process.exit(1);
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACnC,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAC/C,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAk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,CAAA;QACtE,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,cAAc,CAAC,EAAE,OAAO,CAAC,CAAA;QACjD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;IACjB,CAAC,CAAC,CAAA;AACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config-loader.d.ts","sourceRoot":"","sources":["../../src/core/config-loader.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE/C,wBAAsB,UAAU,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC,CAqD3E"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { readFile, access } from 'fs/promises';
|
|
2
|
+
import { join } from 'path';
|
|
3
|
+
import { validateConfig, DEFAULT_CONFIG } from './config-schema.js';
|
|
4
|
+
export async function loadConfig(configPath) {
|
|
5
|
+
const paths = configPath
|
|
6
|
+
? [configPath]
|
|
7
|
+
: [
|
|
8
|
+
'twlint.config.js',
|
|
9
|
+
'twlint.config.mjs',
|
|
10
|
+
'twlint.config.ts',
|
|
11
|
+
'.twlintrc.json',
|
|
12
|
+
'.twlintrc'
|
|
13
|
+
];
|
|
14
|
+
for (const path of paths) {
|
|
15
|
+
try {
|
|
16
|
+
await access(path);
|
|
17
|
+
if (path.endsWith('.json') || path.endsWith('.twlintrc')) {
|
|
18
|
+
const content = await readFile(path, 'utf-8');
|
|
19
|
+
const userConfig = JSON.parse(content);
|
|
20
|
+
return validateConfig(userConfig);
|
|
21
|
+
}
|
|
22
|
+
if (path.endsWith('.js') || path.endsWith('.mjs') || path.endsWith('.ts')) {
|
|
23
|
+
const fullPath = join(process.cwd(), path);
|
|
24
|
+
try {
|
|
25
|
+
const module = await import(fullPath);
|
|
26
|
+
const userConfig = module.default || module;
|
|
27
|
+
return validateConfig(userConfig);
|
|
28
|
+
}
|
|
29
|
+
catch (importError) {
|
|
30
|
+
// 如果是 ES module 匯入問題,嘗試其他方式
|
|
31
|
+
if (path.endsWith('.js') || path.endsWith('.mjs')) {
|
|
32
|
+
const content = await readFile(path, 'utf-8');
|
|
33
|
+
// 簡單的配置檔案執行(僅支援 export default)
|
|
34
|
+
if (content.includes('export default')) {
|
|
35
|
+
console.warn(`Config file ${path} found but unable to import. Using default config.`);
|
|
36
|
+
return DEFAULT_CONFIG;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
throw importError;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
if (configPath) {
|
|
45
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
46
|
+
throw new Error(`Failed to load config from "${configPath}": ${message}`);
|
|
47
|
+
}
|
|
48
|
+
// Continue to next path if no specific config was requested
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
// Return default config if no config file found
|
|
52
|
+
return DEFAULT_CONFIG;
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=config-loader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config-loader.js","sourceRoot":"","sources":["../../src/core/config-loader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAC9C,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAA;AAC3B,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AAGnE,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,UAAmB;IAClD,MAAM,KAAK,GAAG,UAAU;QACtB,CAAC,CAAC,CAAC,UAAU,CAAC;QACd,CAAC,CAAC;YACE,kBAAkB;YAClB,mBAAmB;YACnB,kBAAkB;YAClB,gBAAgB;YAChB,WAAW;SACZ,CAAA;IAEL,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,IAAI,CAAC;YACH,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