forked from hylarucoder/writing4cn
-
Notifications
You must be signed in to change notification settings - Fork 2
/
extension.js
285 lines (275 loc) · 10.9 KB
/
extension.js
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
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
function activate(context) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('"MarkdownFormat" 现在已激活!');
let documentFormatter = new DocumentFormatter();
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
context.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider('markdown', {
provideDocumentFormattingEdits(document) {
let content = documentFormatter.updateDocument(document);
return [new vscode.TextEdit(documentFormatter.current_document_range(document), content)];
}
}));
context.subscriptions.push(vscode.commands.registerCommand('extension.reFormat', () => {
let editor = vscode.window.activeTextEditor;
if (editor === undefined) {
return;
}
let doc = editor.document;
// Only update status if an Markdown file
if (doc.languageId !== "markdown") {
return;
}
editor = vscode.window.activeTextEditor;
if (editor === undefined) {
return;
}
let content = documentFormatter.updateDocument(doc);
editor.edit((editorBuilder) => {
editorBuilder.replace(documentFormatter.current_document_range(doc), content);
});
// Display a message box to the user
vscode.window.showInformationMessage('MD格式化完毕!');
}));
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() { }
exports.deactivate = deactivate;
class DocumentFormatter {
/**
* 更新文档
*/
updateDocument(document) {
// 获取配置
const config = vscode.workspace.getConfiguration("MarkdownFormat");
// 按照每行进行搞定
let content = document.getText(this.current_document_range(document));
let lines = [];
let tag = true;
// 每行操作
lines = content.split("\n").map((line) => {
line = line.replace(/(.*)[\r\n]$/g, "$1").replace(/(\s*$)/g, "");
// 忽略代码块
if (line.trim().search("```") === 0) {
tag = !tag;
if (tag) {
return line + "\n";
} else {
return "\n" + line;
}
} else if (tag) {
// 忽略 @import 语法
if (line.trim().search(/^@import /) == -1) {
let line_tmp = "";
// 使用行中代码块为分割
line.split(/(`.*?`)/).forEach(element => {
if (element.search(/(`.*`)/) == -1) {
// 修复 markdown 链接所使用的标点。
if (config.get("line")) {
element = element.replace(/[『\[]([^』\]]+)[』\]][『\[]([^』\]]+)[』\]]/g, "[$1]($2)");
element = element.replace(/[『\[]([^』\]]+)[』\]][((]([^』)]+)[))]/g, "[$1]($2)");
}
// 注释处理
if (config.get("note")) {
if (element.trim().search(/^<!--.*-->$/) != -1) {
// 注释前后加入空行
element = element.replace(/(^\s*<!--.*-->)([\r\n]*)/, "\n$1\n");
}
}
// 忽略链接以及注释格式
if (!element.match(/(\[.*\])(\(.*\))/g) && !element.match(/(^\s*<.*>$)/g)) {
// 替换全角数字
if (config.get("replaceFullNums")) {
content = this.replaceFullNums(content);
}
// 替换全角英文和标点
if (config.get("replaceFullChars")) {
content = this.replaceFullChars(content);
}
// 汉字后的标点符号,转成全角符号。
if (config.get("replacePunctuations")) {
element = this.replacePunctuations(element);
}
// 汉字与其前后的英文字符、英文标点、数字间增加空白。
if (config.get("cn")) {
element = element.replace(/([\u4e00-\u9fa5\u3040-\u30FF])([a-zA-Z0-9@&=\[\$\%\^\-\+(\/\\])/g, '$1 $2');
element = element.replace(/([a-zA-Z0-9!&;=\]\,\.\:\?\$\%\^\-\+\)\/\\])([\u4e00-\u9fa5\u3040-\u30FF])/g, "$1 $2");
}
// 英文与其前后的英文标点、数字间增加空白。
if (config.get("en")) {
element = element.replace(/([a-zA-Z])([0-9@&=\[\$\%\^\-\+(\/\\])/g, '$1 $2');
element = element.replace(/([0-9!&;=\]\,\.\:\?\$\%\^\-\+\)\/\\])([a-zA-Z])/g, "$1 $2");
}
}
}
line_tmp += element;
});
// 标题处理
if (config.get("title")) {
if (line_tmp.trim().search(/(^#{1,6}.*)([\r\n]*)/) != -1) {
// 标题后加入一个空格
if (line_tmp.trim().search(/(^#{1,6}\s+)([\r\n]*)/) == -1) {
line_tmp = line_tmp.trim().replace(/(^#{1,6})(.*)/, "$1 $2");
} else {
line_tmp = line_tmp.trim().replace(/(^#{1,6})\s+(.*)/, "$1 $2");
}
// 标题前后加入空行
line_tmp = line_tmp.trim().replace(/(^#{1,6}.*)([\r\n]*)/, "\n$1\n");
}
}
line = line_tmp;
}
}
return line;
});
let i = 1;
content = "";
tag = true;
lines.join("\n").split("\n").forEach(line => {
if (line.trim().search("```") === 0) {
tag = !tag;
if (tag) {
content += line + "\n";
} else {
content += line + "\n";
}
i = 0;
} else if (tag) {
if (line.trim().length === 0) {
if (i == 0) {
content += "\n";
}
i += 1;
} else {
i = 0;
}
if (i == 0) {
content += line.replace(/(.*)[\r\n]$/g, "$1") + "\n";
}
} else {
content += line + "\n";
}
});
content = content.trim() + "\n";
return content;
}
/**
* 当前文档范围
*/
current_document_range(doc) {
let start = new vscode.Position(0, 0);
let end = new vscode.Position(doc.lineCount - 1, doc.lineAt(doc.lineCount - 1).text.length);
let range = new vscode.Range(start, end);
return range;
}
/**
* 替换汉字后标点
*/
replacePunctuations(content) {
// 汉字后的标点符号,转成全角符号。
content = content.replace(/([\u4e00-\u9fa5\u3040-\u30FF])\.($|\s*)/g, '$1。');
content = content.replace(/([\u4e00-\u9fa5\u3040-\u30FF]),\s*/g, '$1,');
content = content.replace(/([\u4e00-\u9fa5\u3040-\u30FF]);\s*/g, '$1;');
content = content.replace(/([\u4e00-\u9fa5\u3040-\u30FF])!\s*/g, '$1!');
content = content.replace(/([\u4e00-\u9fa5\u3040-\u30FF]):\s*/g, '$1:');
content = content.replace(/([\u4e00-\u9fa5\u3040-\u30FF])\?\s*/g, '$1?');
content = content.replace(/([\u4e00-\u9fa5\u3040-\u30FF])\\\s*/g, '$1、');
// 圆括号()
content = content.replace(/\(([\u4e00-\u9fa5\u3040-\u30FF])/g, '($1');
content = content.replace(/([\u4e00-\u9fa5\u3040-\u30FF])\)/g, '$1)');
// 方括号【】
content = content.replace(/\[([\u4e00-\u9fa5\u3040-\u30FF])/g, '【$1');
content = content.replace(/([\u4e00-\u9fa5\u3040-\u30FF。!])\]/g, '$1】');
// 尖括号《》
content = content.replace(/<([\u4e00-\u9fa5\u3040-\u30FF])/g, '《$1');
content = content.replace(/([\u4e00-\u9fa5\u3040-\u30FF。!])>/g, '$1》');
content = content.replace(/。\{3,}/g, '......');
content = content.replace(/([!?])$1{3,}/g, '$1$1$1');
content = content.replace(/([。,;:、“”『』〖〗《》])\1{1,}/g, '$1');
return content;
}
/**
* 替换全角数字
*/
replaceFullNums(content) {
content = content.replace("0", "0");
content = content.replace("1", "1");
content = content.replace("2", "2");
content = content.replace("3", "3");
content = content.replace("4", "4");
content = content.replace("5", "5");
content = content.replace("6", "6");
content = content.replace("7", "7");
content = content.replace("8", "8");
content = content.replace("9", "9");
return content;
}
/**
* 替换全角英文和标点
*/
replaceFullChars(content) {
// 全角英文和标点。
content = content.replace("A", "A");
content = content.replace("B", "B");
content = content.replace("C", "C");
content = content.replace("D", "D");
content = content.replace("E", "E");
content = content.replace("F", "F");
content = content.replace("G", "G");
content = content.replace("H", "H");
content = content.replace("I", "I");
content = content.replace("J", "J");
content = content.replace("K", "K");
content = content.replace("L", "L");
content = content.replace("M", "M");
content = content.replace("N", "N");
content = content.replace("O", "O");
content = content.replace("P", "P");
content = content.replace("Q", "Q");
content = content.replace("R", "R");
content = content.replace("S", "S");
content = content.replace("T", "T");
content = content.replace("U", "U");
content = content.replace("V", "V");
content = content.replace("W", "W");
content = content.replace("X", "X");
content = content.replace("Y", "Y");
content = content.replace("Z", "Z");
content = content.replace("a", "a");
content = content.replace("b", "b");
content = content.replace("c", "c");
content = content.replace("d", "d");
content = content.replace("e", "e");
content = content.replace("f", "f");
content = content.replace("g", "g");
content = content.replace("h", "h");
content = content.replace("i", "i");
content = content.replace("j", "j");
content = content.replace("k", "k");
content = content.replace("l", "l");
content = content.replace("m", "m");
content = content.replace("n", "n");
content = content.replace("o", "o");
content = content.replace("p", "p");
content = content.replace("q", "q");
content = content.replace("r", "r");
content = content.replace("s", "s");
content = content.replace("t", "t");
content = content.replace("u", "u");
content = content.replace("v", "v");
content = content.replace("w", "w");
content = content.replace("x", "x");
content = content.replace("y", "y");
content = content.replace("z", "z");
content = content.replace("@", "@");
return content;
}
}