mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +01:00
Add basic infrastructure, MD031, and test.
This commit is contained in:
parent
cdec362dc0
commit
d16e1cafc1
5 changed files with 136 additions and 1 deletions
27
lib/markdownlint.js
Normal file
27
lib/markdownlint.js
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
var fs = require("fs");
|
||||||
|
var rules = require("./rules");
|
||||||
|
|
||||||
|
function lintFile(file, options) {
|
||||||
|
var results = {};
|
||||||
|
var contents = fs.readFileSync(file, { encoding: "utf8" });
|
||||||
|
var lines = contents.split(/\r\n|\n/g);
|
||||||
|
Object.keys(rules).forEach(function(name) {
|
||||||
|
var rule = rules[name];
|
||||||
|
var errors = rule(lines);
|
||||||
|
if (errors.length) {
|
||||||
|
results[name] = errors;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = function(options) {
|
||||||
|
var results = {};
|
||||||
|
var files = options.files || [];
|
||||||
|
files.forEach(function(file) {
|
||||||
|
results[file] = lintFile(file, options);
|
||||||
|
});
|
||||||
|
return results;
|
||||||
|
};
|
||||||
30
lib/rules.js
Normal file
30
lib/rules.js
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
function padAndTrim(lines) {
|
||||||
|
return [].concat(
|
||||||
|
"",
|
||||||
|
lines.map(function(line) {
|
||||||
|
return line.trim();
|
||||||
|
}),
|
||||||
|
"");
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
MD031: function(lines) {
|
||||||
|
// Some parsers have trouble detecting fenced code blocks without
|
||||||
|
// surrounding whitespace, so examine the lines directly.
|
||||||
|
lines = padAndTrim(lines);
|
||||||
|
var errors = [];
|
||||||
|
var inCode = false;
|
||||||
|
lines.forEach(function(line, lineNum) {
|
||||||
|
if (line.match(/^(```|~~~)/)) {
|
||||||
|
inCode = !inCode;
|
||||||
|
if ((inCode && lines[lineNum - 1].length) ||
|
||||||
|
(!inCode && lines[lineNum + 1].length)) {
|
||||||
|
errors.push(lineNum);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return errors;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
"name": "markdownlint",
|
"name": "markdownlint",
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"description": "A linting tool for Markdown files",
|
"description": "A linting tool for Markdown files",
|
||||||
"main": "markdownlint.js",
|
"main": "lib/markdownlint.js",
|
||||||
"author": "David Anson (http://dlaa.me/)",
|
"author": "David Anson (http://dlaa.me/)",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"homepage": "https://github.com/DavidAnson/markdownlint",
|
"homepage": "https://github.com/DavidAnson/markdownlint",
|
||||||
|
|
|
||||||
42
test/fenced_code_without_blank_lines.md
Normal file
42
test/fenced_code_without_blank_lines.md
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
```
|
||||||
|
code at start of file
|
||||||
|
```
|
||||||
|
|
||||||
|
text
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
code
|
||||||
|
```
|
||||||
|
|
||||||
|
text
|
||||||
|
``` {MD031}
|
||||||
|
code
|
||||||
|
``` {MD031}
|
||||||
|
text
|
||||||
|
|
||||||
|
```
|
||||||
|
code
|
||||||
|
``` {MD031}
|
||||||
|
text
|
||||||
|
|
||||||
|
text
|
||||||
|
``` {MD031}
|
||||||
|
code
|
||||||
|
```
|
||||||
|
|
||||||
|
text
|
||||||
|
|
||||||
|
```js
|
||||||
|
code
|
||||||
|
code
|
||||||
|
code
|
||||||
|
```
|
||||||
|
|
||||||
|
```html
|
||||||
|
```
|
||||||
|
|
||||||
|
text
|
||||||
|
|
||||||
|
```
|
||||||
|
code at end of file without newline
|
||||||
|
```
|
||||||
36
test/markdownlint-test.js
Normal file
36
test/markdownlint-test.js
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
var fs = require("fs");
|
||||||
|
var path = require("path");
|
||||||
|
var markdownlint = require("../lib/markdownlint");
|
||||||
|
|
||||||
|
function createTestForFile(file) {
|
||||||
|
return function(test) {
|
||||||
|
test.expect(1);
|
||||||
|
var contents = fs.readFileSync(file, { encoding: "utf8" });
|
||||||
|
var lines = contents.split(/\r\n|\n/g);
|
||||||
|
var results = {};
|
||||||
|
lines.forEach(function(line, lineNum) {
|
||||||
|
var match = line.match(/\{(MD\d+)(?::(\d+))?\}/);
|
||||||
|
if (match) {
|
||||||
|
var rule = match[1];
|
||||||
|
var lines = results[rule] || [];
|
||||||
|
lines.push(lineNum + 1);
|
||||||
|
results[rule] = lines;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
var actual = markdownlint({
|
||||||
|
files: [ file ]
|
||||||
|
});
|
||||||
|
var expected = {};
|
||||||
|
expected[file] = results;
|
||||||
|
test.deepEqual(actual, expected, "Line numbers are not correct.");
|
||||||
|
test.done();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fs.readdirSync(__dirname).forEach(function(file) {
|
||||||
|
if (file.match(/\.md$/)) {
|
||||||
|
module.exports[file] = createTestForFile(path.join(__dirname, file));
|
||||||
|
}
|
||||||
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue