2015-02-23 23:39:20 -08:00
|
|
|
"use strict";
|
|
|
|
|
2015-03-08 23:08:43 -07:00
|
|
|
var shared = require("./shared");
|
|
|
|
|
2015-03-16 22:31:18 -07:00
|
|
|
// Returns the indent for a token
|
2015-03-04 18:23:19 -08:00
|
|
|
function indentFor(token) {
|
2015-03-02 23:52:39 -08:00
|
|
|
return token.line.length - token.line.trimLeft().length;
|
2015-02-25 18:19:36 -08:00
|
|
|
}
|
|
|
|
|
2015-03-16 22:31:18 -07:00
|
|
|
// Returns the heading style for a heading token
|
2015-03-04 18:23:19 -08:00
|
|
|
function headingStyleFor(token) {
|
2015-03-18 23:14:44 -07:00
|
|
|
if ((token.map[1] - token.map[0]) === 1) {
|
2015-03-06 09:21:55 -08:00
|
|
|
if (/#\s*$/.test(token.line)) {
|
2015-02-27 22:06:54 -08:00
|
|
|
return "atx_closed";
|
|
|
|
}
|
|
|
|
return "atx";
|
|
|
|
}
|
|
|
|
return "setext";
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:31:18 -07:00
|
|
|
// Returns the unordered list style for a list item token
|
2015-03-04 18:23:19 -08:00
|
|
|
function unorderedListStyleFor(token) {
|
2015-03-02 23:52:39 -08:00
|
|
|
switch (token.line.trimLeft().substr(0, 1)) {
|
2015-03-01 22:56:52 -08:00
|
|
|
case "-":
|
|
|
|
return "dash";
|
|
|
|
case "+":
|
|
|
|
return "plus";
|
2015-03-10 23:10:06 -07:00
|
|
|
case "*":
|
2015-03-01 22:56:52 -08:00
|
|
|
default:
|
2015-03-10 23:10:06 -07:00
|
|
|
return "asterisk";
|
2015-03-01 22:56:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-29 18:18:20 -07:00
|
|
|
// Calls the provided function for each matching token
|
2015-06-12 09:37:11 -07:00
|
|
|
function filterTokens(params, type, callback) {
|
|
|
|
(params.tokenLists[type] || []).forEach(callback);
|
2015-03-04 18:23:19 -08:00
|
|
|
}
|
|
|
|
|
2015-03-16 22:31:18 -07:00
|
|
|
// Calls the provided function for each line (with context)
|
2015-03-09 00:31:07 -07:00
|
|
|
function forEachLine(params, callback) {
|
2015-06-16 17:50:55 -07:00
|
|
|
if (!params.forEachLine) {
|
|
|
|
var lineMetadata = new Array(params.lines.length);
|
|
|
|
var inFence = false;
|
|
|
|
// Find fenced code by pattern (parser ignores "``` close fence")
|
|
|
|
params.lines.forEach(function forLine(line, lineIndex) {
|
|
|
|
var metadata = 0;
|
|
|
|
if (/^(```|~~~)/.test(line)) {
|
|
|
|
metadata = inFence ? -2 : 2;
|
|
|
|
inFence = !inFence;
|
|
|
|
} else if (inFence) {
|
|
|
|
metadata = 1;
|
|
|
|
}
|
|
|
|
lineMetadata[lineIndex] = metadata;
|
|
|
|
});
|
|
|
|
// Find code blocks normally
|
|
|
|
filterTokens(params, "code_block", function forToken(token) {
|
|
|
|
for (var i = token.map[0]; i < token.map[1]; i++) {
|
|
|
|
lineMetadata[i] = 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
params.forEachLine = lineMetadata;
|
|
|
|
}
|
|
|
|
// Invoke callback
|
2015-03-09 00:31:07 -07:00
|
|
|
params.lines.forEach(function forLine(line, lineIndex) {
|
2015-06-16 17:50:55 -07:00
|
|
|
var metadata = params.forEachLine[lineIndex];
|
|
|
|
callback(line, lineIndex, !!metadata, (metadata >> 1));
|
2015-03-09 00:31:07 -07:00
|
|
|
});
|
2015-02-23 23:39:20 -08:00
|
|
|
}
|
|
|
|
|
2015-04-15 18:24:42 -07:00
|
|
|
// Calls the provided function for each specified inline child token
|
|
|
|
function forEachInlineChild(params, type, callback) {
|
2015-06-12 09:37:11 -07:00
|
|
|
filterTokens(params, "inline", function forToken(token) {
|
|
|
|
token.children.forEach(function forChild(child) {
|
|
|
|
if (child.type === type) {
|
|
|
|
callback(child, token);
|
|
|
|
}
|
2015-04-15 17:50:01 -07:00
|
|
|
});
|
2015-05-29 18:18:20 -07:00
|
|
|
});
|
2015-04-15 17:50:01 -07:00
|
|
|
}
|
|
|
|
|
2015-03-16 22:31:18 -07:00
|
|
|
// Calls the provided function for each heading's content
|
2015-03-11 18:40:46 -07:00
|
|
|
function forEachHeading(params, callback) {
|
|
|
|
var heading = null;
|
|
|
|
params.tokens.forEach(function forToken(token) {
|
|
|
|
if (token.type === "heading_open") {
|
|
|
|
heading = token;
|
|
|
|
} else if (token.type === "heading_close") {
|
|
|
|
heading = null;
|
|
|
|
} else if ((token.type === "inline") && heading) {
|
|
|
|
callback(heading, token.content);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:31:18 -07:00
|
|
|
// Returns (nested) lists as a flat array (in order)
|
2015-06-11 18:33:40 -07:00
|
|
|
function flattenLists(params) {
|
|
|
|
if (!params.flattenLists) {
|
|
|
|
var lists = [];
|
|
|
|
var stack = [];
|
|
|
|
var current = null;
|
|
|
|
var lastWithMap = { "map": [ 0, 1 ] };
|
|
|
|
params.tokens.forEach(function forToken(token) {
|
|
|
|
if ((token.type === "bullet_list_open") ||
|
|
|
|
(token.type === "ordered_list_open")) {
|
|
|
|
// Save current context and start a new one
|
|
|
|
stack.push(current);
|
|
|
|
current = {
|
|
|
|
"ordered": (token.type === "ordered_list_open"),
|
|
|
|
"open": token,
|
|
|
|
"items": [],
|
|
|
|
"nesting": stack.length - 1,
|
|
|
|
"lastLineIndex": -1,
|
|
|
|
"insert": lists.length
|
|
|
|
};
|
|
|
|
} else if ((token.type === "bullet_list_close") ||
|
|
|
|
(token.type === "ordered_list_close")) {
|
|
|
|
// Finalize current context and restore previous
|
|
|
|
current.lastLineIndex = lastWithMap.map[1];
|
2015-03-10 23:10:06 -07:00
|
|
|
lists.splice(current.insert, 0, current);
|
|
|
|
delete current.insert;
|
2015-06-11 18:33:40 -07:00
|
|
|
current = stack.pop();
|
|
|
|
} else if (token.type === "list_item_open") {
|
|
|
|
// Add list item
|
|
|
|
current.items.push(token);
|
|
|
|
} else if (token.map) {
|
|
|
|
// Track last token with map
|
|
|
|
lastWithMap = token;
|
2015-03-10 23:10:06 -07:00
|
|
|
}
|
2015-06-11 18:33:40 -07:00
|
|
|
});
|
|
|
|
params.flattenLists = lists;
|
|
|
|
}
|
|
|
|
return params.flattenLists;
|
2015-03-10 23:10:06 -07:00
|
|
|
}
|
|
|
|
|
2015-02-24 23:50:37 -08:00
|
|
|
module.exports = [
|
2015-02-25 18:19:36 -08:00
|
|
|
{
|
|
|
|
"name": "MD001",
|
|
|
|
"desc": "Header levels should only increment by one level at a time",
|
2015-03-16 22:31:18 -07:00
|
|
|
"tags": [ "headers" ],
|
2015-02-27 22:06:54 -08:00
|
|
|
"func": function MD001(params, errors) {
|
2015-02-25 18:19:36 -08:00
|
|
|
var prevLevel = 0;
|
2015-06-12 09:37:11 -07:00
|
|
|
filterTokens(params, "heading_open", function forToken(token) {
|
2015-05-29 18:18:20 -07:00
|
|
|
var level = parseInt(token.tag.slice(1), 10);
|
|
|
|
if (prevLevel && (level > prevLevel + 1)) {
|
|
|
|
errors.push(token.lineNumber);
|
|
|
|
}
|
|
|
|
prevLevel = level;
|
|
|
|
});
|
2015-02-25 18:19:36 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-02-25 18:00:08 -08:00
|
|
|
{
|
|
|
|
"name": "MD002",
|
|
|
|
"desc": "First header should be a h1 header",
|
2015-03-16 22:31:18 -07:00
|
|
|
"tags": [ "headers" ],
|
2015-02-27 22:06:54 -08:00
|
|
|
"func": function MD002(params, errors) {
|
|
|
|
params.tokens.every(function forToken(token) {
|
2015-02-25 18:19:36 -08:00
|
|
|
if (token.type === "heading_open") {
|
2015-03-18 23:14:44 -07:00
|
|
|
if (token.tag !== "h1") {
|
2015-03-02 23:52:39 -08:00
|
|
|
errors.push(token.lineNumber);
|
2015-02-25 18:19:36 -08:00
|
|
|
}
|
2015-02-25 18:00:08 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-02-27 22:06:54 -08:00
|
|
|
{
|
|
|
|
"name": "MD003",
|
|
|
|
"desc": "Header style",
|
2015-03-16 22:31:18 -07:00
|
|
|
"tags": [ "headers" ],
|
2015-02-27 22:06:54 -08:00
|
|
|
"func": function MD003(params, errors) {
|
|
|
|
var style = params.options.style || "consistent";
|
2015-06-12 09:37:11 -07:00
|
|
|
filterTokens(params, "heading_open", function forToken(token) {
|
2015-05-29 18:18:20 -07:00
|
|
|
if (style === "consistent") {
|
|
|
|
style = headingStyleFor(token);
|
|
|
|
}
|
2015-03-04 18:23:19 -08:00
|
|
|
if (headingStyleFor(token) !== style) {
|
2015-03-02 23:52:39 -08:00
|
|
|
errors.push(token.lineNumber);
|
2015-02-27 22:06:54 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-03-01 22:56:52 -08:00
|
|
|
{
|
|
|
|
"name": "MD004",
|
|
|
|
"desc": "Unordered list style",
|
2015-03-16 22:31:18 -07:00
|
|
|
"tags": [ "bullet", "ul" ],
|
2015-03-01 22:56:52 -08:00
|
|
|
"func": function MD004(params, errors) {
|
|
|
|
var style = params.options.style || "consistent";
|
2015-06-11 18:33:40 -07:00
|
|
|
flattenLists(params).forEach(function forList(list) {
|
|
|
|
if (!list.ordered) {
|
|
|
|
if (style === "consistent") {
|
|
|
|
style = unorderedListStyleFor(list.items[0]);
|
2015-03-10 23:10:06 -07:00
|
|
|
}
|
2015-06-11 18:33:40 -07:00
|
|
|
list.items.forEach(function forItem(item) {
|
|
|
|
if (unorderedListStyleFor(item) !== style) {
|
|
|
|
errors.push(item.lineNumber);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-03-01 22:56:52 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-03-02 23:30:40 -08:00
|
|
|
{
|
|
|
|
"name": "MD005",
|
|
|
|
"desc": "Inconsistent indentation for list items at the same level",
|
2015-03-16 22:31:18 -07:00
|
|
|
"tags": [ "bullet", "ul", "indentation" ],
|
2015-03-02 23:30:40 -08:00
|
|
|
"func": function MD005(params, errors) {
|
2015-06-11 18:33:40 -07:00
|
|
|
flattenLists(params).forEach(function forList(list) {
|
2015-03-10 23:10:06 -07:00
|
|
|
var indent = indentFor(list.items[0]);
|
|
|
|
list.items.forEach(function forItem(item) {
|
|
|
|
if (indentFor(item) !== indent) {
|
|
|
|
errors.push(item.lineNumber);
|
2015-03-04 18:23:19 -08:00
|
|
|
}
|
|
|
|
});
|
2015-03-10 23:10:06 -07:00
|
|
|
});
|
2015-03-02 23:30:40 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-03-03 09:29:13 -08:00
|
|
|
{
|
|
|
|
"name": "MD006",
|
|
|
|
"desc": "Consider starting bulleted lists at the beginning of the line",
|
2015-03-16 22:31:18 -07:00
|
|
|
"tags": [ "bullet", "ul", "indentation" ],
|
2015-03-03 09:29:13 -08:00
|
|
|
"func": function MD006(params, errors) {
|
2015-06-11 18:33:40 -07:00
|
|
|
flattenLists(params).forEach(function forList(list) {
|
|
|
|
if (!list.ordered && !list.nesting && indentFor(list.open)) {
|
2015-03-10 23:10:06 -07:00
|
|
|
errors.push(list.open.lineNumber);
|
2015-03-03 09:29:13 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-03-02 23:30:40 -08:00
|
|
|
{
|
|
|
|
"name": "MD007",
|
|
|
|
"desc": "Unordered list indentation",
|
2015-03-16 22:31:18 -07:00
|
|
|
"tags": [ "bullet", "ul", "indentation" ],
|
2015-03-02 23:30:40 -08:00
|
|
|
"func": function MD007(params, errors) {
|
|
|
|
var optionsIndent = params.options.indent || 2;
|
|
|
|
var prevIndent = 0;
|
2015-06-11 18:33:40 -07:00
|
|
|
flattenLists(params).forEach(function forList(list) {
|
|
|
|
if (!list.ordered) {
|
|
|
|
var indent = indentFor(list.open);
|
|
|
|
if ((indent > prevIndent) &&
|
|
|
|
((indent - prevIndent) !== optionsIndent)) {
|
|
|
|
errors.push(list.open.lineNumber);
|
|
|
|
}
|
|
|
|
prevIndent = indent;
|
2015-03-10 23:10:06 -07:00
|
|
|
}
|
|
|
|
});
|
2015-03-02 23:30:40 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-03-03 09:29:13 -08:00
|
|
|
{
|
|
|
|
"name": "MD009",
|
|
|
|
"desc": "Trailing spaces",
|
2015-03-16 22:31:18 -07:00
|
|
|
"tags": [ "whitespace" ],
|
2015-03-03 09:29:13 -08:00
|
|
|
"func": function MD009(params, errors) {
|
2015-04-14 09:07:25 -07:00
|
|
|
var brSpaces = params.options.br_spaces || 0;
|
2015-03-03 09:29:13 -08:00
|
|
|
params.lines.forEach(function forLine(line, lineIndex) {
|
2015-04-14 09:07:25 -07:00
|
|
|
if (/\s$/.test(line) &&
|
|
|
|
((brSpaces < 2) ||
|
|
|
|
(line.length - line.trimRight().length !== brSpaces))) {
|
2015-03-03 09:29:13 -08:00
|
|
|
errors.push(lineIndex + 1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"name": "MD010",
|
|
|
|
"desc": "Hard tabs",
|
2015-03-17 22:34:47 -07:00
|
|
|
"tags": [ "whitespace", "hard_tab" ],
|
2015-03-03 09:29:13 -08:00
|
|
|
"func": function MD010(params, errors) {
|
|
|
|
params.lines.forEach(function forLine(line, lineIndex) {
|
2015-03-06 09:21:55 -08:00
|
|
|
if (/\t/.test(line)) {
|
2015-03-03 09:29:13 -08:00
|
|
|
errors.push(lineIndex + 1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-03-03 18:28:59 -08:00
|
|
|
{
|
|
|
|
"name": "MD011",
|
|
|
|
"desc": "Reversed link syntax",
|
2015-03-16 22:31:18 -07:00
|
|
|
"tags": [ "links" ],
|
2015-03-03 18:28:59 -08:00
|
|
|
"func": function MD011(params, errors) {
|
2015-04-15 18:24:42 -07:00
|
|
|
forEachInlineChild(params, "text", function forToken(token) {
|
2015-04-15 17:50:01 -07:00
|
|
|
if (/\([^)]+\)\[[^\]]+\]/.test(token.content)) {
|
|
|
|
errors.push(token.lineNumber);
|
|
|
|
}
|
|
|
|
});
|
2015-03-03 18:28:59 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-03-04 18:09:46 -08:00
|
|
|
{
|
|
|
|
"name": "MD012",
|
|
|
|
"desc": "Multiple consecutive blank lines",
|
2015-03-16 22:31:18 -07:00
|
|
|
"tags": [ "whitespace", "blank_lines" ],
|
2015-03-04 18:09:46 -08:00
|
|
|
"func": function MD012(params, errors) {
|
|
|
|
var prevLine = "-";
|
2015-03-09 00:31:07 -07:00
|
|
|
forEachLine(params, function forLine(line, lineIndex, inCode) {
|
2015-03-04 18:09:46 -08:00
|
|
|
line = line.trim();
|
2015-03-09 00:31:07 -07:00
|
|
|
if (!inCode && !line.length && !prevLine.length) {
|
2015-03-04 18:09:46 -08:00
|
|
|
errors.push(lineIndex + 1);
|
|
|
|
}
|
|
|
|
prevLine = line;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-03-04 18:36:30 -08:00
|
|
|
{
|
|
|
|
"name": "MD013",
|
|
|
|
"desc": "Line length",
|
2015-03-16 22:31:18 -07:00
|
|
|
"tags": [ "line_length" ],
|
2015-03-04 18:36:30 -08:00
|
|
|
"func": function MD013(params, errors) {
|
|
|
|
var lineLength = params.options.line_length || 80;
|
2015-03-16 22:31:18 -07:00
|
|
|
var re = new RegExp("^.{" + lineLength + "}.*\\s");
|
2015-03-04 18:36:30 -08:00
|
|
|
params.lines.forEach(function forLine(line, lineIndex) {
|
2015-03-16 22:31:18 -07:00
|
|
|
if (re.test(line)) {
|
2015-03-04 18:36:30 -08:00
|
|
|
errors.push(lineIndex + 1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-03-05 23:30:01 -08:00
|
|
|
{
|
|
|
|
"name": "MD014",
|
|
|
|
"desc": "Dollar signs used before commands without showing output",
|
2015-03-16 22:31:18 -07:00
|
|
|
"tags": [ "code" ],
|
2015-03-05 23:30:01 -08:00
|
|
|
"func": function MD014(params, errors) {
|
2015-05-29 18:18:20 -07:00
|
|
|
[ "code_block", "fence" ].forEach(function forType(type) {
|
2015-06-12 09:37:11 -07:00
|
|
|
filterTokens(params, type, function forToken(token) {
|
2015-03-11 18:40:46 -07:00
|
|
|
if (token.content && token.content.split(shared.newLineRe)
|
2015-05-31 22:29:01 -07:00
|
|
|
.every(function forLine(line) {
|
|
|
|
return !line || /^\$\s/.test(line);
|
2015-03-05 23:30:01 -08:00
|
|
|
})) {
|
2015-03-06 09:21:55 -08:00
|
|
|
errors.push(token.lineNumber);
|
|
|
|
}
|
|
|
|
});
|
2015-05-29 18:18:20 -07:00
|
|
|
});
|
2015-03-06 09:21:55 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"name": "MD018",
|
|
|
|
"desc": "No space after hash on atx style header",
|
2015-03-16 22:31:18 -07:00
|
|
|
"tags": [ "headers", "atx", "spaces" ],
|
2015-03-06 09:21:55 -08:00
|
|
|
"func": function MD018(params, errors) {
|
2015-03-09 00:31:07 -07:00
|
|
|
forEachLine(params, function forLine(line, lineIndex, inCode) {
|
|
|
|
if (!inCode && /^#+[^#\s]/.test(line) && !/#$/.test(line)) {
|
2015-03-06 09:21:55 -08:00
|
|
|
errors.push(lineIndex + 1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"name": "MD019",
|
|
|
|
"desc": "Multiple spaces after hash on atx style header",
|
2015-03-16 22:31:18 -07:00
|
|
|
"tags": [ "headers", "atx", "spaces" ],
|
2015-03-06 09:21:55 -08:00
|
|
|
"func": function MD019(params, errors) {
|
2015-06-12 09:37:11 -07:00
|
|
|
filterTokens(params, "heading_open", function forToken(token) {
|
2015-05-29 18:18:20 -07:00
|
|
|
if ((headingStyleFor(token) === "atx") &&
|
|
|
|
/^#+\s\s/.test(token.line)) {
|
|
|
|
errors.push(token.lineNumber);
|
|
|
|
}
|
|
|
|
});
|
2015-03-06 18:16:16 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"name": "MD020",
|
|
|
|
"desc": "No space inside hashes on closed atx style header",
|
2015-03-16 22:31:18 -07:00
|
|
|
"tags": [ "headers", "atx_closed", "spaces" ],
|
2015-03-06 18:16:16 -08:00
|
|
|
"func": function MD020(params, errors) {
|
2015-03-09 00:31:07 -07:00
|
|
|
forEachLine(params, function forLine(line, lineIndex, inCode) {
|
|
|
|
if (!inCode && /^#+[^#]*[^\\]#+$/.test(line) &&
|
2015-03-06 18:16:16 -08:00
|
|
|
(/^#+[^#\s]/.test(line) || /[^#\s]#+$/.test(line))) {
|
|
|
|
errors.push(lineIndex + 1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"name": "MD021",
|
|
|
|
"desc": "Multiple spaces inside hashes on closed atx style header",
|
2015-03-16 22:31:18 -07:00
|
|
|
"tags": [ "headers", "atx_closed", "spaces" ],
|
2015-03-06 18:16:16 -08:00
|
|
|
"func": function MD021(params, errors) {
|
2015-06-12 09:37:11 -07:00
|
|
|
filterTokens(params, "heading_open", function forToken(token) {
|
2015-05-29 18:18:20 -07:00
|
|
|
if ((headingStyleFor(token) === "atx_closed") &&
|
|
|
|
(/^#+\s\s/.test(token.line) || /\s\s#+$/.test(token.line))) {
|
|
|
|
errors.push(token.lineNumber);
|
|
|
|
}
|
|
|
|
});
|
2015-03-05 23:30:01 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-03-08 23:08:43 -07:00
|
|
|
{
|
|
|
|
"name": "MD022",
|
|
|
|
"desc": "Headers should be surrounded by blank lines",
|
2015-03-16 22:31:18 -07:00
|
|
|
"tags": [ "headers", "blank_lines" ],
|
2015-03-08 23:08:43 -07:00
|
|
|
"func": function MD022(params, errors) {
|
|
|
|
var prevHeadingLineNumber = 0;
|
|
|
|
var prevMaxLineIndex = -1;
|
|
|
|
var needBlankLine = false;
|
|
|
|
params.tokens.forEach(function forToken(token) {
|
|
|
|
if (token.type === "heading_open") {
|
2015-03-18 23:14:44 -07:00
|
|
|
if ((token.map[0] - prevMaxLineIndex) === 0) {
|
2015-03-08 23:08:43 -07:00
|
|
|
errors.push(token.lineNumber);
|
|
|
|
}
|
|
|
|
prevHeadingLineNumber = token.lineNumber;
|
|
|
|
} else if (token.type === "heading_close") {
|
|
|
|
needBlankLine = true;
|
|
|
|
} else if (token.type === "inline") {
|
|
|
|
token.content.split(shared.newLineRe)
|
|
|
|
.forEach(function forLine(line, offset) {
|
|
|
|
if (/^(-+|=+)\s*$/.test(line)) {
|
2015-03-18 23:14:44 -07:00
|
|
|
errors.push(token.map[0] + offset);
|
2015-03-08 23:08:43 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-03-18 23:14:44 -07:00
|
|
|
if (token.map) {
|
2015-03-08 23:08:43 -07:00
|
|
|
if (needBlankLine) {
|
2015-03-18 23:14:44 -07:00
|
|
|
if ((token.map[0] - prevMaxLineIndex) === 0) {
|
2015-03-08 23:08:43 -07:00
|
|
|
errors.push(prevHeadingLineNumber);
|
|
|
|
}
|
|
|
|
needBlankLine = false;
|
|
|
|
}
|
2015-03-18 23:14:44 -07:00
|
|
|
prevMaxLineIndex = Math.max(prevMaxLineIndex, token.map[1]);
|
2015-03-08 23:08:43 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-03-11 09:30:34 -07:00
|
|
|
{
|
|
|
|
"name": "MD023",
|
|
|
|
"desc": "Headers must start at the beginning of the line",
|
2015-03-16 22:31:18 -07:00
|
|
|
"tags": [ "headers", "spaces" ],
|
2015-03-11 09:30:34 -07:00
|
|
|
"func": function MD023(params, errors) {
|
2015-06-12 09:37:11 -07:00
|
|
|
filterTokens(params, "heading_open", function forToken(token) {
|
2015-05-29 18:18:20 -07:00
|
|
|
if (/^\s/.test(token.line)) {
|
|
|
|
errors.push(token.lineNumber);
|
|
|
|
}
|
|
|
|
});
|
2015-03-11 09:30:34 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"name": "MD024",
|
|
|
|
"desc": "Multiple headers with the same content",
|
2015-03-16 22:31:18 -07:00
|
|
|
"tags": [ "headers" ],
|
2015-03-11 09:30:34 -07:00
|
|
|
"func": function MD024(params, errors) {
|
2015-03-11 18:40:46 -07:00
|
|
|
var knownContent = [];
|
|
|
|
forEachHeading(params, function forHeading(heading, content) {
|
|
|
|
if (knownContent.indexOf(content) === -1) {
|
|
|
|
knownContent.push(content);
|
|
|
|
} else {
|
|
|
|
errors.push(heading.lineNumber);
|
2015-03-11 09:30:34 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"name": "MD025",
|
|
|
|
"desc": "Multiple top level headers in the same document",
|
2015-03-16 22:31:18 -07:00
|
|
|
"tags": [ "headers" ],
|
2015-03-11 09:30:34 -07:00
|
|
|
"func": function MD025(params, errors) {
|
|
|
|
var hasTopLevelHeading = false;
|
2015-06-12 09:37:11 -07:00
|
|
|
filterTokens(params, "heading_open", function forToken(token) {
|
2015-05-29 18:18:20 -07:00
|
|
|
if (token.tag === "h1") {
|
|
|
|
if (hasTopLevelHeading) {
|
|
|
|
errors.push(token.lineNumber);
|
|
|
|
} else if (token.lineNumber === 1) {
|
|
|
|
hasTopLevelHeading = true;
|
2015-03-11 09:30:34 -07:00
|
|
|
}
|
2015-05-29 18:18:20 -07:00
|
|
|
}
|
|
|
|
});
|
2015-03-11 09:30:34 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-03-11 18:40:46 -07:00
|
|
|
{
|
|
|
|
"name": "MD026",
|
|
|
|
"desc": "Trailing punctuation in header",
|
2015-03-16 22:31:18 -07:00
|
|
|
"tags": [ "headers" ],
|
2015-03-11 18:40:46 -07:00
|
|
|
"func": function MD026(params, errors) {
|
|
|
|
var punctuation = params.options.punctuation || ".,;:!?";
|
|
|
|
var re = new RegExp("[" + punctuation + "]$");
|
|
|
|
forEachHeading(params, function forHeading(heading, content) {
|
|
|
|
if (re.test(content)) {
|
|
|
|
errors.push(heading.lineNumber);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"name": "MD027",
|
|
|
|
"desc": "Multiple spaces after blockquote symbol",
|
2015-03-16 22:31:18 -07:00
|
|
|
"tags": [ "blockquote", "whitespace", "indentation" ],
|
2015-03-11 18:40:46 -07:00
|
|
|
"func": function MD027(params, errors) {
|
|
|
|
var inBlockquote = false;
|
|
|
|
params.tokens.forEach(function forToken(token) {
|
|
|
|
if (token.type === "blockquote_open") {
|
|
|
|
inBlockquote = true;
|
|
|
|
} else if (token.type === "blockquote_close") {
|
|
|
|
inBlockquote = false;
|
|
|
|
} else if ((token.type === "inline") && inBlockquote) {
|
|
|
|
token.content.split(shared.newLineRe)
|
|
|
|
.forEach(function forLine(line, offset) {
|
|
|
|
if (/^\s/.test(line) ||
|
|
|
|
(!offset && /^\s*>\s\s/.test(token.line))) {
|
|
|
|
errors.push(token.lineNumber + offset);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-03-04 18:09:46 -08:00
|
|
|
{
|
|
|
|
"name": "MD028",
|
|
|
|
"desc": "Blank line inside blockquote",
|
2015-03-16 22:31:18 -07:00
|
|
|
"tags": [ "blockquote", "whitespace" ],
|
2015-03-04 18:09:46 -08:00
|
|
|
"func": function MD028(params, errors) {
|
|
|
|
var prevToken = {};
|
|
|
|
params.tokens.forEach(function forToken(token) {
|
|
|
|
if ((token.type === "blockquote_open") &&
|
|
|
|
(prevToken.type === "blockquote_close")) {
|
|
|
|
errors.push(token.lineNumber - 1);
|
|
|
|
}
|
|
|
|
prevToken = token;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-03-07 22:46:45 -08:00
|
|
|
{
|
|
|
|
"name": "MD029",
|
|
|
|
"desc": "Ordered list item prefix",
|
2015-03-16 22:31:18 -07:00
|
|
|
"tags": [ "ol" ],
|
2015-03-07 22:46:45 -08:00
|
|
|
"func": function MD029(params, errors) {
|
|
|
|
var style = params.options.style || "one";
|
2015-06-11 18:33:40 -07:00
|
|
|
flattenLists(params).forEach(function forList(list) {
|
|
|
|
if (list.ordered) {
|
|
|
|
var number = 1;
|
|
|
|
list.items.forEach(function forItem(item) {
|
|
|
|
var re = new RegExp("^\\s*" + String(number) + "\\.");
|
|
|
|
if (!re.test(item.line)) {
|
|
|
|
errors.push(item.lineNumber);
|
|
|
|
}
|
|
|
|
if (style === "ordered") {
|
|
|
|
number++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-03-10 23:10:06 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"name": "MD030",
|
|
|
|
"desc": "Spaces after list markers",
|
2015-03-16 22:31:18 -07:00
|
|
|
"tags": [ "ol", "ul", "whitespace" ],
|
2015-03-10 23:10:06 -07:00
|
|
|
"func": function MD030(params, errors) {
|
|
|
|
var ulSingle = params.options.ul_single || 1;
|
|
|
|
var olSingle = params.options.ol_single || 1;
|
|
|
|
var ulMulti = params.options.ul_multi || 1;
|
|
|
|
var olMulti = params.options.ol_multi || 1;
|
2015-06-11 18:33:40 -07:00
|
|
|
flattenLists(params).forEach(function forList(list) {
|
2015-03-18 23:14:44 -07:00
|
|
|
var lineCount = list.lastLineIndex - list.open.map[0];
|
2015-03-10 23:10:06 -07:00
|
|
|
var allSingle = lineCount === list.items.length;
|
|
|
|
var expectedSpaces = list.ordered ?
|
|
|
|
(allSingle ? olSingle : olMulti) :
|
|
|
|
(allSingle ? ulSingle : ulMulti);
|
|
|
|
list.items.forEach(function forItem(item) {
|
|
|
|
var match = /^\s*\S+(\s+)/.exec(item.line);
|
2015-05-03 20:48:07 -07:00
|
|
|
if (!match || (match[1].length !== expectedSpaces)) {
|
2015-03-10 23:10:06 -07:00
|
|
|
errors.push(item.lineNumber);
|
|
|
|
}
|
|
|
|
});
|
2015-03-07 22:46:45 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-02-24 23:50:37 -08:00
|
|
|
{
|
|
|
|
"name": "MD031",
|
|
|
|
"desc": "Fenced code blocks should be surrounded by blank lines",
|
2015-03-16 22:31:18 -07:00
|
|
|
"tags": [ "code", "blank_lines" ],
|
2015-02-27 22:06:54 -08:00
|
|
|
"func": function MD031(params, errors) {
|
2015-06-16 17:50:55 -07:00
|
|
|
var lines = params.lines;
|
|
|
|
forEachLine(params, function forLine(line, i, inCode, onFence) {
|
|
|
|
if (((onFence > 0) && (i - 1 >= 0) && lines[i - 1].length) ||
|
|
|
|
((onFence < 0) && (i + 1 < lines.length) && lines[i + 1].length)) {
|
|
|
|
errors.push(i + 1);
|
2015-02-23 23:39:20 -08:00
|
|
|
}
|
2015-02-24 23:50:37 -08:00
|
|
|
});
|
|
|
|
}
|
2015-02-24 23:35:34 -08:00
|
|
|
},
|
|
|
|
|
2015-02-24 23:50:37 -08:00
|
|
|
{
|
|
|
|
"name": "MD032",
|
|
|
|
"desc": "Lists should be surrounded by blank lines",
|
2015-03-16 22:31:18 -07:00
|
|
|
"tags": [ "bullet", "ul", "ol", "blank_lines" ],
|
2015-02-27 22:06:54 -08:00
|
|
|
"func": function MD032(params, errors) {
|
2015-02-24 23:50:37 -08:00
|
|
|
var inList = false;
|
|
|
|
var prevLine = "";
|
2015-03-09 00:31:07 -07:00
|
|
|
forEachLine(params, function forLine(line, lineIndex, inCode, onFence) {
|
|
|
|
if (!inCode || onFence) {
|
2015-03-06 09:21:55 -08:00
|
|
|
var listMarker = /^([\*\+\-]|(\d+\.))\s/.test(line.trim());
|
|
|
|
if (listMarker && !inList && !/^($|\s)/.test(prevLine)) {
|
2015-03-03 09:29:13 -08:00
|
|
|
errors.push(lineIndex + 1);
|
2015-03-06 09:21:55 -08:00
|
|
|
} else if (!listMarker && inList && !/^($|\s)/.test(line)) {
|
2015-03-03 09:29:13 -08:00
|
|
|
errors.push(lineIndex);
|
2015-02-24 23:50:37 -08:00
|
|
|
}
|
|
|
|
inList = listMarker;
|
2015-02-24 23:35:34 -08:00
|
|
|
}
|
2015-02-24 23:50:37 -08:00
|
|
|
prevLine = line;
|
|
|
|
});
|
|
|
|
}
|
2015-04-13 08:47:15 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"name": "MD033",
|
|
|
|
"desc": "Inline HTML",
|
|
|
|
"tags": [ "html" ],
|
|
|
|
"func": function MD033(params, errors) {
|
2015-06-12 09:37:11 -07:00
|
|
|
filterTokens(params, "html_block", function forToken(token) {
|
2015-05-29 18:18:20 -07:00
|
|
|
errors.push(token.lineNumber);
|
|
|
|
});
|
2015-04-16 09:39:04 -07:00
|
|
|
forEachInlineChild(params, "html_inline", function forToken(token) {
|
|
|
|
errors.push(token.lineNumber);
|
|
|
|
});
|
2015-04-13 08:47:15 -07:00
|
|
|
}
|
2015-04-14 00:01:57 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"name": "MD034",
|
|
|
|
"desc": "Bare URL used",
|
|
|
|
"tags": [ "links", "url" ],
|
|
|
|
"func": function MD034(params, errors) {
|
2015-06-12 09:37:11 -07:00
|
|
|
filterTokens(params, "inline", function forToken(token) {
|
2015-05-29 18:18:20 -07:00
|
|
|
var inLink = false;
|
|
|
|
token.children.forEach(function forChild(child) {
|
|
|
|
if (child.type === "link_open") {
|
|
|
|
inLink = true;
|
|
|
|
} else if (child.type === "link_close") {
|
|
|
|
inLink = false;
|
|
|
|
} else if ((child.type === "text") &&
|
|
|
|
!inLink &&
|
|
|
|
/https?:\/\//.test(child.content)) {
|
|
|
|
errors.push(child.lineNumber);
|
|
|
|
}
|
2015-04-14 00:01:57 -07:00
|
|
|
});
|
2015-05-29 18:18:20 -07:00
|
|
|
});
|
2015-04-14 00:01:57 -07:00
|
|
|
}
|
2015-04-14 09:40:16 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"name": "MD035",
|
|
|
|
"desc": "Horizontal rule style",
|
|
|
|
"tags": [ "hr" ],
|
|
|
|
"func": function MD035(params, errors) {
|
|
|
|
var style = params.options.style || "consistent";
|
2015-06-12 09:37:11 -07:00
|
|
|
filterTokens(params, "hr", function forToken(token) {
|
2015-05-29 18:18:20 -07:00
|
|
|
if (style === "consistent") {
|
|
|
|
style = token.line;
|
|
|
|
}
|
2015-04-14 09:40:16 -07:00
|
|
|
if (token.line !== style) {
|
|
|
|
errors.push(token.lineNumber);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-04-14 22:37:56 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"name": "MD036",
|
|
|
|
"desc": "Emphasis used instead of a header",
|
|
|
|
"tags": [ "headers", "emphasis" ],
|
|
|
|
"func": function MD036(params, errors) {
|
|
|
|
function base(token) {
|
|
|
|
if (token.type === "paragraph_open") {
|
|
|
|
return function inParagraph(t) {
|
|
|
|
if ((t.type === "inline") &&
|
|
|
|
(t.children.length === 3) &&
|
|
|
|
((t.children[0].type === "strong_open") ||
|
|
|
|
(t.children[0].type === "em_open")) &&
|
|
|
|
(t.children[1].type === "text")) {
|
2015-04-15 17:50:01 -07:00
|
|
|
errors.push(t.lineNumber);
|
2015-04-14 22:37:56 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} else if (token.type === "blockquote_open") {
|
|
|
|
return function inBlockquote(t) {
|
|
|
|
if (t.type !== "blockquote_close") {
|
|
|
|
return inBlockquote;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var state = base;
|
|
|
|
params.tokens.forEach(function forToken(token) {
|
|
|
|
state = state(token) || base;
|
|
|
|
});
|
|
|
|
}
|
2015-04-15 17:50:01 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"name": "MD037",
|
|
|
|
"desc": "Spaces inside emphasis markers",
|
|
|
|
"tags": [ "whitespace", "emphasis" ],
|
|
|
|
"func": function MD037(params, errors) {
|
2015-04-15 18:24:42 -07:00
|
|
|
forEachInlineChild(params, "text", function forToken(token) {
|
2015-04-15 17:50:01 -07:00
|
|
|
if (/\s(\*\*?|__?)\s.+\1/.test(token.content) ||
|
|
|
|
/(\*\*?|__?).+\s\1\s/.test(token.content)) {
|
|
|
|
errors.push(token.lineNumber);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-04-15 18:24:42 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"name": "MD038",
|
|
|
|
"desc": "Spaces inside code span elements",
|
|
|
|
"tags": [ "whitespace", "code" ],
|
|
|
|
"func": function MD038(params, errors) {
|
|
|
|
forEachInlineChild(params, "code_inline",
|
|
|
|
function forToken(token, inline) {
|
|
|
|
if (inline.content.indexOf("`" + token.content + "`") === -1) {
|
|
|
|
errors.push(token.lineNumber);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"name": "MD039",
|
|
|
|
"desc": "Spaces inside link text",
|
|
|
|
"tags": [ "whitespace", "links" ],
|
|
|
|
"func": function MD039(params, errors) {
|
2015-06-12 09:37:11 -07:00
|
|
|
filterTokens(params, "inline", function forToken(token) {
|
2015-05-29 18:18:20 -07:00
|
|
|
var inLink = false;
|
|
|
|
var index = 0;
|
|
|
|
var lastChildRightSpaceLineNumber = 0;
|
|
|
|
token.children.forEach(function forChild(child) {
|
|
|
|
if (child.type === "link_open") {
|
|
|
|
inLink = true;
|
|
|
|
index = 0;
|
|
|
|
} else if (child.type === "link_close") {
|
|
|
|
inLink = false;
|
|
|
|
if (lastChildRightSpaceLineNumber) {
|
|
|
|
errors.push(lastChildRightSpaceLineNumber);
|
|
|
|
}
|
|
|
|
} else if (inLink) {
|
|
|
|
if ((index === 0) &&
|
|
|
|
(child.content.trimLeft().length !== child.content.length)) {
|
|
|
|
errors.push(child.lineNumber);
|
2015-04-15 18:24:42 -07:00
|
|
|
}
|
2015-05-29 18:18:20 -07:00
|
|
|
lastChildRightSpaceLineNumber =
|
|
|
|
(child.content.trimRight().length !== child.content.length) ?
|
|
|
|
child.lineNumber : 0;
|
|
|
|
index++;
|
|
|
|
}
|
2015-04-15 18:24:42 -07:00
|
|
|
});
|
2015-05-29 18:18:20 -07:00
|
|
|
});
|
2015-04-15 18:24:42 -07:00
|
|
|
}
|
2015-04-16 09:13:56 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"name": "MD040",
|
|
|
|
"desc": "Fenced code blocks should have a language specified",
|
|
|
|
"tags": [ "code", "language" ],
|
|
|
|
"func": function MD040(params, errors) {
|
2015-06-12 09:37:11 -07:00
|
|
|
filterTokens(params, "fence", function forToken(token) {
|
2015-05-29 18:18:20 -07:00
|
|
|
if (!token.info.trim()) {
|
|
|
|
errors.push(token.lineNumber);
|
|
|
|
}
|
|
|
|
});
|
2015-04-16 09:13:56 -07:00
|
|
|
}
|
2015-07-20 22:06:48 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"name": "MD041",
|
|
|
|
"desc": "First line in file should be a top level header",
|
|
|
|
"tags": [ "headers" ],
|
|
|
|
"func": function MD041(params, errors) {
|
|
|
|
var firstHeader = null;
|
|
|
|
params.tokens.every(function forToken(token) {
|
|
|
|
if (token.type === "heading_open") {
|
|
|
|
firstHeader = token;
|
|
|
|
return false;
|
|
|
|
} else if (token.lineNumber > 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
if (!firstHeader ||
|
|
|
|
(firstHeader.lineNumber !== 1) ||
|
|
|
|
(firstHeader.tag !== "h1")) {
|
|
|
|
errors.push(1);
|
|
|
|
}
|
|
|
|
}
|
2015-02-23 23:39:20 -08:00
|
|
|
}
|
2015-02-24 23:50:37 -08:00
|
|
|
];
|