Skip to content

Commit ae0bb57

Browse files
committed
Allow VS Code Diagnostic Severity level to be configured for markdownlint violation severity error and warning (fixes #241).
1 parent b28e52e commit ae0bb57

File tree

4 files changed

+105
-14
lines changed

4 files changed

+105
-14
lines changed

.markdownlint.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666
"### markdownlint.configFile",
6767
"### markdownlint.focusMode",
6868
"### markdownlint.run",
69+
"### markdownlint.severityForError",
70+
"### markdownlint.severityForWarning",
6971
"### markdownlint.customRules",
7072
"### markdownlint.lintWorkspaceGlobs",
7173
"## Suppress",
@@ -77,6 +79,9 @@
7779
"strong-style": {
7880
"style": "asterisk"
7981
},
82+
"table-column-style": {
83+
"style": "aligned"
84+
},
8085
"table-pipe-style": {
8186
"style": "leading_and_trailing"
8287
},

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,37 @@ If you find this distracting, linting can be configured to run only when the doc
303303

304304
> **Note**: When configured to run `onSave`, the list of reported issues will become outdated while the document is edited and will update when the document is saved.
305305
306+
### markdownlint.severityForError
307+
308+
By default, the `markdownlint` library reports violations at severity level `error` and they are displayed in VS Code at Diagnostic Severity `Warning`.
309+
310+
If you want to change the Diagnostic Severity shown by VS Code to something else, the `markdownlint.severityForError` configuration property can be set to any of the following values:
311+
312+
| Value | VS Code Definition |
313+
| ------------- | --------------------------------------------------------------- |
314+
| `Error` | Something not allowed by the rules of a language or other means |
315+
| `Warning` | Something suspicious but allowed |
316+
| `Information` | Something to inform about but not a problem |
317+
| `Hint` | Something to hint to a better way of doing it |
318+
| `Ignore` | *Ignore the violation and do not report it* |
319+
320+
For example:
321+
322+
```json
323+
{
324+
"editor.someSetting": true,
325+
"markdownlint.severityForError": "Information"
326+
}
327+
```
328+
329+
### markdownlint.severityForWarning
330+
331+
This configuration property behaves the same as `markdownlint.severityForError` (see above) and is used for violations reported by the `markdownlint` library at severity level `warning`.
332+
333+
By default, such violations are displayed in VS Code at Diagnostic Severity `Information`.
334+
335+
The value of `severityForWarning` can be the same as the value of `severityForError`.
336+
306337
### markdownlint.customRules
307338

308339
Custom rules can be specified in VS Code's user/workspace configuration to apply additional linting beyond the default set of rules. Custom rules are specified by the path to a JavaScript file or the name of or path to an [npm](https://www.npmjs.com/) package exporting one rule or an array of rules ([examples of custom rules](https://www.npmjs.com/search?q=keywords:markdownlint-rule)).

extension.mjs

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ const codeActionKindSourceFixAllExtension = codeActionKindSourceFixAll.append(ex
6262
const defaultConfig = {
6363
"MD013": false
6464
};
65+
const diagnosticSeverityError = "Error";
66+
const diagnosticSeverityHint = "Hint";
67+
const diagnosticSeverityIgnore = "Ignore";
68+
const diagnosticSeverityInformation = "Information";
69+
const diagnosticSeverityWarning = "Warning";
70+
const diagnosticSeverities = new Set([
71+
diagnosticSeverityError,
72+
diagnosticSeverityHint,
73+
diagnosticSeverityIgnore,
74+
diagnosticSeverityInformation,
75+
diagnosticSeverityWarning
76+
]);
6577

6678
const clickForInfo = "More information about ";
6779
const clickToFixThis = "Fix this violation of ";
@@ -85,6 +97,8 @@ const sectionCustomRules = "customRules";
8597
const sectionFocusMode = "focusMode";
8698
const sectionLintWorkspaceGlobs = "lintWorkspaceGlobs";
8799
const sectionRun = "run";
100+
const sectionSeverityForError = "severityForError";
101+
const sectionSeverityForWarning = "severityForWarning";
88102
const applicationConfigurationSections = [ sectionFocusMode ];
89103
const throttleDuration = 500;
90104
const customRuleExtensionPrefixRe = /^\{([^}]+)\}\/(.*)$/iu;
@@ -328,6 +342,14 @@ function outputLine (message, isError) {
328342
}
329343
}
330344

345+
function getDiagnosticSeverity (configuration, section, fallback) {
346+
let value = configuration.get(section);
347+
if (!diagnosticSeverities.has(value)) {
348+
value = fallback;
349+
}
350+
return vscode.DiagnosticSeverity[value] ?? null;
351+
}
352+
331353
// Returns rule configuration from user/workspace configuration
332354
async function getConfig (fs, configuration, uri) {
333355
let userWorkspaceConfig = configuration.get(sectionConfig);
@@ -430,6 +452,8 @@ async function markdownlintWrapper (document) {
430452
new FsNull() :
431453
new FsWrapper(workspaceFolderUri);
432454
const configuration = vscode.workspace.getConfiguration(extensionDisplayName, document.uri);
455+
const errorSeverity = getDiagnosticSeverity(configuration, sectionSeverityForError, diagnosticSeverityWarning);
456+
const warningSeverity = getDiagnosticSeverity(configuration, sectionSeverityForWarning, diagnosticSeverityInformation);
433457
const config = await getConfig(fs, configuration, document.uri);
434458
const directory = independentDocument ?
435459
null :
@@ -466,7 +490,11 @@ async function markdownlintWrapper (document) {
466490
.catch((error) => import("./stringify-error.mjs").then(
467491
(stringifyError) => outputLine(errorExceptionPrefix + stringifyError.default(error), true)
468492
))
469-
.then(() => results);
493+
.then(() => ({
494+
results,
495+
errorSeverity,
496+
warningSeverity
497+
}));
470498
// If necessary some day to filter results by matching file name...
471499
// .then(() => results.filter((result) => isSchemeUntitled || (result.fileName === path.posix.relative(directory, name))))
472500
}
@@ -541,7 +569,7 @@ function lint (document) {
541569
const targetGeneration = diagnosticGeneration;
542570
// Lint
543571
markdownlintWrapper(document)
544-
.then((results) => {
572+
.then(({ results, errorSeverity, warningSeverity }) => {
545573
const { activeTextEditor } = vscode.window;
546574
for (const result of results) {
547575
// Create Diagnostics
@@ -550,12 +578,15 @@ function lint (document) {
550578
const focusModeRange = (!Number.isInteger(focusMode) || (focusMode < 0)) ?
551579
0 :
552580
focusMode;
581+
const severity = (result.severity === "warning") ? warningSeverity : errorSeverity;
553582
if (
554-
(applicationConfiguration[sectionFocusMode] === false) ||
555-
!activeTextEditor ||
556-
(activeTextEditor.document !== document) ||
557-
(activeTextEditor.selection.active.line < (lineNumber - focusModeRange - 1)) ||
558-
(activeTextEditor.selection.active.line > (lineNumber + focusModeRange - 1))
583+
(severity !== null) && (
584+
(applicationConfiguration[sectionFocusMode] === false) ||
585+
!activeTextEditor ||
586+
(activeTextEditor.document !== document) ||
587+
(activeTextEditor.selection.active.line < (lineNumber - focusModeRange - 1)) ||
588+
(activeTextEditor.selection.active.line > (lineNumber + focusModeRange - 1))
589+
)
559590
) {
560591
const ruleName = result.ruleNames[0];
561592
const ruleDescription = result.ruleDescription;
@@ -571,9 +602,7 @@ function lint (document) {
571602
const end = start + result.errorRange[1];
572603
range = range.with(range.start.with(undefined, start), range.end.with(undefined, end));
573604
}
574-
const severity = (result.severity === "warning") ?
575-
vscode.DiagnosticSeverity.Information :
576-
vscode.DiagnosticSeverity.Warning;
605+
// @ts-ignore
577606
const diagnostic = new vscode.Diagnostic(range, message, severity);
578607
diagnostic.code = ruleInformationUri ?
579608
{
@@ -723,10 +752,10 @@ function fixAll (ruleNameFilter) {
723752
const document = editor.document;
724753
if (isMarkdownDocument(document)) {
725754
return markdownlintWrapper(document)
726-
.then((errors) => {
755+
.then(({ results }) => {
727756
const text = document.getText();
728757
const errorsToFix =
729-
errors.filter((error) => (!ruleNameFilter || (error.ruleNames[0] === ruleNameFilter)));
758+
results.filter((error) => (!ruleNameFilter || (error.ruleNames[0] === ruleNameFilter)));
730759
const fixedText = applyFixes(text, errorsToFix);
731760
return (text === fixedText) ?
732761
null :
@@ -748,8 +777,8 @@ function formatDocument (document, range) {
748777
return new Promise((resolve, reject) => {
749778
if (isMarkdownDocument(document)) {
750779
return markdownlintWrapper(document)
751-
.then((errors) => {
752-
const rangeErrors = errors.filter((error) => {
780+
.then(({ results }) => {
781+
const rangeErrors = results.filter((error) => {
753782
const { fixInfo } = error;
754783
if (fixInfo) {
755784
// eslint-disable-next-line unicorn/consistent-destructuring

package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,32 @@
228228
"onType"
229229
],
230230
"default": "onType"
231+
},
232+
"markdownlint.severityForError": {
233+
"description": "VS Code diagnostic level to use for issues with severity \"error\"",
234+
"scope": "resource",
235+
"type": "string",
236+
"enum": [
237+
"Error",
238+
"Warning",
239+
"Information",
240+
"Hint",
241+
"Ignore"
242+
],
243+
"default": "Warning"
244+
},
245+
"markdownlint.severityForWarning": {
246+
"description": "VS Code diagnostic level to use for issues with severity \"warning\"",
247+
"scope": "resource",
248+
"type": "string",
249+
"enum": [
250+
"Error",
251+
"Warning",
252+
"Information",
253+
"Hint",
254+
"Ignore"
255+
],
256+
"default": "Information"
231257
}
232258
}
233259
}

0 commit comments

Comments
 (0)