This repository was archived by the owner on May 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathextension.ts
429 lines (370 loc) · 17.1 KB
/
extension.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// The module 'vscode' contains the VS Code extensibility API
// Import the necessary extensibility types to use in your code below
import {
window,
commands,
Disposable,
ExtensionContext,
StatusBarAlignment,
StatusBarItem,
TextDocument,
workspace,
Position,
Range,
TextEditor,
TextEditorEdit
} from 'vscode';
const REGEXP_TOC_START = /\s*<!--(.*)TOC(.*)-->/gi;
const REGEXP_TOC_STOP = /\s*<!--(.*)\/TOC(.*)-->/gi;
const REGEXP_TOC_CONFIG = /\w+[:=][\w.]+/gi;
const REGEXP_TOC_CONFIG_ITEM = /(\w+)[:=]([\w.]+)/;
const REGEXP_MARKDOWN_ANCHOR = /^<a id="markdown-.+" name=".+"><\/a\>/;
const REGEXP_HEADER = /^(\#{1,6})\s*(.+)/;
const REGEXP_CODE_BLOCK1 = /^```/;
const REGEXP_CODE_BLOCK2 = /^~~~/;
const REGEXP_ANCHOR = /\[.+\]\(#(.+)\)/
const REGEXP_IGNORE_TITLE = /<!-- TOC ignore:true -->/
const DEPTH_FROM = "depthFrom";
const DEPTH_TO = "depthTo";
const INSERT_ANCHOR = "insertAnchor";
const WITH_LINKS = "withLinks";
const ORDERED_LIST = "orderedList";
const UPDATE_ON_SAVE = "updateOnSave";
const ANCHOR_MODE = "anchorMode";
const LOWER_DEPTH_FROM = DEPTH_FROM.toLocaleLowerCase();
const LOWER_DEPTH_TO = DEPTH_TO.toLocaleLowerCase();
const LOWER_INSERT_ANCHOR = INSERT_ANCHOR.toLocaleLowerCase();
const LOWER_WITH_LINKS = WITH_LINKS.toLocaleLowerCase();
const LOWER_ORDERED_LIST = ORDERED_LIST.toLocaleLowerCase();
const LOWER_UPDATE_ON_SAVE = UPDATE_ON_SAVE.toLocaleLowerCase();
const LOWER_ANCHOR_MODE = ANCHOR_MODE.toLocaleLowerCase();
const ANCHOR_MODE_LIST =
[
"github.com",
"bitbucket.org",
"ghost.org",
"gitlab.com"
]
const EOL = require('os').EOL;
export function activate(context: ExtensionContext) {
// create a MarkdownTocTools
let markdownTocTools = new MarkdownTocTools();
let disposable_updateMarkdownToc = commands.registerCommand('extension.updateMarkdownToc', () => { markdownTocTools.updateMarkdownToc(); });
let disposable_deleteMarkdownToc = commands.registerCommand('extension.deleteMarkdownToc', () => { markdownTocTools.deleteMarkdownToc(); });
let disposable_updateMarkdownSections = commands.registerCommand('extension.updateMarkdownSections', () => { markdownTocTools.updateMarkdownSections(); });
let disposable_deleteMarkdownSections = commands.registerCommand('extension.deleteMarkdownSections', () => { markdownTocTools.deleteMarkdownSections(); });
let disposable_saveMarkdownToc = workspace.onDidSaveTextDocument((doc : TextDocument) => { markdownTocTools.notifyDocumentSave(); });
// Add to a list of disposables which are disposed when this extension is deactivated.
context.subscriptions.push(disposable_updateMarkdownToc);
context.subscriptions.push(disposable_deleteMarkdownToc);
context.subscriptions.push(disposable_updateMarkdownSections);
context.subscriptions.push(disposable_deleteMarkdownSections);
context.subscriptions.push(disposable_saveMarkdownToc);
}
class MarkdownTocTools {
options = {
DEPTH_FROM : 1,
DEPTH_TO : 6,
INSERT_ANCHOR : false,
WITH_LINKS : true,
ORDERED_LIST : false,
UPDATE_ON_SAVE : true,
ANCHOR_MODE : ANCHOR_MODE_LIST[0]
};
optionsFlag = [];
saveBySelf = false;
// Public function
public updateMarkdownToc(isBySave : boolean = false) {
let editor = window.activeTextEditor;
let markdownTocTools = this;
window.activeTextEditor.edit(function(editBuilder) {
let tocRange = markdownTocTools.getTocRange();
markdownTocTools.updateOptions(tocRange);
if (isBySave && ((!markdownTocTools.options.UPDATE_ON_SAVE) || (tocRange == null))) return false;
let insertPosition = editor.selection.active;
// save options, and delete last insert
if (tocRange != null) {
insertPosition = tocRange.start;
editBuilder.delete(tocRange);
markdownTocTools.deleteAnchor(editBuilder);
}
let headerList = markdownTocTools.getHeaderList();
markdownTocTools.createToc(editBuilder, headerList, insertPosition);
markdownTocTools.insertAnchor(editBuilder, headerList);
});
return true;
}
public deleteMarkdownToc() {
let markdownTocTools = this;
window.activeTextEditor.edit(function(editBuilder) {
let tocRange = markdownTocTools.getTocRange();
if (tocRange == null) return;
editBuilder.delete(tocRange);
markdownTocTools.deleteAnchor(editBuilder);
});
}
public updateMarkdownSections() {
let tocRange = this.getTocRange();
this.updateOptions(tocRange);
let headerList = this.getHeaderList();
window.activeTextEditor.edit(function(editBuilder) {
headerList.forEach(element => {
let newHeader = element.header + " " + element.orderedList + " " + element.baseTitle
editBuilder.replace(element.range, newHeader);
});
});
}
public deleteMarkdownSections() {
let tocRange = this.getTocRange();
this.updateOptions(tocRange);
let headerList = this.getHeaderList();
window.activeTextEditor.edit(function(editBuilder) {
headerList.forEach(element => {
let newHeader = element.header + " " + element.baseTitle
editBuilder.replace(element.range, newHeader);
});
});
}
public notifyDocumentSave() {
// Prevent save again
if (this.saveBySelf) {
this.saveBySelf = false;
return;
}
let doc = window.activeTextEditor.document;
if (doc.languageId != 'markdown') return;
if (this.updateMarkdownToc(true)) {
doc.save();
this.saveBySelf = true;
}
}
// Private function
private getTocRange() {
let doc = window.activeTextEditor.document;
let start, stop : Position;
for(let index = 0; index < doc.lineCount; index++) {
let lineText = doc.lineAt(index).text;
if ((start == null) && (lineText.match(REGEXP_TOC_START))) {
start = new Position(index, 0);
} else if (lineText.match(REGEXP_TOC_STOP)) {
stop = new Position(index, lineText.length);
break;
}
}
if ((start != null) && (stop != null)) {
return new Range(start, stop);
}
return null;
}
private updateOptions(tocRange : Range) {
this.loadConfigurations();
this.loadCustomOptions(tocRange);
}
private loadConfigurations() {
this.options.DEPTH_FROM = <number> workspace.getConfiguration('markdown-toc').get('depthFrom');
this.options.DEPTH_TO = <number> workspace.getConfiguration('markdown-toc').get('depthTo');
this.options.INSERT_ANCHOR = <boolean> workspace.getConfiguration('markdown-toc').get('insertAnchor');
this.options.WITH_LINKS = <boolean> workspace.getConfiguration('markdown-toc').get('withLinks');
this.options.ORDERED_LIST = <boolean> workspace.getConfiguration('markdown-toc').get('orderedList');
this.options.UPDATE_ON_SAVE = <boolean> workspace.getConfiguration('markdown-toc').get('updateOnSave');
this.options.ANCHOR_MODE = <string> workspace.getConfiguration('markdown-toc').get('anchorMode');
}
private loadCustomOptions(tocRange : Range) {
this.optionsFlag = [];
if (tocRange == null) return;
let optionsText = window.activeTextEditor.document.lineAt(tocRange.start.line).text;
let options = optionsText.match(REGEXP_TOC_CONFIG);
if (options == null) return;
options.forEach(element => {
let pair = REGEXP_TOC_CONFIG_ITEM.exec(element)
let key = pair[1].toLocaleLowerCase();
let value = pair[2];
switch (key) {
case LOWER_DEPTH_FROM:
this.optionsFlag.push(DEPTH_FROM);
this.options.DEPTH_FROM = this.parseValidNumber(value);
break;
case LOWER_DEPTH_TO:
this.optionsFlag.push(DEPTH_TO);
this.options.DEPTH_TO = Math.max(this.parseValidNumber(value), this.options.DEPTH_FROM);
break;
case LOWER_INSERT_ANCHOR:
this.optionsFlag.push(INSERT_ANCHOR);
this.options.INSERT_ANCHOR = this.parseBool(value);
break;
case LOWER_WITH_LINKS:
this.optionsFlag.push(WITH_LINKS);
this.options.WITH_LINKS = this.parseBool(value);
break;
case LOWER_ORDERED_LIST:
this.optionsFlag.push(ORDERED_LIST);
this.options.ORDERED_LIST = this.parseBool(value);
break;
case LOWER_UPDATE_ON_SAVE:
this.optionsFlag.push(UPDATE_ON_SAVE);
this.options.UPDATE_ON_SAVE = this.parseBool(value);
break;
case LOWER_ANCHOR_MODE:
this.optionsFlag.push(ANCHOR_MODE);
this.options.ANCHOR_MODE = this.parseValidAnchorMode(value);
break;
}
});
}
private insertAnchor(editBuilder : TextEditorEdit, headerList : any[]) {
if (!this.options.INSERT_ANCHOR) return;
headerList.forEach(element => {
let name = element.hash.match(REGEXP_ANCHOR)[1];
let text = [ '<a id="markdown-', name, '" name="', name, '"></a>\n' ];
let insertPosition = new Position(element.line, 0);
editBuilder.insert(insertPosition, text.join(''));
});
}
private deleteAnchor(editBuilder : TextEditorEdit) {
let doc = window.activeTextEditor.document;
for(let index = 0; index < doc.lineCount; index++) {
let lineText = doc.lineAt(index).text;
if(lineText.match(REGEXP_MARKDOWN_ANCHOR) == null) continue;
let range = new Range(new Position(index, 0), new Position(index + 1, 0));
editBuilder.delete(range);
}
}
private createToc(editBuilder : TextEditorEdit, headerList : any[], insertPosition : Position) {
let lineEnding = <string> workspace.getConfiguration("files").get("eol");
let tabSize = <number> workspace.getConfiguration("[markdown]")["editor.tabSize"];
let insertSpaces = <boolean> workspace.getConfiguration("[markdown]")["editor.insertSpaces"];
if (lineEnding === 'auto') {
lineEnding = <string> EOL;
}
if(tabSize === undefined || tabSize === null) {
tabSize = <number> workspace.getConfiguration("editor").get("tabSize");
}
if(insertSpaces === undefined || insertSpaces === null) {
insertSpaces = <boolean> workspace.getConfiguration("editor").get("insertSpaces");
}
let tab = '\t';
if (insertSpaces && tabSize > 0) {
tab = " ".repeat(tabSize);
}
let optionsText = [];
optionsText.push('<!-- TOC ');
if (this.optionsFlag.indexOf(DEPTH_FROM) != -1) optionsText.push(DEPTH_FROM + ':' + this.options.DEPTH_FROM +' ');
if (this.optionsFlag.indexOf(DEPTH_TO) != -1) optionsText.push(DEPTH_TO + ':' + this.options.DEPTH_TO +' ');
if (this.optionsFlag.indexOf(INSERT_ANCHOR) != -1) optionsText.push(INSERT_ANCHOR + ':' + this.options.INSERT_ANCHOR +' ');
if (this.optionsFlag.indexOf(ORDERED_LIST) != -1) optionsText.push(ORDERED_LIST + ':' + this.options.ORDERED_LIST +' ');
if (this.optionsFlag.indexOf(UPDATE_ON_SAVE)!= -1) optionsText.push(UPDATE_ON_SAVE + ':' + this.options.UPDATE_ON_SAVE +' ');
if (this.optionsFlag.indexOf(WITH_LINKS) != -1) optionsText.push(WITH_LINKS + ':' + this.options.WITH_LINKS +' ');
if (this.optionsFlag.indexOf(ANCHOR_MODE) != -1) optionsText.push(ANCHOR_MODE + ':' + this.options.ANCHOR_MODE +' ');
optionsText.push('-->' + lineEnding);
let text = [];
text.push(optionsText.join(''));
let indicesOfDepth = Array.apply(null, new Array(this.options.DEPTH_TO - this.options.DEPTH_FROM + 1)).map(Number.prototype.valueOf, 0);
let waitResetList = Array.apply(null, new Array(indicesOfDepth.length)).map(Boolean.prototype.valueOf, false);
let minDepth = 6;
headerList.forEach(element => {
minDepth = Math.min(element.depth, minDepth);
});
let startDepth = Math.max(minDepth , this.options.DEPTH_FROM);
headerList.forEach(element => {
if (element.depth <= this.options.DEPTH_TO) {
let length = element.depth - startDepth;
for (var index = 0; index < waitResetList.length; index++) {
if (waitResetList[index] && (length < index)) {
indicesOfDepth[index] = 0;
waitResetList[index] = false;
}
}
let row = [
tab.repeat(length),
this.options.ORDERED_LIST ? (++indicesOfDepth[length] + '. ') : '- ',
this.options.WITH_LINKS ? element.hash : element.title
];
text.push(row.join(''));
waitResetList[length] = true;
}
});
text.push(lineEnding + "<!-- /TOC -->");
editBuilder.insert(insertPosition, text.join(lineEnding));
}
private getHeaderList() {
let doc = window.activeTextEditor.document;
let headerList = [];
let hashMap = {};
let isInCode = 0;
let indicesOfDepth = Array.apply(null, new Array(6)).map(Number.prototype.valueOf, 0);
for (let index = 0; index < doc.lineCount; index++) {
let lineText = doc.lineAt(index).text;
let codeResult1 = lineText.match(REGEXP_CODE_BLOCK1);
let codeResult2 = lineText.match(REGEXP_CODE_BLOCK2);
if (isInCode == 0) {
isInCode = codeResult1 != null ? 1 : (codeResult2 != null ? 2 : isInCode);
} else if (isInCode == 1) {
isInCode = codeResult1 != null ? 0 : isInCode;
} else if (isInCode == 2) {
isInCode = codeResult2 != null ? 0 : isInCode;
}
if (isInCode) continue;
let headerResult = lineText.match(REGEXP_HEADER);
if (headerResult == null) continue;
let depth = headerResult[1].length;
if (depth < this.options.DEPTH_FROM) continue;
if (depth > this.options.DEPTH_TO) continue;
if (lineText.match(REGEXP_IGNORE_TITLE)) continue;
for (var i = depth; i <= this.options.DEPTH_TO; i++) {
indicesOfDepth[depth] = 0;
}
indicesOfDepth[depth - 1]++;
let orderedListStr = ""
for (var i = this.options.DEPTH_FROM - 1; i < depth; i++) {
orderedListStr += indicesOfDepth[i].toString() + ".";
}
let title = lineText.substr(depth).trim();
let baseTitle = title.replace(/^(?:\d+\.)+/, "").trim(); // title without section number
title = title.replace(/\[(.+)]\([^)]*\)/gi, "$1"); // replace link
title = title.replace(/<!--.+-->/gi, ""); // replace comment
title = title.replace(/\#*_/gi, "").trim(); // replace special char
if (hashMap[title] == null) {
hashMap[title] = 0
} else {
hashMap[title] += 1;
}
let hash = this.getHash(title, this.options.ANCHOR_MODE, hashMap[title]);
headerList.push({
line : index,
depth : depth,
title : title,
hash : hash,
range : new Range(index, 0, index, lineText.length),
header : headerResult[1],
orderedList : orderedListStr,
baseTitle : baseTitle
});
}
return headerList;
}
private getHash(headername : string, mode : string, repetition : number) {
let anchor = require('anchor-markdown-header');
return decodeURI(anchor(headername, mode, repetition));
}
private parseValidNumber(value : string) {
let num = parseInt(value);
if (num < 1) {
return 1;
}
if (num > 6) {
return 6;
}
return num;
}
private parseValidAnchorMode(value : string) {
if (ANCHOR_MODE_LIST.indexOf(value) != -1) {
return value;
}
return ANCHOR_MODE_LIST[0];
}
private parseBool(value : string) {
return value.toLocaleLowerCase() == 'true';
}
dispose() {
}
}