/
handlers.ts
185 lines (165 loc) · 5.47 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
// @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);
},
},
};