forked from DavidAnson/vscode-markdownlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
636 lines (592 loc) · 20.2 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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
"use strict";
// Requires
const vscode = require("vscode");
// Lazy requires
function lazyRequire (name) {
let module = null;
return new Proxy({}, {
"get": (obj, prop) => {
module = module || require(name);
return module[prop];
}
});
}
const markdownlint = lazyRequire("markdownlint");
const minimatch = lazyRequire("minimatch");
const jsYaml = lazyRequire("js-yaml");
const fs = lazyRequire("fs");
const os = lazyRequire("os");
const path = lazyRequire("path");
// Constants
const extensionDisplayName = "markdownlint";
const configFileGlob = ".markdownlint.{json,yaml,yml}";
const configFileNames = [
".markdownlint.json",
".markdownlint.yaml",
".markdownlint.yml"
];
const markdownLanguageId = "markdown";
const markdownScheme = "file";
const documentSelector = {
"language": markdownLanguageId,
"scheme": markdownScheme
};
const configParsers = [
JSON.parse,
// @ts-ignore
(content) => jsYaml.safeLoad(content)
];
const clickForInfo = "Click for more information about ";
const clickToFix = "Click to fix this violation of ";
const fixLineCommandName = "markdownlint.fixLine";
const clickForConfigureInfo = `Click for details about configuring ${extensionDisplayName} rules`;
const clickForConfigureUrl = "https://github.com/DavidAnson/vscode-markdownlint#configure";
const clickForConfigSource = `Click to open this document's ${extensionDisplayName} configuration`;
const openGlobalSettingsCommand = "workbench.action.openGlobalSettings";
const openWorkspaceSettingsCommand = "workbench.action.openWorkspaceSettings";
const openFolderSettingsCommand = "workbench.action.openFolderSettings";
const openCommand = "vscode.open";
const throttleDuration = 500;
const customRuleExtensionPrefixRe = /^\{([^}]+)\}\/(.*)$/iu;
// Shared RegExps
const bareUrlRe = /(?:http|ftp)s?:\/\/[^\s]*/i;
const reversedLinkRe = /\(([^)]+)\)\[([^\]^][^\]]*)]/;
const spaceInsideCodeRe = /`(?:\s+([^`]*?)\s*|([^`]*?)\s+)`/;
const spaceInsideEmphasisRe = /(\*\*?|__?)(?:\s+(.+?)\s*|(.+?)\s+)\1/;
const spaceInsideLinkRe = /\[(?:\s+([^\]]*?)\s*|([^\]]*?)\s+)]/;
const trailingSpaceRe = /\s+$/;
// Fix functions
function removeLeadingWhitespace (text) {
return text.replace(/^\s+/, "");
}
function removeTrailingWhitespace (text) {
return text.replace(trailingSpaceRe, "");
}
function replaceTabsWithSpaces (text) {
return text.replace(/\t/g, " ");
}
function fixAtxHeadingFormat (text) {
return text.replace(/^(\s*#+)\s*(.*)$/, "$1 $2");
}
function fixAtxClosedHeadingFormat (text) {
return fixAtxHeadingFormat(text).replace(/^(.*?)\s*(#+\s*)$/, "$1 $2");
}
function fixBlockquoteSpacing (text) {
return text.replace(/^(\s*(> )+)\s+(.*)$/, "$1$3");
}
function addBlockquoteJoiner (text) {
return text.replace(/^\s*$/, ">");
}
function wrapBareUrl (text) {
return text.replace(bareUrlRe, "<$&>");
}
function fixReversedLink (text) {
return text.replace(reversedLinkRe, "[$1]($2)");
}
function fixSpaceInEmphasis (text) {
return text.replace(spaceInsideEmphasisRe, "$1$2$3$1");
}
function fixSpaceInCode (text) {
return text.replace(spaceInsideCodeRe, "`$1$2`");
}
function fixSpaceInLink (text) {
return text.replace(spaceInsideLinkRe, "[$1$2]");
}
function fixTrailingNewline (text) {
return text + "\n";
}
const fixFunctions = {
"MD006": removeLeadingWhitespace,
"MD009": removeTrailingWhitespace,
"MD010": replaceTabsWithSpaces,
"MD011": fixReversedLink,
"MD018": fixAtxHeadingFormat,
"MD019": fixAtxHeadingFormat,
"MD020": fixAtxClosedHeadingFormat,
"MD021": fixAtxClosedHeadingFormat,
"MD023": removeLeadingWhitespace,
"MD027": fixBlockquoteSpacing,
"MD028": addBlockquoteJoiner,
"MD034": wrapBareUrl,
"MD037": fixSpaceInEmphasis,
"MD038": fixSpaceInCode,
"MD039": fixSpaceInLink,
"MD047": fixTrailingNewline
};
// Variables
const ruleNameToInformation = {};
let outputChannel = null;
let diagnosticCollection = null;
let configMap = {};
let runMap = {};
let customRules = null;
let ignores = null;
const throttle = {
"document": null,
"timeout": null
};
// Writes date and message to the output channel
function outputLine (message, show) {
const datePrefix = "[" + (new Date()).toLocaleTimeString() + "] ";
outputChannel.appendLine(datePrefix + message);
if (show) {
outputChannel.show();
}
}
// Returns rule configuration from nearest config file or workspace
function getConfig (document) {
const name = document.fileName;
// @ts-ignore
let dir = path.dirname(name);
let workspaceDetail = "not in a workspace folder";
// While inside the workspace
while (vscode.workspace.getWorkspaceFolder(vscode.Uri.file(dir))) {
workspaceDetail = "no configuration file in workspace folder";
// Use cached configuration if present for directory
if (configMap[dir]) {
return configMap[dir];
}
if (configMap[dir] === undefined) {
// Look for config file in current directory
for (const configFileName of configFileNames) {
// @ts-ignore
const configFilePath = path.join(dir, configFileName);
// @ts-ignore
if (fs.existsSync(configFilePath)) {
outputLine("INFO: Loading custom configuration from '" + configFilePath +
"', overrides user/workspace/custom configuration for directory and its children.");
try {
return (configMap[dir] = {
// @ts-ignore
"config": markdownlint.readConfigSync(configFilePath, configParsers),
"source": configFilePath
});
} catch (ex) {
outputLine("ERROR: Unable to read configuration file '" +
configFilePath + "' (" + (ex.message || ex.toString()) + ").", true);
}
}
}
// Remember missing or invalid file
configMap[dir] = null;
}
// @ts-ignore
const parent = path.dirname(dir);
// Move to parent directory, stop if no parent
if (dir === parent) {
break;
}
dir = parent;
}
// Use cached configuration if present for file
if (configMap[name]) {
return configMap[name];
}
// Use user/workspace configuration
outputLine("INFO: Loading user/workspace configuration for '" + name + "' (" + workspaceDetail + ").");
const configuration = vscode.workspace.getConfiguration(extensionDisplayName, document.uri);
const sectionConfig = "config";
let userWorkspaceConfig = configuration.get(sectionConfig);
const userWorkspaceConfigMetadata = configuration.inspect(sectionConfig);
let source = null;
if (userWorkspaceConfigMetadata.workspaceFolderValue && (vscode.workspace.workspaceFolders.length > 1)) {
// Length check to work around https://github.com/Microsoft/vscode/issues/34386
source = openFolderSettingsCommand;
} else if (userWorkspaceConfigMetadata.workspaceValue) {
source = openWorkspaceSettingsCommand;
} else if (userWorkspaceConfigMetadata.globalValue) {
source = openGlobalSettingsCommand;
}
// Bootstrap extend behavior into readConfigSync
if (userWorkspaceConfig && userWorkspaceConfig.extends) {
// @ts-ignore
const extendPath = path.resolve(os.homedir(), userWorkspaceConfig.extends);
try {
// @ts-ignore
const extendConfig = markdownlint.readConfigSync(extendPath, configParsers);
userWorkspaceConfig = {
...extendConfig,
...userWorkspaceConfig
};
} catch (ex) {
outputLine("ERROR: Unable to extend configuration file '" +
extendPath + "' (" + (ex.message || ex.toString()) + ").", true);
}
}
return (configMap[name] = {
"config": userWorkspaceConfig,
source
});
}
// Returns custom rule configuration for user/workspace
function getCustomRules () {
if (!Array.isArray(customRules)) {
customRules = [];
const itemAllow = "Allow";
const itemAlwaysAllow = "Always allow";
const itemBlock = "Block";
const sectionCustomRules = "customRules";
const sectionCustomRulesAlwaysAllow = "customRulesAlwaysAllow";
const configuration = vscode.workspace.getConfiguration(extensionDisplayName);
const customRulesPaths = configuration.get(sectionCustomRules);
if (customRulesPaths.length) {
const workspaceRootPath = vscode.workspace.rootPath;
const allowPaths = configuration.get(sectionCustomRulesAlwaysAllow);
const customRulesMetadata = configuration.inspect(sectionCustomRules);
const showWarning = customRulesMetadata.workspaceValue && !allowPaths.includes(workspaceRootPath);
const promise = showWarning ?
vscode.window.showWarningMessage(
"This workspace includes custom rules for Markdown linting. " +
"Custom rules include JavaScript that runs within VS Code. " +
"Only allow custom rules if you trust the workspace.",
itemAllow, itemAlwaysAllow, itemBlock
) :
Promise.resolve(itemAllow);
promise.then((response) => {
if (response === itemAlwaysAllow) {
allowPaths.push(workspaceRootPath);
configuration.update(sectionCustomRulesAlwaysAllow, allowPaths, vscode.ConfigurationTarget.Global);
}
if ((response === itemAllow) || (response === itemAlwaysAllow)) {
const workspacePath = vscode.workspace.workspaceFolders ?
vscode.workspace.workspaceFolders[0].uri.fsPath :
"";
customRulesPaths.forEach((rulePath) => {
let resolvedPath = null;
try {
const match = customRuleExtensionPrefixRe.exec(rulePath);
if (match) {
const extensionName = match[1];
const relativePath = match[2];
const extension = vscode.extensions.getExtension(extensionName);
if (!extension) {
throw new Error(`Extension '${extensionName}' not installed`);
}
// @ts-ignore
resolvedPath = path.resolve(extension.extensionPath, relativePath);
} else {
// @ts-ignore
resolvedPath = path.resolve(workspacePath, rulePath);
}
const exports = require(resolvedPath);
const rules = Array.isArray(exports) ?
exports :
[ exports ];
rules.forEach((rule) => {
if (rule.names && rule.description && rule.tags && rule.function) {
customRules.push(rule);
} else {
outputLine(`WARNING: Skipping invalid custom rule '${JSON.stringify(rule)}'.`, true);
}
});
outputLine(`INFO: Loaded custom rules from '${resolvedPath}'.`);
} catch (ex) {
outputLine("ERROR: Unable to load custom rules from '" + (resolvedPath || rulePath) +
"' (" + (ex.message || ex.toString()) + ").", true);
}
});
cleanLintVisibleFiles();
}
});
}
}
return customRules;
}
// Clears the custom rule list
function clearCustomRules () {
customRules = null;
}
// Returns ignore configuration for user/workspace
function getIgnores () {
if (!Array.isArray(ignores)) {
ignores = [];
const configuration = vscode.workspace.getConfiguration(extensionDisplayName);
const ignorePaths = configuration.get("ignore");
ignorePaths.forEach((ignorePath) => {
// @ts-ignore
const ignore = minimatch.makeRe(ignorePath, {
"dot": true,
"nocomment": true
});
if (ignore) {
ignores.push(ignore);
}
});
}
return ignores;
}
// Clears the ignore list
function clearIgnores () {
ignores = null;
}
// Lints a Markdown document
function lint (document) {
// Skip if not Markdown or local file
if ((document.languageId !== markdownLanguageId) ||
((document.uri.scheme !== markdownScheme) && (document.uri.scheme !== "untitled"))) {
return;
}
// Check ignore list
const diagnostics = [];
const relativePath = vscode.workspace.asRelativePath(document.uri, false);
// @ts-ignore
const normalizedPath = relativePath.split(path.sep).join("/");
if (getIgnores().every((ignore) => !ignore.test(normalizedPath))) {
// Configure
const uri = document.uri.toString();
const {config, source} = getConfig(document);
const options = {
"strings": {
[uri]: document.getText()
},
config,
"customRules": getCustomRules(),
"handleRuleFailures": true,
"markdownItPlugins": [ [ require("markdown-it-katex") ] ]
};
// Lint and create Diagnostics
try {
markdownlint
// @ts-ignore
.sync(options)[uri]
.forEach((result) => {
const ruleName = result.ruleNames[0];
const ruleDescription = result.ruleDescription;
ruleNameToInformation[ruleName] = result.ruleInformation;
let message = result.ruleNames.join("/") + ": " + ruleDescription;
if (result.errorDetail) {
message += " [" + result.errorDetail + "]";
}
let range = document.lineAt(result.lineNumber - 1).range;
if (result.errorRange) {
const start = result.errorRange[0] - 1;
const end = start + result.errorRange[1];
range = range.with(range.start.with(undefined, start), range.end.with(undefined, end));
}
const diagnostic = new vscode.Diagnostic(range, message, vscode.DiagnosticSeverity.Warning);
diagnostic.code = ruleName;
diagnostic.source = extensionDisplayName;
// @ts-ignore
diagnostic.configSource = source;
diagnostics.push(diagnostic);
});
} catch (ex) {
outputLine("ERROR: Exception while linting:\n" + ex.stack, true);
}
}
// Publish
diagnosticCollection.set(document.uri, diagnostics);
}
// Implements CodeActionsProvider.provideCodeActions to provide information and fix rule violations
function provideCodeActions (document, range, codeActionContext) {
const codeActions = [];
const diagnostics = codeActionContext.diagnostics || [];
let showConfigureInfo = false;
let configSource = null;
diagnostics
.filter((diagnostic) => diagnostic.source === extensionDisplayName)
.forEach((diagnostic) => {
const ruleName = diagnostic.code;
const ruleNameAlias = diagnostic.message.split(":")[0];
// Provide code action to fix the violation
if (diagnostic.range.isSingleLine && fixFunctions[ruleName]) {
const fixTitle = clickToFix + ruleNameAlias;
const fixAction = new vscode.CodeAction(fixTitle, vscode.CodeActionKind.QuickFix);
fixAction.command = {
"title": fixTitle,
"command": fixLineCommandName,
"arguments": [
diagnostic.range,
ruleName
]
};
fixAction.diagnostics = [ diagnostic ];
// @ts-ignore
fixAction.isPreferred = true;
codeActions.push(fixAction);
}
// Provide code action for information about the violation
const ruleInformation = ruleNameToInformation[ruleName];
if (ruleInformation) {
const infoTitle = clickForInfo + ruleNameAlias;
const infoAction = new vscode.CodeAction(infoTitle, vscode.CodeActionKind.QuickFix);
infoAction.command = {
"title": infoTitle,
"command": openCommand,
"arguments": [ vscode.Uri.parse(ruleInformation) ]
};
infoAction.diagnostics = [ diagnostic ];
codeActions.push(infoAction);
}
showConfigureInfo = true;
configSource = configSource || diagnostic.configSource;
});
// Open the source for the document's rule configuration
if (configSource) {
const configSourceIsSettings =
(configSource === openGlobalSettingsCommand) ||
(configSource === openWorkspaceSettingsCommand) ||
(configSource === openFolderSettingsCommand);
const infoAction = new vscode.CodeAction(clickForConfigSource, vscode.CodeActionKind.QuickFix);
infoAction.command = {
"title": clickForConfigSource,
"command": configSourceIsSettings ?
configSource :
openCommand,
"arguments": configSourceIsSettings ?
null :
[ vscode.Uri.file(configSource) ]
};
codeActions.push(infoAction);
}
// Add information about configuring rules
if (showConfigureInfo) {
const configureInfoAction = new vscode.CodeAction(clickForConfigureInfo, vscode.CodeActionKind.QuickFix);
configureInfoAction.command = {
"title": clickForConfigureInfo,
"command": openCommand,
"arguments": [ vscode.Uri.parse(clickForConfigureUrl) ]
};
codeActions.push(configureInfoAction);
}
return codeActions;
}
// Fixes violations of a rule on a line
function fixLine (range, ruleName) {
return new Promise((resolve, reject) => {
const editor = vscode.window.activeTextEditor;
const line = editor && editor.document.lineAt(range.start.line);
const text = line && line.text.substring(range.start.character, range.end.character);
const fixFunction = fixFunctions[ruleName];
const fixedText = fixFunction && fixFunction(text || "");
if (editor && (typeof fixedText === "string")) {
editor.edit((editBuilder) => {
editBuilder.replace(range, fixedText);
}).then(resolve, reject);
} else {
reject(new Error("Unable to fix rule violaton."));
}
});
}
// Cleanly (i.e., from scratch) lint all visible files
function cleanLintVisibleFiles () {
diagnosticCollection.clear();
didChangeVisibleTextEditors(vscode.window.visibleTextEditors);
}
// Clears the map of custom configuration files and re-lints files
function clearConfigMap (eventUri) {
outputLine("INFO: Resetting configuration cache due to '" + configFileGlob + "' or setting change.");
configMap = {};
if (eventUri) {
cleanLintVisibleFiles();
}
}
// Returns the run setting for the document
function getRun (document) {
const name = document.fileName;
// Use cached configuration if present for file
if (runMap[name]) {
return runMap[name];
}
// Read workspace configuration
const configuration = vscode.workspace.getConfiguration(extensionDisplayName, document.uri);
runMap[name] = configuration.get("run");
outputLine("INFO: Linting for '" + document.fileName + "' will be run '" + runMap[name] + "'.");
return runMap[name];
}
// Clears the map of run settings
function clearRunMap () {
runMap = {};
}
// Suppresses a pending lint for the specified document
function suppressLint (document) {
if (throttle.timeout && (document === throttle.document)) {
clearTimeout(throttle.timeout);
throttle.document = null;
throttle.timeout = null;
}
}
// Requests a lint of the specified document
function requestLint (document) {
suppressLint(document);
throttle.document = document;
throttle.timeout = setTimeout(() => {
// Do not use throttle.document in this function; it may have changed
lint(document);
suppressLint(document);
}, throttleDuration);
}
// Handles the onDidChangeVisibleTextEditors event
function didChangeVisibleTextEditors (textEditors) {
textEditors.forEach((textEditor) => lint(textEditor.document));
}
// Handles the onDidChangeTextDocument event
function didChangeTextDocument (change) {
const document = change.document;
if ((document.languageId === markdownLanguageId) && (getRun(document) === "onType")) {
requestLint(document);
}
}
// Handles the onDidSaveTextDocument event
function didSaveTextDocument (document) {
if ((document.languageId === markdownLanguageId) && (getRun(document) === "onSave")) {
lint(document);
suppressLint(document);
}
}
// Handles the onDidCloseTextDocument event
function didCloseTextDocument (document) {
suppressLint(document);
diagnosticCollection.delete(document.uri);
}
// Handles the onDidChangeConfiguration event
function didChangeConfiguration () {
clearConfigMap();
clearRunMap();
clearCustomRules();
clearIgnores();
cleanLintVisibleFiles();
}
function activate (context) {
// Create OutputChannel
outputChannel = vscode.window.createOutputChannel(extensionDisplayName);
context.subscriptions.push(outputChannel);
// Hook up to workspace events
context.subscriptions.push(
vscode.window.onDidChangeVisibleTextEditors(didChangeVisibleTextEditors),
vscode.workspace.onDidChangeTextDocument(didChangeTextDocument),
vscode.workspace.onDidSaveTextDocument(didSaveTextDocument),
vscode.workspace.onDidCloseTextDocument(didCloseTextDocument),
vscode.workspace.onDidChangeConfiguration(didChangeConfiguration)
);
// Register CodeActionsProvider
context.subscriptions.push(
vscode.languages.registerCodeActionsProvider(documentSelector, {
provideCodeActions
})
);
// Register Command
context.subscriptions.push(
vscode.commands.registerCommand(fixLineCommandName, fixLine)
);
// Create DiagnosticCollection
diagnosticCollection = vscode.languages.createDiagnosticCollection(extensionDisplayName);
context.subscriptions.push(diagnosticCollection);
// Hook up to file system changes for custom config file(s) ("/" vs. "\" due to bug in VS Code glob)
const fileSystemWatcher = vscode.workspace.createFileSystemWatcher("**/" + configFileGlob);
context.subscriptions.push(
fileSystemWatcher,
fileSystemWatcher.onDidCreate(clearConfigMap),
fileSystemWatcher.onDidChange(clearConfigMap),
fileSystemWatcher.onDidDelete(clearConfigMap)
);
// Cancel any pending operations during deactivation
context.subscriptions.push({
"dispose": () => suppressLint(throttle.document)
});
// Request (deferred) lint of active document
if (vscode.window.activeTextEditor) {
requestLint(vscode.window.activeTextEditor.document);
}
}
exports.activate = activate;