Add support for using custom rules.

This commit is contained in:
David Anson 2018-02-15 21:35:58 -08:00
parent 4619a8c824
commit f24f98e146
11 changed files with 497 additions and 71 deletions

22
test/rules/blockquote.js Normal file
View file

@ -0,0 +1,22 @@
// @ts-check
"use strict";
module.exports = {
"names": [ "blockquote" ],
"description": "Rule that reports an error for blockquotes",
"tags": [ "test" ],
"function": function rule(params, onError) {
params.tokens.filter(function filterToken(token) {
return token.type === "blockquote_open";
}).forEach(function forToken(blockquote) {
var lines = blockquote.map[1] - blockquote.map[0];
onError({
"lineNumber": blockquote.lineNumber,
"detail": "Blockquote spans " + lines + " line(s).",
"context": blockquote.line.substr(0, 7),
"range": null
});
});
}
};

View file

@ -0,0 +1,23 @@
// @ts-check
"use strict";
module.exports = {
"names": [ "every-n-lines" ],
"description": "Rule that reports an error every N lines",
"tags": [ "test" ],
"function": function rule(params, onError) {
var n = params.config.n || 2;
params.lines.forEach(function forLine(line, lineIndex) {
var lineNumber = lineIndex + 1;
if ((lineNumber % n) === 0) {
onError({
"lineNumber": lineNumber,
"detail": "Line number " + lineNumber,
"context": null,
"range": null
});
}
});
}
};

28
test/rules/letters-E-X.js Normal file
View file

@ -0,0 +1,28 @@
// @ts-check
"use strict";
module.exports = {
"names": [ "letters-E-X", "letter-E-letter-X", "contains-ex" ],
"description": "Rule that reports an error for lines with the letters 'EX'",
"tags": [ "test" ],
"function": function rule(params, onError) {
params.tokens.filter(function filterToken(token) {
return token.type === "inline";
}).forEach(function forToken(inline) {
inline.children.filter(function filterChild(child) {
return child.type === "text";
}).forEach(function forChild(text) {
var index = text.content.toLowerCase().indexOf("ex");
if (index !== -1) {
onError({
"lineNumber": text.lineNumber,
"detail": null,
"context": text.content.substr(index - 1, 4),
"range": null
});
}
});
});
}
};

13
test/rules/package.json Normal file
View file

@ -0,0 +1,13 @@
{
"name": "markdownlint-rules-test",
"version": "0.0.1",
"description": "Package of markdownlint custom rules used for testing",
"main": "rules.js",
"author": "David Anson (https://dlaa.me/)",
"homepage": "https://github.com/DavidAnson/markdownlint",
"license": "MIT",
"keywords": [
"markdownlint-rules"
],
"private": true
}

18
test/rules/rules.js Normal file
View file

@ -0,0 +1,18 @@
// @ts-check
"use strict";
var blockquote = require("./blockquote");
module.exports.blockquote = blockquote;
var everyNLines = require("./every-n-lines");
module.exports.everyNLines = everyNLines;
var lettersEX = require("./letters-E-X");
module.exports.lettersEX = lettersEX;
module.exports.all = [
blockquote,
everyNLines,
lettersEX
];