-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathhandlers.ts
413 lines (392 loc) · 10.4 KB
/
handlers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// @ts-ignore
import pangu from "pangu";
import {Converter} from "opencc-js";
export interface TextHandler {
activate: boolean, // 默认是否启用
description: string, // 对该功能的描述
executor: (text: string) => string // 功能函数
}
export interface TextHandlers {
[handlerName: string]: TextHandler;
}
export interface TextHandlerWithName extends TextHandler {
handlerName: string;
}
export type ReplaceTuple = [RegExp | string, string]
export const textHandlers: TextHandlers = {
transferEnglishPunctuationToChinese: {
activate: false,
description: "将英文标点转换为中文标点",
executor: text => {
const punctuationTuples: ReplaceTuple[] = [
[/'([\s\S]+?)'/g, "‘$1’"],
[/"([\s\S]+?)"/g, "“$1”"],
[",", ","],
[/\.(\D)/g, "。$1"], /** `\D`:避免是小数点 */
[":", ":"],
[";", ";"],
["[", "【"],
["]", "】"],
["?", "?"],
["(", "("],
[")", ")"],
["!", "!"],
];
return punctuationTuples.reduce((t, tuple) => t.replaceAll(tuple[0], tuple[1]), text);
},
},
transferChinesePunctuationToEnglish: {
activate: false,
description: "将中文标点转换为英文标点",
executor: text => {
const punctuationTuples: ReplaceTuple[] = [
[/[‘’]/g, "'"],
[/[“”]/g, "\""],
[",", ","],
["。", "."],
[":", ":"],
[";", ";"],
["【", "["],
["】", "]"],
["?", "?"],
["(", "("],
[")", ")"],
["!", "!"],
];
return punctuationTuples.reduce((t, tuple) => t.replaceAll(tuple[0], tuple[1]), text);
},
},
deleteReferenceCurveBadge: {
description: "删除引用角标,如: (1), (2, 3), (4-7)",
activate: false,
executor: text => text.replaceAll(/\([\d,\-\s]+\)/g, ""),
},
deleteReferenceBadge: {
description: "删除引用角标,如: [1], [2, 3], [4-7]",
activate: true,
executor: text => {
text = text.replaceAll(/\[[\d,\-\s]+]/g, "");
text = text.replaceAll(/【[\d,\-\s]+】/g, "");
return text;
},
},
/** 全角转半角, 参考:
* 1. https://www.cnblogs.com/html55/p/10298569.html
* 2. https://unicode-table.com/cn/search/?q=%E5%85%A8%E5%BD%A2%E6%95%B0%E5%AD%97 */
convertFullWidthCharactersToHalfWidth: {
activate: true,
description: "全角字符转半角字符",
executor: (text: string) => {
let result = "";
for (let i = 0; i < text.length; i++) {
let char = text.charCodeAt(i);
// 中文空格替换为英文空格
if (char == 12288) {
result += " ";
} else if (char > 65280 && char < 65375
// 对以下全角字符不做转换
&& [...",:;·!#¥%…()"].indexOf(text[i]) === -1) {
result += String.fromCharCode(char - 65248);
} else {
result += String.fromCharCode(text.charCodeAt(i));
}
}
return result;
},
},
deleteMultipleNewlines: {
activate: true,
description: "删除重复的换行符",
executor: (text: string) => text.replaceAll(/[\f\r\t\n]+/g, "\n"),
},
/** 替换换行符为空格 (参考:https://blog.csdn.net/lfod1997/article/details/121095287) */
replaceNewlinesWithSpaces: {
activate: true,
description: "将换行符替换为空格",
executor: (text: string) => text.replaceAll(/[\f\r\t\n]/g, " "),
},
replaceMultipleSpacesWithASingleSpace: {
activate: true,
description: "删除重复的空格",
executor: (text: string) => text.replaceAll(/ +/g, " "),
},
removeSpacesBetweenNonEnglishLetters: {
activate: true,
description: "删除非英文字母间的空格",
executor: (text: string) => text
.replaceAll(/([^A-Za-z"':]) +/g, "$1")
.replaceAll(/ +([^A-Za-z"':])/g, "$1"),
},
addSpaceAfterEnglishPunctuation: {
activate: true,
description: "在标点符号后添加空格",
executor: (text: string) => text
.replaceAll(/([,.?:;])([^,.?:;\s])/g, "$1 $2")
.replaceAll(/(\))([a-zA-Z\d])/g, "$1 $2"),
},
deleteSpaceBetweenDotAndNumber: {
activate: true,
description: "删除小数点和数字之间的空格",
executor: (text: string) => text.replaceAll(/(\.)\s+(\d)/g, "$1$2"),
},
addSpacesBetweenEnglishLettersAndNumbers: {
activate: false,
description: "在字母与数字之间添加空格",
executor: (text: string) => text
.replaceAll(/(\d)([A-Za-z])/g, "$1 $2")
.replaceAll(/([A-Za-z])(\d)/g, "$1 $2"),
},
deleteSpaceBetweenColonAndNumber: {
activate: false,
description: "删除冒号和数字之间的空格",
executor: (text: string) => text.replaceAll(/(:)\s+(\d)/g, "$1$2"),
},
addWhiteOfPanGu: {
description: `规范中文排版 <a href='https://sspai.com/post/37815' target='_blank'>(参考文章)</a>`,
activate: false,
executor: (text: string) => pangu.spacing(text),
},
switchSimplifiedChineseToTraditionalChinese: {
description: "将简体中文转换为繁体中文",
activate: false,
executor: (text: string) => {
const converter = Converter({from: "cn", to: "hk"});
return converter(text);
},
},
switchTraditionalChineseToSimplifiedChinese: {
description: "将繁体中文转换为简体中文",
activate: false,
executor: (text: string) => {
const converter = Converter({from: "hk", to: "cn"});
return converter(text);
},
},
correctKangxiRadicals: {
description: "将康熙部首替换为正常字符",
activate: true,
executor: (text: string) => {
const kangxiMap = {
"⼀": "一",
"⼁": "丨",
"⼂": "丶",
"⼃": "丿",
"⼄": "乙",
"⼅": "亅",
"⼆": "二",
"⼇": "亠",
"⼈": "人",
"⼉": "儿",
"⼊": "入",
"⼋": "八",
"⼌": "冂",
"⼍": "冖",
"⼎": "冫",
"⼏": "几",
"⼐": "凵",
"⼑": "刀",
"⼒": "力",
"⼓": "勹",
"⼔": "匕",
"⼕": "匚",
"⼖": "匸",
"⼗": "十",
"⼘": "卜",
"⼙": "卩",
"⼚": "厂",
"⼛": "厶",
"⼜": "又",
"⼝": "口",
"⼞": "囗",
"⼟": "土",
"⼠": "士",
"⼡": "夂",
"⼢": "夊",
"⼣": "夕",
"⼤": "大",
"⼥": "女",
"⼦": "子",
"⼧": "宀",
"⼨": "寸",
"⼩": "小",
"⼪": "尢",
"⼫": "尸",
"⼬": "屮",
"⼭": "山",
"⼮": "巛",
"⼯": "工",
"⼰": "己",
"⼱": "巾",
"⼲": "干",
"⼳": "幺",
"⼴": "广",
"⼵": "廴",
"⼶": "廾",
"⼷": "弋",
"⼸": "弓",
"⼹": "彐",
"⼺": "彡",
"⼻": "彳",
"⼼": "心",
"⼽": "戈",
"⼾": "戸",
"⼿": "手",
"⽀": "支",
"⽁": "攴",
"⽂": "文",
"⽃": "斗",
"⽄": "斤",
"⽅": "方",
"⽆": "无",
"⽇": "日",
"⽈": "曰",
"⽉": "月",
"⽊": "木",
"⽋": "欠",
"⽌": "止",
"⽍": "歹",
"⽎": "殳",
"⽏": "毋",
"⽐": "比",
"⽑": "毛",
"⽒": "氏",
"⽓": "气",
"⽔": "水",
"⽕": "火",
"⽖": "爪",
"⽗": "父",
"⽘": "爻",
"⽙": "爿",
"⽚": "片",
"⽛": "牙",
"⽜": "牛",
"⽝": "犬",
"⽞": "玄",
"⽟": "玉",
"⽠": "瓜",
"⽡": "瓦",
"⽢": "甘",
"⽣": "生",
"⽤": "用",
"⽥": "田",
"⽦": "疋",
"⽧": "疒",
"⽨": "癶",
"⽩": "白",
"⽪": "皮",
"⽫": "皿",
"⽬": "目",
"⽭": "矛",
"⽮": "矢",
"⽯": "石",
"⽰": "示",
"⽱": "禸",
"⽲": "禾",
"⽳": "穴",
"⽴": "立",
"⽵": "竹",
"⽶": "米",
"⽷": "糸",
"⽸": "缶",
"⽹": "网",
"⽺": "羊",
"⽻": "羽",
"⽼": "老",
"⽽": "而",
"⽾": "耒",
"⽿": "耳",
"⾀": "聿",
"⾁": "肉",
"⾂": "臣",
"⾃": "自",
"⾄": "至",
"⾅": "臼",
"⾆": "舌",
"⾇": "舛",
"⾈": "舟",
"⾉": "艮",
"⾊": "色",
"⾋": "艸",
"⾌": "虍",
"⾍": "虫",
"⾎": "血",
"⾏": "行",
"⾐": "衣",
"⾑": "襾",
"⾒": "見",
"⾓": "角",
"⾔": "言",
"⾕": "谷",
"⾖": "豆",
"⾗": "豕",
"⾘": "豸",
"⾙": "貝",
"⾚": "赤",
"⾛": "走",
"⾜": "足",
"⾝": "身",
"⾞": "車",
"⾟": "辛",
"⾠": "辰",
"⾡": "辵",
"⾢": "邑",
"⾣": "酉",
"⾤": "釆",
"⾥": "里",
"⾦": "金",
"⾧": "長",
"⾨": "門",
"⾩": "阜",
"⾪": "隶",
"⾫": "隹",
"⾬": "雨",
"⾭": "靑",
"⾮": "非",
"⾯": "面",
"⾰": "革",
"⾱": "韋",
"⾲": "韭",
"⾳": "音",
"⾴": "頁",
"⾵": "風",
"⾶": "飛",
"⾷": "食",
"⾸": "首",
"⾹": "香",
"⾺": "馬",
"⾻": "骨",
"⾼": "高",
"⾽": "髟",
"⾾": "鬥",
"⾿": "鬯",
"⿀": "鬲",
"⿁": "鬼",
"⿂": "魚",
"⿃": "鳥",
"⿄": "鹵",
"⿅": "鹿",
"⿆": "麥",
"⿇": "麻",
"⿈": "黃",
"⿉": "黍",
"⿊": "黒",
"⿋": "黹",
"⿌": "黽",
"⿍": "鼎",
"⿎": "鼓",
"⿏": "鼠",
"⿐": "鼻",
"⿑": "齊",
"⿒": "齒",
"⿓": "龍",
"⿔": "龜",
"⿕": "龠",
};
type Radicals = keyof typeof kangxiMap;
const regexp = /([\u2f00-\u2fd5])/g;
return text.replace(regexp, (m: string) => {
return kangxiMap[m as Radicals] || m;
});
},
},
};