Skip to content

Commit

Permalink
Rewrites linter-markdownlint to support Linter Provider v2
Browse files Browse the repository at this point in the history
- Supports Linter 2.3.0 and up. Closes #10
- Adds setting to customize the severity: error (by default), warning and info. Closes #9
- Rewrites package in Javascript / Drops Coffescript
  • Loading branch information
leonelgalan committed Jul 24, 2019
1 parent 22636b3 commit 827bf1c
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 81 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ You can configure _linter-markdownlint_ by editing ~/.atom/config.cson (choose _

```cson
'linter-markdownlint':
executablePath: null
executablePath: 'mdl'
severity: 'error'
```

Run `which mdl` to find the path, if you using _rbenv_ run `rbenv which mdl`
133 changes: 133 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
'use babel';

const { BufferedProcess, CompositeDisposable } = require('atom');

export const config = {
executablePath: {
type: 'string',
default: 'mdl',
description: 'Path to mdl executable',
},
severity: {
type: 'string',
default: 'error',
enum: [
{ value: 'error', description: 'Error' },
{ value: 'warning', description: 'Warning' },
{ value: 'info', description: 'Info' },
],
description: 'Sets the severity of mdl findings',
},
};

class Command {
static setExecutablePath(path) {
this.executablePath = path;
}

static getExecutablePath() {
return this.executablePath || config.executablePath.default;
}

static setSeverity(severity) {
this.severity = severity;
}

static getSeverity() {
return this.severity || config.severity.default;
}
}

class Parser {
static match(line) {
// Example line: /absolute/path/file.md:3: MD013 Line length
const match = /(.+):([0-9]+): +(MD[0-9]{3}) +(.+)/.exec(line);

if (match) {
const text = `${match[3]}: ${match[4]}`;
return {
severity: Command.getSeverity(),
location: {
file: match[1],
position: [[Number(match[2]) - 1, 0], [Number(match[2]) - 1, 0]],
},
excerpt: text,
description: text,
};
}

return undefined;
}

static parse(data) {
const errors = data.map(line => Parser.match(line)).filter(Boolean);
if (errors.length === 0) {
return { passed: true };
} return { passed: false, errors };
}
}

export function activate() {
this.subscriptions = new CompositeDisposable();
this.subscriptions.add(
atom.config.observe(
'linter-markdownlint.executablePath',
executablePath => Command.setExecutablePath(executablePath),
),
);

this.subscriptions.add(
atom.config.observe(
'linter-markdownlint.severity',
severity => Command.setSeverity(severity),
),
);
}

export function deactivate() {
this.subscriptions.dispose();
}

export function provideLinter() {
return {
name: 'markdownlint',
scope: 'file',
lintsOnChange: false,
grammarScopes: ['source.gfm', 'source.pfm'],
lint(textEditor) {
const editorPath = textEditor.getPath();
const cwd = atom.project.relativizePath(editorPath)[0];

return new Promise(((resolve) => {
const lines = [];
const process = new BufferedProcess({
options: { cwd },
command: Command.getExecutablePath(),
args: [editorPath],
stdout(data) {
data.split('\n').forEach(line => lines.push(line));
},
exit(code) {
if (code === 0) {
return resolve([]);
}
const info = Parser.parse(lines);
if (info == null || info.passed) {
return resolve([]);
}
return resolve(info.errors);
},
});

process.onWillThrowError((arg) => {
const { error, handle } = arg;
atom.notifications.addError(`Failed to run ${this.executablePath}`, {
detail: `${error.message}`, dismissable: true,
});
handle();
return resolve([]);
});
}));
},
};
}
78 changes: 0 additions & 78 deletions lib/init.coffee

This file was deleted.

5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "linter-markdownlint",
"activationCommands": {},
"main": "./lib/init",
"main": "./lib/index",
"version": "1.0.2",
"description": "Lint Markdown with mdl",
"keywords": [
Expand All @@ -19,7 +19,7 @@
"providedServices": {
"linter": {
"versions": {
"1.0.0": "provideLinter"
"2.0.0": "provideLinter"
}
}
}
Expand Down

0 comments on commit 827bf1c

Please sign in to comment.