mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +01:00
Move code shared by rules into shared.js.
This commit is contained in:
parent
f79cdcb0d4
commit
513a1351a5
2 changed files with 383 additions and 372 deletions
510
lib/rules.js
510
lib/rules.js
|
|
@ -4,233 +4,6 @@
|
|||
|
||||
var shared = require("./shared");
|
||||
|
||||
// Range regular expressions
|
||||
var atxClosedHeaderNoSpaceRe = /(?:^#+[^#\s])|(?:[^#\s]#+\s*$)/;
|
||||
var atxClosedHeaderSpaceRe = /(?:^#+\s\s+?\S)|(?:\S\s\s+?#+\s*$)/;
|
||||
var atxHeaderSpaceRe = /^#+\s*\S/;
|
||||
var bareUrlRe = /(?:http|ftp)s?:\/\/[^\s]*/i;
|
||||
var dollarCommandRe = /^(\s*)(\$\s)/;
|
||||
var emptyLinkRe = /\[[^\]]*](?:\((?:#?|(?:<>))\))/;
|
||||
var htmlRe = /<[^>]*>/;
|
||||
var listItemMarkerRe = /^[\s>]*(?:[*+-]|\d+\.)\s+/;
|
||||
var listItemMarkerInterruptsRe = /^[\s>]*(?:[*+-]|1\.)\s+/;
|
||||
var reversedLinkRe = /\([^)]+\)\[[^\]^][^\]]*]/;
|
||||
var spaceAfterBlockQuote = /^\s*(?:>\s+)+\S/;
|
||||
var spaceBeforeHeaderRe = /^\s+\S/;
|
||||
var spaceInsideLinkRe = /\[(?:\s+(?:[^\]]*?)\s*|(?:[^\]]*?)\s+)](?=\(\S*\))/;
|
||||
var tabRe = /\t+/;
|
||||
var trailingPunctuationRe = /.$/;
|
||||
var trailingSpaceRe = /\s+$/;
|
||||
var defaultLineLength = 80;
|
||||
|
||||
// Escapes a string for use in a RegExp
|
||||
function escapeForRegExp(str) {
|
||||
return str.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
|
||||
}
|
||||
|
||||
// Returns the indent for a token
|
||||
function indentFor(token) {
|
||||
var line = token.line.replace(/^[\s>]*(> |>)/, "");
|
||||
return line.length - shared.trimLeft(line).length;
|
||||
}
|
||||
|
||||
// Returns the heading style for a heading token
|
||||
function headingStyleFor(token) {
|
||||
if ((token.map[1] - token.map[0]) === 1) {
|
||||
if (/#\s*$/.test(token.line)) {
|
||||
return "atx_closed";
|
||||
}
|
||||
return "atx";
|
||||
}
|
||||
return "setext";
|
||||
}
|
||||
|
||||
// Returns the unordered list style for a list item token
|
||||
function unorderedListStyleFor(token) {
|
||||
switch (shared.trimLeft(token.line).substr(0, 1)) {
|
||||
case "-":
|
||||
return "dash";
|
||||
case "+":
|
||||
return "plus";
|
||||
// case "*":
|
||||
default:
|
||||
return "asterisk";
|
||||
}
|
||||
}
|
||||
|
||||
// Calls the provided function for each matching token
|
||||
function filterTokens(params, type, callback) {
|
||||
(params.tokenLists[type] || []).forEach(callback);
|
||||
}
|
||||
|
||||
// Calls the provided function for each line (with context)
|
||||
function forEachLine(params, callback) {
|
||||
if (!params.forEachLine) {
|
||||
var lineMetadata = new Array(params.lines.length);
|
||||
var fenceStart = null;
|
||||
var inFence = false;
|
||||
// Find fenced code by pattern (parser ignores "``` close fence")
|
||||
params.lines.forEach(function forLine(line, lineIndex) {
|
||||
var metadata = 0;
|
||||
var match = /^(`{3,}|~{3,})/.exec(line);
|
||||
var fence = match && match[1];
|
||||
if (fence &&
|
||||
(!inFence || (fence.substr(0, fenceStart.length) === fenceStart))) {
|
||||
metadata = inFence ? 2 : 6;
|
||||
fenceStart = inFence ? null : fence;
|
||||
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;
|
||||
}
|
||||
});
|
||||
// Find tables normally
|
||||
filterTokens(params, "table_open", function forToken(token) {
|
||||
for (var i = token.map[0]; i < token.map[1]; i++) {
|
||||
lineMetadata[i] += 8;
|
||||
}
|
||||
});
|
||||
params.forEachLine = lineMetadata;
|
||||
}
|
||||
// Invoke callback
|
||||
params.lines.forEach(function forLine(line, lineIndex) {
|
||||
var metadata = params.forEachLine[lineIndex];
|
||||
callback(
|
||||
line,
|
||||
lineIndex,
|
||||
!!(metadata & 7),
|
||||
(((metadata & 6) >> 1) || 2) - 2,
|
||||
!!(metadata & 8));
|
||||
});
|
||||
}
|
||||
|
||||
// Calls the provided function for each specified inline child token
|
||||
function forEachInlineChild(params, type, callback) {
|
||||
filterTokens(params, "inline", function forToken(token) {
|
||||
token.children.forEach(function forChild(child) {
|
||||
if (child.type === type) {
|
||||
callback(child);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Calls the provided function for each heading's content
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Returns (nested) lists as a flat array (in order)
|
||||
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 = {
|
||||
"unordered": (token.type === "bullet_list_open"),
|
||||
"parentsUnordered": !current ||
|
||||
(current.unordered && current.parentsUnordered),
|
||||
"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];
|
||||
lists.splice(current.insert, 0, current);
|
||||
delete current.insert;
|
||||
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;
|
||||
}
|
||||
});
|
||||
params.flattenLists = lists;
|
||||
}
|
||||
return params.flattenLists;
|
||||
}
|
||||
|
||||
// Adds a generic error object via the onError callback
|
||||
function addError(onError, lineNumber, detail, context, range) {
|
||||
onError({
|
||||
"lineNumber": lineNumber,
|
||||
"detail": detail,
|
||||
"context": context,
|
||||
"range": range
|
||||
});
|
||||
}
|
||||
|
||||
// Adds an error object with details conditionally via the onError callback
|
||||
function addErrorDetailIf(
|
||||
onError, lineNumber, expected, actual, detail, range) {
|
||||
if (expected !== actual) {
|
||||
addError(
|
||||
onError,
|
||||
lineNumber,
|
||||
"Expected: " + expected + "; Actual: " + actual +
|
||||
(detail ? "; " + detail : ""),
|
||||
null,
|
||||
range);
|
||||
}
|
||||
}
|
||||
|
||||
// Adds an error object with context via the onError callback
|
||||
function addErrorContext(onError, lineNumber, context, left, right, range) {
|
||||
if (context.length <= 30) {
|
||||
// Nothing to do
|
||||
} else if (left && right) {
|
||||
context = context.substr(0, 15) + "..." + context.substr(-15);
|
||||
} else if (right) {
|
||||
context = "..." + context.substr(-30);
|
||||
} else {
|
||||
context = context.substr(0, 30) + "...";
|
||||
}
|
||||
addError(onError, lineNumber, null, context, range);
|
||||
}
|
||||
|
||||
// Returns a range object for a line by applying a RegExp
|
||||
function rangeFromRegExp(line, regexp) {
|
||||
var range = null;
|
||||
var match = line.match(regexp);
|
||||
if (match) {
|
||||
var column = match.index + 1;
|
||||
var length = match[0].length;
|
||||
if (match[2]) {
|
||||
column += match[1].length;
|
||||
length -= match[1].length;
|
||||
}
|
||||
range = [ column, length ];
|
||||
}
|
||||
return range;
|
||||
}
|
||||
|
||||
module.exports = [
|
||||
{
|
||||
"names": [ "MD001", "header-increment" ],
|
||||
|
|
@ -238,10 +11,10 @@ module.exports = [
|
|||
"tags": [ "headers" ],
|
||||
"func": function MD001(params, onError) {
|
||||
var prevLevel = 0;
|
||||
filterTokens(params, "heading_open", function forToken(token) {
|
||||
shared.filterTokens(params, "heading_open", function forToken(token) {
|
||||
var level = parseInt(token.tag.slice(1), 10);
|
||||
if (prevLevel && (level > prevLevel)) {
|
||||
addErrorDetailIf(onError, token.lineNumber,
|
||||
shared.addErrorDetailIf(onError, token.lineNumber,
|
||||
"h" + (prevLevel + 1), "h" + level);
|
||||
}
|
||||
prevLevel = level;
|
||||
|
|
@ -258,7 +31,7 @@ module.exports = [
|
|||
var tag = "h" + level;
|
||||
params.tokens.every(function forToken(token) {
|
||||
if (token.type === "heading_open") {
|
||||
addErrorDetailIf(onError, token.lineNumber, tag, token.tag);
|
||||
shared.addErrorDetailIf(onError, token.lineNumber, tag, token.tag);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
|
@ -272,8 +45,8 @@ module.exports = [
|
|||
"tags": [ "headers" ],
|
||||
"func": function MD003(params, onError) {
|
||||
var style = params.config.style || "consistent";
|
||||
filterTokens(params, "heading_open", function forToken(token) {
|
||||
var styleForToken = headingStyleFor(token);
|
||||
shared.filterTokens(params, "heading_open", function forToken(token) {
|
||||
var styleForToken = shared.headingStyleFor(token);
|
||||
if (style === "consistent") {
|
||||
style = styleForToken;
|
||||
}
|
||||
|
|
@ -294,7 +67,7 @@ module.exports = [
|
|||
} else if (style === "setext_with_atx_closed") {
|
||||
expected = h12 ? "setext" : "atx_closed";
|
||||
}
|
||||
addErrorDetailIf(onError, token.lineNumber,
|
||||
shared.addErrorDetailIf(onError, token.lineNumber,
|
||||
expected, styleForToken);
|
||||
}
|
||||
}
|
||||
|
|
@ -310,27 +83,27 @@ module.exports = [
|
|||
var style = params.config.style || "consistent";
|
||||
var expectedStyle = style;
|
||||
var nestingStyles = [];
|
||||
flattenLists(params).forEach(function forList(list) {
|
||||
shared.flattenLists(params).forEach(function forList(list) {
|
||||
if (list.unordered) {
|
||||
if (expectedStyle === "consistent") {
|
||||
expectedStyle = unorderedListStyleFor(list.items[0]);
|
||||
expectedStyle = shared.unorderedListStyleFor(list.items[0]);
|
||||
}
|
||||
list.items.forEach(function forItem(item) {
|
||||
var itemStyle = unorderedListStyleFor(item);
|
||||
var itemStyle = shared.unorderedListStyleFor(item);
|
||||
if (style === "sublist") {
|
||||
var nesting = list.nesting;
|
||||
if (!nestingStyles[nesting] &&
|
||||
(itemStyle !== nestingStyles[nesting - 1])) {
|
||||
nestingStyles[nesting] = itemStyle;
|
||||
} else {
|
||||
addErrorDetailIf(onError, item.lineNumber,
|
||||
shared.addErrorDetailIf(onError, item.lineNumber,
|
||||
nestingStyles[nesting], itemStyle, null,
|
||||
rangeFromRegExp(item.line, listItemMarkerRe));
|
||||
shared.rangeFromRegExp(item.line, shared.listItemMarkerRe));
|
||||
}
|
||||
} else {
|
||||
addErrorDetailIf(onError, item.lineNumber,
|
||||
shared.addErrorDetailIf(onError, item.lineNumber,
|
||||
expectedStyle, itemStyle, null,
|
||||
rangeFromRegExp(item.line, listItemMarkerRe));
|
||||
shared.rangeFromRegExp(item.line, shared.listItemMarkerRe));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -343,11 +116,12 @@ module.exports = [
|
|||
"desc": "Inconsistent indentation for list items at the same level",
|
||||
"tags": [ "bullet", "ul", "indentation" ],
|
||||
"func": function MD005(params, onError) {
|
||||
flattenLists(params).forEach(function forList(list) {
|
||||
var indent = indentFor(list.items[0]);
|
||||
shared.flattenLists(params).forEach(function forList(list) {
|
||||
var indent = shared.indentFor(list.items[0]);
|
||||
list.items.forEach(function forItem(item) {
|
||||
addErrorDetailIf(onError, item.lineNumber, indent, indentFor(item),
|
||||
null, rangeFromRegExp(item.line, listItemMarkerRe));
|
||||
shared.addErrorDetailIf(onError, item.lineNumber, indent,
|
||||
shared.indentFor(item), null,
|
||||
shared.rangeFromRegExp(item.line, shared.listItemMarkerRe));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
@ -358,11 +132,11 @@ module.exports = [
|
|||
"desc": "Consider starting bulleted lists at the beginning of the line",
|
||||
"tags": [ "bullet", "ul", "indentation" ],
|
||||
"func": function MD006(params, onError) {
|
||||
flattenLists(params).forEach(function forList(list) {
|
||||
shared.flattenLists(params).forEach(function forList(list) {
|
||||
if (list.unordered && !list.nesting) {
|
||||
addErrorDetailIf(onError, list.open.lineNumber,
|
||||
0, indentFor(list.open), null,
|
||||
rangeFromRegExp(list.open.line, listItemMarkerRe));
|
||||
shared.addErrorDetailIf(onError, list.open.lineNumber,
|
||||
0, shared.indentFor(list.open), null,
|
||||
shared.rangeFromRegExp(list.open.line, shared.listItemMarkerRe));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -375,13 +149,13 @@ module.exports = [
|
|||
"func": function MD007(params, onError) {
|
||||
var optionsIndent = params.config.indent || 2;
|
||||
var prevIndent = 0;
|
||||
flattenLists(params).forEach(function forList(list) {
|
||||
shared.flattenLists(params).forEach(function forList(list) {
|
||||
if (list.unordered && list.parentsUnordered) {
|
||||
var indent = indentFor(list.open);
|
||||
var indent = shared.indentFor(list.open);
|
||||
if (indent > prevIndent) {
|
||||
addErrorDetailIf(onError, list.open.lineNumber,
|
||||
shared.addErrorDetailIf(onError, list.open.lineNumber,
|
||||
prevIndent + optionsIndent, indent, null,
|
||||
rangeFromRegExp(list.open.line, listItemMarkerRe));
|
||||
shared.rangeFromRegExp(list.open.line, shared.listItemMarkerRe));
|
||||
}
|
||||
prevIndent = indent;
|
||||
}
|
||||
|
|
@ -396,24 +170,25 @@ module.exports = [
|
|||
"func": function MD009(params, onError) {
|
||||
var brSpaces = params.config.br_spaces || 0;
|
||||
var listItemEmptyLines = params.config.list_item_empty_lines;
|
||||
var trailingSpaceRe = /\s+$/;
|
||||
var allowListItemEmptyLines =
|
||||
(listItemEmptyLines === undefined) ? false : !!listItemEmptyLines;
|
||||
var listItemLineNumbers = [];
|
||||
if (allowListItemEmptyLines) {
|
||||
filterTokens(params, "list_item_open", function forToken(token) {
|
||||
shared.filterTokens(params, "list_item_open", function forToken(token) {
|
||||
for (var i = token.map[0]; i < token.map[1]; i++) {
|
||||
listItemLineNumbers.push(i + 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
forEachLine(params, function forLine(line, lineIndex) {
|
||||
shared.forEachLine(params, function forLine(line, lineIndex) {
|
||||
var lineNumber = lineIndex + 1;
|
||||
if (trailingSpaceRe.test(line) &&
|
||||
(listItemLineNumbers.indexOf(lineNumber) === -1)) {
|
||||
var expected = (brSpaces < 2) ? 0 : brSpaces;
|
||||
addErrorDetailIf(onError, lineNumber,
|
||||
shared.addErrorDetailIf(onError, lineNumber,
|
||||
expected, line.length - shared.trimRight(line).length, null,
|
||||
rangeFromRegExp(line, trailingSpaceRe));
|
||||
shared.rangeFromRegExp(line, trailingSpaceRe));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -426,11 +201,12 @@ module.exports = [
|
|||
"func": function MD010(params, onError) {
|
||||
var codeBlocks = params.config.code_blocks;
|
||||
var includeCodeBlocks = (codeBlocks === undefined) ? true : !!codeBlocks;
|
||||
forEachLine(params, function forLine(line, lineIndex, inCode) {
|
||||
var tabRe = /\t+/;
|
||||
shared.forEachLine(params, function forLine(line, lineIndex, inCode) {
|
||||
if (tabRe.test(line) && (!inCode || includeCodeBlocks)) {
|
||||
addError(onError, lineIndex + 1,
|
||||
shared.addError(onError, lineIndex + 1,
|
||||
"Column: " + (line.indexOf("\t") + 1), null,
|
||||
rangeFromRegExp(line, tabRe));
|
||||
shared.rangeFromRegExp(line, tabRe));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -441,11 +217,12 @@ module.exports = [
|
|||
"desc": "Reversed link syntax",
|
||||
"tags": [ "links" ],
|
||||
"func": function MD011(params, onError) {
|
||||
forEachInlineChild(params, "text", function forToken(token) {
|
||||
var reversedLinkRe = /\([^)]+\)\[[^\]^][^\]]*]/;
|
||||
shared.forEachInlineChild(params, "text", function forToken(token) {
|
||||
var match = reversedLinkRe.exec(token.content);
|
||||
if (match) {
|
||||
addError(onError, token.lineNumber, match[0], null,
|
||||
rangeFromRegExp(token.line, reversedLinkRe));
|
||||
shared.addError(onError, token.lineNumber, match[0], null,
|
||||
shared.rangeFromRegExp(token.line, reversedLinkRe));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -458,10 +235,10 @@ module.exports = [
|
|||
"func": function MD012(params, onError) {
|
||||
var maximum = params.config.maximum || 1;
|
||||
var count = 0;
|
||||
forEachLine(params, function forLine(line, lineIndex, inCode) {
|
||||
shared.forEachLine(params, function forLine(line, lineIndex, inCode) {
|
||||
count = (inCode || line.trim().length) ? 0 : count + 1;
|
||||
if (maximum < count) {
|
||||
addErrorDetailIf(onError, lineIndex + 1, maximum, count);
|
||||
shared.addErrorDetailIf(onError, lineIndex + 1, maximum, count);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -472,7 +249,7 @@ module.exports = [
|
|||
"desc": "Line length",
|
||||
"tags": [ "line_length" ],
|
||||
"func": function MD013(params, onError) {
|
||||
var lineLength = params.config.line_length || defaultLineLength;
|
||||
var lineLength = params.config.line_length || 80;
|
||||
var codeBlocks = params.config.code_blocks;
|
||||
var includeCodeBlocks = (codeBlocks === undefined) ? true : !!codeBlocks;
|
||||
var tables = params.config.tables;
|
||||
|
|
@ -481,7 +258,7 @@ module.exports = [
|
|||
var includeHeaders = (headers === undefined) ? true : !!headers;
|
||||
var headerLineNumbers = [];
|
||||
if (!includeHeaders) {
|
||||
forEachHeading(params, function forHeading(heading) {
|
||||
shared.forEachHeading(params, function forHeading(heading) {
|
||||
headerLineNumbers.push(heading.lineNumber);
|
||||
});
|
||||
}
|
||||
|
|
@ -495,7 +272,7 @@ module.exports = [
|
|||
"text": "T"
|
||||
};
|
||||
var linkOnlyLineNumbers = [];
|
||||
filterTokens(params, "inline", function forToken(token) {
|
||||
shared.filterTokens(params, "inline", function forToken(token) {
|
||||
var childTokenTypes = "";
|
||||
token.children.forEach(function forChild(child) {
|
||||
if (child.type !== "text" || child.content !== "") {
|
||||
|
|
@ -508,7 +285,7 @@ module.exports = [
|
|||
});
|
||||
var longLineRe = new RegExp("^(.{" + lineLength + "})(.*\\s.*)$");
|
||||
var labelRe = /^\s*\[.*[^\\]]:/;
|
||||
forEachLine(params,
|
||||
shared.forEachLine(params,
|
||||
function forLine(line, lineIndex, inCode, onFence, inTable) {
|
||||
var lineNumber = lineIndex + 1;
|
||||
if ((includeCodeBlocks || !inCode) &&
|
||||
|
|
@ -517,8 +294,8 @@ module.exports = [
|
|||
(linkOnlyLineNumbers.indexOf(lineNumber) < 0) &&
|
||||
longLineRe.test(line) &&
|
||||
!labelRe.test(line)) {
|
||||
addErrorDetailIf(onError, lineNumber, lineLength, line.length, null,
|
||||
rangeFromRegExp(line, longLineRe));
|
||||
shared.addErrorDetailIf(onError, lineNumber, lineLength,
|
||||
line.length, null, shared.rangeFromRegExp(line, longLineRe));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -529,16 +306,17 @@ module.exports = [
|
|||
"desc": "Dollar signs used before commands without showing output",
|
||||
"tags": [ "code" ],
|
||||
"func": function MD014(params, onError) {
|
||||
var dollarCommandRe = /^(\s*)(\$\s)/;
|
||||
[ "code_block", "fence" ].forEach(function forType(type) {
|
||||
filterTokens(params, type, function forToken(token) {
|
||||
shared.filterTokens(params, type, function forToken(token) {
|
||||
var allBlank = true;
|
||||
if (token.content && token.content.split(shared.newLineRe)
|
||||
.every(function forLine(line) {
|
||||
return !line || (allBlank = false) || dollarCommandRe.test(line);
|
||||
}) && !allBlank) {
|
||||
addErrorContext(onError, token.lineNumber,
|
||||
shared.addErrorContext(onError, token.lineNumber,
|
||||
token.content.split(shared.newLineRe)[0].trim(), null, null,
|
||||
rangeFromRegExp(token.line, dollarCommandRe));
|
||||
shared.rangeFromRegExp(token.line, dollarCommandRe));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
@ -550,10 +328,10 @@ module.exports = [
|
|||
"desc": "No space after hash on atx style header",
|
||||
"tags": [ "headers", "atx", "spaces" ],
|
||||
"func": function MD018(params, onError) {
|
||||
forEachLine(params, function forLine(line, lineIndex, inCode) {
|
||||
shared.forEachLine(params, function forLine(line, lineIndex, inCode) {
|
||||
if (!inCode && /^#+[^#\s]/.test(line) && !/#$/.test(line)) {
|
||||
addErrorContext(onError, lineIndex + 1, line.trim(), null, null,
|
||||
rangeFromRegExp(line, atxHeaderSpaceRe));
|
||||
shared.addErrorContext(onError, lineIndex + 1, line.trim(), null,
|
||||
null, shared.rangeFromRegExp(line, shared.atxHeaderSpaceRe));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -564,11 +342,12 @@ module.exports = [
|
|||
"desc": "Multiple spaces after hash on atx style header",
|
||||
"tags": [ "headers", "atx", "spaces" ],
|
||||
"func": function MD019(params, onError) {
|
||||
filterTokens(params, "heading_open", function forToken(token) {
|
||||
if ((headingStyleFor(token) === "atx") &&
|
||||
shared.filterTokens(params, "heading_open", function forToken(token) {
|
||||
if ((shared.headingStyleFor(token) === "atx") &&
|
||||
/^#+\s\s/.test(token.line)) {
|
||||
addErrorContext(onError, token.lineNumber, token.line.trim(),
|
||||
null, null, rangeFromRegExp(token.line, atxHeaderSpaceRe));
|
||||
shared.addErrorContext(onError, token.lineNumber, token.line.trim(),
|
||||
null, null,
|
||||
shared.rangeFromRegExp(token.line, shared.atxHeaderSpaceRe));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -579,13 +358,14 @@ module.exports = [
|
|||
"desc": "No space inside hashes on closed atx style header",
|
||||
"tags": [ "headers", "atx_closed", "spaces" ],
|
||||
"func": function MD020(params, onError) {
|
||||
forEachLine(params, function forLine(line, lineIndex, inCode) {
|
||||
var atxClosedHeaderNoSpaceRe = /(?:^#+[^#\s])|(?:[^#\s]#+\s*$)/;
|
||||
shared.forEachLine(params, function forLine(line, lineIndex, inCode) {
|
||||
if (!inCode && /^#+[^#]*[^\\]#+$/.test(line)) {
|
||||
var left = /^#+[^#\s]/.test(line);
|
||||
var right = /[^#\s]#+$/.test(line);
|
||||
if (left || right) {
|
||||
addErrorContext(onError, lineIndex + 1, line.trim(), left, right,
|
||||
rangeFromRegExp(line, atxClosedHeaderNoSpaceRe));
|
||||
shared.addErrorContext(onError, lineIndex + 1, line.trim(), left,
|
||||
right, shared.rangeFromRegExp(line, atxClosedHeaderNoSpaceRe));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
@ -597,13 +377,15 @@ module.exports = [
|
|||
"desc": "Multiple spaces inside hashes on closed atx style header",
|
||||
"tags": [ "headers", "atx_closed", "spaces" ],
|
||||
"func": function MD021(params, onError) {
|
||||
filterTokens(params, "heading_open", function forToken(token) {
|
||||
if (headingStyleFor(token) === "atx_closed") {
|
||||
var atxClosedHeaderSpaceRe = /(?:^#+\s\s+?\S)|(?:\S\s\s+?#+\s*$)/;
|
||||
shared.filterTokens(params, "heading_open", function forToken(token) {
|
||||
if (shared.headingStyleFor(token) === "atx_closed") {
|
||||
var left = /^#+\s\s/.test(token.line);
|
||||
var right = /\s\s#+$/.test(token.line);
|
||||
if (left || right) {
|
||||
addErrorContext(onError, token.lineNumber, token.line.trim(),
|
||||
left, right, rangeFromRegExp(token.line, atxClosedHeaderSpaceRe));
|
||||
shared.addErrorContext(onError, token.lineNumber, token.line.trim(),
|
||||
left, right,
|
||||
shared.rangeFromRegExp(token.line, atxClosedHeaderSpaceRe));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
@ -621,7 +403,8 @@ module.exports = [
|
|||
params.tokens.forEach(function forToken(token) {
|
||||
if (token.type === "heading_open") {
|
||||
if ((token.map[0] - prevMaxLineIndex) === 0) {
|
||||
addErrorContext(onError, token.lineNumber, token.line.trim());
|
||||
shared.addErrorContext(onError, token.lineNumber,
|
||||
token.line.trim());
|
||||
}
|
||||
} else if (token.type === "heading_close") {
|
||||
needBlankLine = true;
|
||||
|
|
@ -629,7 +412,7 @@ module.exports = [
|
|||
if (token.map) {
|
||||
if (needBlankLine) {
|
||||
if ((token.map[0] - prevMaxLineIndex) === 0) {
|
||||
addErrorContext(onError, prevHeadingLineNumber,
|
||||
shared.addErrorContext(onError, prevHeadingLineNumber,
|
||||
params.lines[prevHeadingLineNumber - 1].trim());
|
||||
}
|
||||
needBlankLine = false;
|
||||
|
|
@ -648,10 +431,11 @@ module.exports = [
|
|||
"desc": "Headers must start at the beginning of the line",
|
||||
"tags": [ "headers", "spaces" ],
|
||||
"func": function MD023(params, onError) {
|
||||
filterTokens(params, "heading_open", function forToken(token) {
|
||||
var spaceBeforeHeaderRe = /^\s+\S/;
|
||||
shared.filterTokens(params, "heading_open", function forToken(token) {
|
||||
if (spaceBeforeHeaderRe.test(token.line)) {
|
||||
addErrorContext(onError, token.lineNumber, token.line, null, null,
|
||||
rangeFromRegExp(token.line, spaceBeforeHeaderRe));
|
||||
shared.addErrorContext(onError, token.lineNumber, token.line, null,
|
||||
null, shared.rangeFromRegExp(token.line, spaceBeforeHeaderRe));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -663,11 +447,12 @@ module.exports = [
|
|||
"tags": [ "headers" ],
|
||||
"func": function MD024(params, onError) {
|
||||
var knownContent = [];
|
||||
forEachHeading(params, function forHeading(heading, content) {
|
||||
shared.forEachHeading(params, function forHeading(heading, content) {
|
||||
if (knownContent.indexOf(content) === -1) {
|
||||
knownContent.push(content);
|
||||
} else {
|
||||
addErrorContext(onError, heading.lineNumber, heading.line.trim());
|
||||
shared.addErrorContext(onError, heading.lineNumber,
|
||||
heading.line.trim());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -681,10 +466,11 @@ module.exports = [
|
|||
var level = params.config.level || 1;
|
||||
var tag = "h" + level;
|
||||
var hasTopLevelHeading = false;
|
||||
filterTokens(params, "heading_open", function forToken(token) {
|
||||
shared.filterTokens(params, "heading_open", function forToken(token) {
|
||||
if (token.tag === tag) {
|
||||
if (hasTopLevelHeading) {
|
||||
addErrorContext(onError, token.lineNumber, token.line.trim());
|
||||
shared.addErrorContext(onError, token.lineNumber,
|
||||
token.line.trim());
|
||||
} else if (token.lineNumber === 1) {
|
||||
hasTopLevelHeading = true;
|
||||
}
|
||||
|
|
@ -699,13 +485,13 @@ module.exports = [
|
|||
"tags": [ "headers" ],
|
||||
"func": function MD026(params, onError) {
|
||||
var punctuation = params.config.punctuation || ".,;:!?";
|
||||
var re = new RegExp("[" + punctuation + "]$");
|
||||
forEachHeading(params, function forHeading(heading, content) {
|
||||
var match = re.exec(content);
|
||||
var trailingPunctuationRe = new RegExp("[" + punctuation + "]$");
|
||||
shared.forEachHeading(params, function forHeading(heading, content) {
|
||||
var match = trailingPunctuationRe.exec(content);
|
||||
if (match) {
|
||||
addError(onError, heading.lineNumber,
|
||||
shared.addError(onError, heading.lineNumber,
|
||||
"Punctuation: '" + match[0] + "'", null,
|
||||
rangeFromRegExp(heading.line, trailingPunctuationRe));
|
||||
shared.rangeFromRegExp(heading.line, trailingPunctuationRe));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -716,6 +502,7 @@ module.exports = [
|
|||
"desc": "Multiple spaces after blockquote symbol",
|
||||
"tags": [ "blockquote", "whitespace", "indentation" ],
|
||||
"func": function MD027(params, onError) {
|
||||
var spaceAfterBlockQuote = /^\s*(?:>\s+)+\S/;
|
||||
var blockquoteNesting = 0;
|
||||
var listItemNesting = 0;
|
||||
params.tokens.forEach(function forToken(token) {
|
||||
|
|
@ -732,15 +519,15 @@ module.exports = [
|
|||
/^(\s*>)+\s\s+>/.test(token.line) :
|
||||
/^(\s*>)+\s\s/.test(token.line);
|
||||
if (multipleSpaces) {
|
||||
addErrorContext(onError, token.lineNumber, token.line, null, null,
|
||||
rangeFromRegExp(token.line, spaceAfterBlockQuote));
|
||||
shared.addErrorContext(onError, token.lineNumber, token.line, null,
|
||||
null, shared.rangeFromRegExp(token.line, spaceAfterBlockQuote));
|
||||
}
|
||||
token.content.split(shared.newLineRe)
|
||||
.forEach(function forLine(line, offset) {
|
||||
if (/^\s/.test(line)) {
|
||||
addErrorContext(onError, token.lineNumber + offset,
|
||||
shared.addErrorContext(onError, token.lineNumber + offset,
|
||||
"> " + line, null, null,
|
||||
rangeFromRegExp(line, spaceAfterBlockQuote));
|
||||
shared.rangeFromRegExp(line, spaceAfterBlockQuote));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -757,7 +544,7 @@ module.exports = [
|
|||
params.tokens.forEach(function forToken(token) {
|
||||
if ((token.type === "blockquote_open") &&
|
||||
(prevToken.type === "blockquote_close")) {
|
||||
addError(onError, token.lineNumber - 1);
|
||||
shared.addError(onError, token.lineNumber - 1);
|
||||
}
|
||||
prevToken = token;
|
||||
});
|
||||
|
|
@ -771,7 +558,7 @@ module.exports = [
|
|||
"func": function MD029(params, onError) {
|
||||
var style = params.config.style || "one_or_ordered";
|
||||
var numberRe = /^[\s>]*([^.)]*)[.)]/;
|
||||
flattenLists(params).forEach(function forList(list) {
|
||||
shared.flattenLists(params).forEach(function forList(list) {
|
||||
if (!list.unordered) {
|
||||
var listStyle = style;
|
||||
if (listStyle === "one_or_ordered") {
|
||||
|
|
@ -782,10 +569,10 @@ module.exports = [
|
|||
var number = 1;
|
||||
list.items.forEach(function forItem(item) {
|
||||
var match = numberRe.exec(item.line);
|
||||
addErrorDetailIf(onError, item.lineNumber,
|
||||
shared.addErrorDetailIf(onError, item.lineNumber,
|
||||
String(number), !match || match[1],
|
||||
"Style: " + (listStyle === "one" ? "1/1/1" : "1/2/3"),
|
||||
rangeFromRegExp(item.line, listItemMarkerRe));
|
||||
shared.rangeFromRegExp(item.line, shared.listItemMarkerRe));
|
||||
if (listStyle === "ordered") {
|
||||
number++;
|
||||
}
|
||||
|
|
@ -804,7 +591,7 @@ module.exports = [
|
|||
var olSingle = params.config.ol_single || 1;
|
||||
var ulMulti = params.config.ul_multi || 1;
|
||||
var olMulti = params.config.ol_multi || 1;
|
||||
flattenLists(params).forEach(function forList(list) {
|
||||
shared.flattenLists(params).forEach(function forList(list) {
|
||||
var lineCount = list.lastLineIndex - list.open.map[0];
|
||||
var allSingle = lineCount === list.items.length;
|
||||
var expectedSpaces = list.unordered ?
|
||||
|
|
@ -812,9 +599,9 @@ module.exports = [
|
|||
(allSingle ? olSingle : olMulti);
|
||||
list.items.forEach(function forItem(item) {
|
||||
var match = /^[\s>]*\S+(\s+)/.exec(item.line);
|
||||
addErrorDetailIf(onError, item.lineNumber,
|
||||
shared.addErrorDetailIf(onError, item.lineNumber,
|
||||
expectedSpaces, (match ? match[1].length : 0), null,
|
||||
rangeFromRegExp(item.line, listItemMarkerRe));
|
||||
shared.rangeFromRegExp(item.line, shared.listItemMarkerRe));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
@ -826,10 +613,10 @@ module.exports = [
|
|||
"tags": [ "code", "blank_lines" ],
|
||||
"func": function MD031(params, onError) {
|
||||
var lines = params.lines;
|
||||
forEachLine(params, function forLine(line, i, inCode, onFence) {
|
||||
shared.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)) {
|
||||
addErrorContext(onError, i + 1, lines[i].trim());
|
||||
shared.addErrorContext(onError, i + 1, lines[i].trim());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -840,27 +627,30 @@ module.exports = [
|
|||
"desc": "Lists should be surrounded by blank lines",
|
||||
"tags": [ "bullet", "ul", "ol", "blank_lines" ],
|
||||
"func": function MD032(params, onError) {
|
||||
var listItemMarkerInterruptsRe = /^[\s>]*(?:[*+-]|1\.)\s+/;
|
||||
var blankOrListRe = /^[\s>]*($|\s)/;
|
||||
var inList = false;
|
||||
var prevLine = "";
|
||||
forEachLine(params, function forLine(line, lineIndex, inCode, onFence) {
|
||||
shared.forEachLine(params,
|
||||
function forLine(line, lineIndex, inCode, onFence) {
|
||||
if (!inCode || onFence) {
|
||||
var lineTrim = line.trim();
|
||||
var listMarker = listItemMarkerRe.test(lineTrim);
|
||||
var listMarker = shared.listItemMarkerRe.test(lineTrim);
|
||||
if (listMarker && !inList && !blankOrListRe.test(prevLine)) {
|
||||
// Check whether this list prefix can interrupt a paragraph
|
||||
if (listItemMarkerInterruptsRe.test(lineTrim)) {
|
||||
addErrorContext(onError, lineIndex + 1, lineTrim);
|
||||
shared.addErrorContext(onError, lineIndex + 1, lineTrim);
|
||||
} else {
|
||||
listMarker = false;
|
||||
}
|
||||
} else if (!listMarker && inList && !blankOrListRe.test(line)) {
|
||||
addErrorContext(onError, lineIndex, lineTrim);
|
||||
shared.addErrorContext(onError, lineIndex, lineTrim);
|
||||
}
|
||||
inList = listMarker;
|
||||
}
|
||||
prevLine = line;
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
|
|
@ -873,6 +663,7 @@ module.exports = [
|
|||
.map(function forElement(element) {
|
||||
return element.toLowerCase();
|
||||
});
|
||||
var htmlRe = /<[^>]*>/;
|
||||
function forToken(token) {
|
||||
token.content.split(shared.newLineRe)
|
||||
.forEach(function forLine(line, offset) {
|
||||
|
|
@ -887,14 +678,14 @@ module.exports = [
|
|||
return allowedElements.indexOf(element) === -1;
|
||||
});
|
||||
if (allowed.length) {
|
||||
addError(onError, token.lineNumber + offset,
|
||||
shared.addError(onError, token.lineNumber + offset,
|
||||
"Element: " + allowed[0], null,
|
||||
rangeFromRegExp(token.line, htmlRe));
|
||||
shared.rangeFromRegExp(token.line, htmlRe));
|
||||
}
|
||||
});
|
||||
}
|
||||
filterTokens(params, "html_block", forToken);
|
||||
forEachInlineChild(params, "html_inline", forToken);
|
||||
shared.filterTokens(params, "html_block", forToken);
|
||||
shared.forEachInlineChild(params, "html_inline", forToken);
|
||||
}
|
||||
},
|
||||
|
||||
|
|
@ -903,7 +694,7 @@ module.exports = [
|
|||
"desc": "Bare URL used",
|
||||
"tags": [ "links", "url" ],
|
||||
"func": function MD034(params, onError) {
|
||||
filterTokens(params, "inline", function forToken(token) {
|
||||
shared.filterTokens(params, "inline", function forToken(token) {
|
||||
var inLink = false;
|
||||
token.children.forEach(function forChild(child) {
|
||||
var match = null;
|
||||
|
|
@ -913,9 +704,9 @@ module.exports = [
|
|||
inLink = false;
|
||||
} else if ((child.type === "text") &&
|
||||
!inLink &&
|
||||
(match = bareUrlRe.exec(child.content))) {
|
||||
addErrorContext(onError, child.lineNumber, match[0], null, null,
|
||||
rangeFromRegExp(child.line, bareUrlRe));
|
||||
(match = shared.bareUrlRe.exec(child.content))) {
|
||||
shared.addErrorContext(onError, child.lineNumber, match[0], null,
|
||||
null, shared.rangeFromRegExp(child.line, shared.bareUrlRe));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
@ -928,12 +719,12 @@ module.exports = [
|
|||
"tags": [ "hr" ],
|
||||
"func": function MD035(params, onError) {
|
||||
var style = params.config.style || "consistent";
|
||||
filterTokens(params, "hr", function forToken(token) {
|
||||
shared.filterTokens(params, "hr", function forToken(token) {
|
||||
var lineTrim = token.line.trim();
|
||||
if (style === "consistent") {
|
||||
style = lineTrim;
|
||||
}
|
||||
addErrorDetailIf(onError, token.lineNumber, style, lineTrim);
|
||||
shared.addErrorDetailIf(onError, token.lineNumber, style, lineTrim);
|
||||
});
|
||||
}
|
||||
},
|
||||
|
|
@ -958,7 +749,8 @@ module.exports = [
|
|||
(children[0].type === "em_open")) &&
|
||||
(children[1].type === "text") &&
|
||||
!re.test(children[1].content)) {
|
||||
addErrorContext(onError, t.lineNumber, children[1].content);
|
||||
shared.addErrorContext(onError, t.lineNumber,
|
||||
children[1].content);
|
||||
}
|
||||
return base;
|
||||
};
|
||||
|
|
@ -991,7 +783,7 @@ module.exports = [
|
|||
"desc": "Spaces inside emphasis markers",
|
||||
"tags": [ "whitespace", "emphasis" ],
|
||||
"func": function MD037(params, onError) {
|
||||
forEachInlineChild(params, "text", function forToken(token) {
|
||||
shared.forEachInlineChild(params, "text", function forToken(token) {
|
||||
var left = true;
|
||||
var match = /\s(\*\*?|__?)\s.+\1/.exec(token.content);
|
||||
if (!match) {
|
||||
|
|
@ -1003,7 +795,7 @@ module.exports = [
|
|||
var line = params.lines[token.lineNumber - 1];
|
||||
var column = line.indexOf(text) + 1;
|
||||
var length = text.length;
|
||||
addErrorContext(onError, token.lineNumber,
|
||||
shared.addErrorContext(onError, token.lineNumber,
|
||||
text, left, !left, [ column, length ]);
|
||||
}
|
||||
});
|
||||
|
|
@ -1016,7 +808,7 @@ module.exports = [
|
|||
"tags": [ "whitespace", "code" ],
|
||||
"func": function MD038(params, onError) {
|
||||
var inlineCodeSpansRe = /(?:^|[^\\])((`+)((?:.*?[^`])|)\2(?!`))/g;
|
||||
forEachInlineChild(params, "code_inline",
|
||||
shared.forEachInlineChild(params, "code_inline",
|
||||
function forToken(token) {
|
||||
var line = params.lines[token.lineNumber - 1];
|
||||
var match = null;
|
||||
|
|
@ -1027,10 +819,10 @@ module.exports = [
|
|||
var column = match.index + 1 + (match[0].length - length);
|
||||
var range = [ column, length ];
|
||||
if (/^\s([^`]|$)/.test(content)) {
|
||||
addErrorContext(onError, token.lineNumber,
|
||||
shared.addErrorContext(onError, token.lineNumber,
|
||||
inlineCodeSpan, true, false, range);
|
||||
} else if (/[^`]\s$/.test(content)) {
|
||||
addErrorContext(onError, token.lineNumber,
|
||||
shared.addErrorContext(onError, token.lineNumber,
|
||||
inlineCodeSpan, false, true, range);
|
||||
}
|
||||
}
|
||||
|
|
@ -1043,7 +835,8 @@ module.exports = [
|
|||
"desc": "Spaces inside link text",
|
||||
"tags": [ "whitespace", "links" ],
|
||||
"func": function MD039(params, onError) {
|
||||
filterTokens(params, "inline", function forToken(token) {
|
||||
var spaceInLinkRe = /\[(?:\s+(?:[^\]]*?)\s*|(?:[^\]]*?)\s+)](?=\(\S*\))/;
|
||||
shared.filterTokens(params, "inline", function forToken(token) {
|
||||
var inLink = false;
|
||||
var linkText = "";
|
||||
token.children.forEach(function forChild(child) {
|
||||
|
|
@ -1055,9 +848,9 @@ module.exports = [
|
|||
var left = shared.trimLeft(linkText).length !== linkText.length;
|
||||
var right = shared.trimRight(linkText).length !== linkText.length;
|
||||
if (left || right) {
|
||||
addErrorContext(onError, token.lineNumber,
|
||||
shared.addErrorContext(onError, token.lineNumber,
|
||||
"[" + linkText + "]", left, right,
|
||||
rangeFromRegExp(token.line, spaceInsideLinkRe));
|
||||
shared.rangeFromRegExp(token.line, spaceInLinkRe));
|
||||
}
|
||||
} else if (inLink) {
|
||||
linkText += child.content;
|
||||
|
|
@ -1072,9 +865,9 @@ module.exports = [
|
|||
"desc": "Fenced code blocks should have a language specified",
|
||||
"tags": [ "code", "language" ],
|
||||
"func": function MD040(params, onError) {
|
||||
filterTokens(params, "fence", function forToken(token) {
|
||||
shared.filterTokens(params, "fence", function forToken(token) {
|
||||
if (!token.info.trim()) {
|
||||
addErrorContext(onError, token.lineNumber, token.line);
|
||||
shared.addErrorContext(onError, token.lineNumber, token.line);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -1094,7 +887,7 @@ module.exports = [
|
|||
if (token.type === "heading_open") {
|
||||
if (!((token.lineNumber === 1) || (index > 0)) ||
|
||||
(token.tag !== tag)) {
|
||||
addErrorContext(onError, token.lineNumber, token.line);
|
||||
shared.addErrorContext(onError, token.lineNumber, token.line);
|
||||
}
|
||||
return false;
|
||||
} else if (token.type === "html_block") {
|
||||
|
|
@ -1104,7 +897,7 @@ module.exports = [
|
|||
!params.frontMatterLines.some(function forLine(line) {
|
||||
return frontMatterTitleRe.test(line);
|
||||
})) {
|
||||
addErrorContext(onError, token.lineNumber, token.line);
|
||||
shared.addErrorContext(onError, token.lineNumber, token.line);
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
|
@ -1116,7 +909,8 @@ module.exports = [
|
|||
"desc": "No empty links",
|
||||
"tags": [ "links" ],
|
||||
"func": function MD042(params, onError) {
|
||||
filterTokens(params, "inline", function forToken(token) {
|
||||
var emptyLinkRe = /\[[^\]]*](?:\((?:#?|(?:<>))\))/;
|
||||
shared.filterTokens(params, "inline", function forToken(token) {
|
||||
var inLink = false;
|
||||
var linkText = "";
|
||||
var emptyLink = false;
|
||||
|
|
@ -1132,9 +926,9 @@ module.exports = [
|
|||
} else if (child.type === "link_close") {
|
||||
inLink = false;
|
||||
if (emptyLink) {
|
||||
addErrorContext(onError, child.lineNumber,
|
||||
shared.addErrorContext(onError, child.lineNumber,
|
||||
"[" + linkText + "]()", null, null,
|
||||
rangeFromRegExp(child.line, emptyLinkRe));
|
||||
shared.rangeFromRegExp(child.line, emptyLinkRe));
|
||||
}
|
||||
} else if (inLink) {
|
||||
linkText += child.content;
|
||||
|
|
@ -1158,7 +952,7 @@ module.exports = [
|
|||
var i = 0;
|
||||
var optional = false;
|
||||
var errorCount = 0;
|
||||
forEachHeading(params, function forHeading(heading, content) {
|
||||
shared.forEachHeading(params, function forHeading(heading, content) {
|
||||
if (!errorCount) {
|
||||
var actual = levels[heading.tag] + " " + content;
|
||||
var expected = requiredHeaders[i++] || "[None]";
|
||||
|
|
@ -1169,13 +963,15 @@ module.exports = [
|
|||
} else if (optional) {
|
||||
i--;
|
||||
} else {
|
||||
addErrorDetailIf(onError, heading.lineNumber, expected, actual);
|
||||
shared.addErrorDetailIf(onError, heading.lineNumber,
|
||||
expected, actual);
|
||||
errorCount++;
|
||||
}
|
||||
}
|
||||
});
|
||||
if ((i < requiredHeaders.length) && !errorCount) {
|
||||
addErrorContext(onError, params.lines.length, requiredHeaders[i]);
|
||||
shared.addErrorContext(onError, params.lines.length,
|
||||
requiredHeaders[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1190,7 +986,7 @@ module.exports = [
|
|||
var codeBlocks = params.config.code_blocks;
|
||||
var includeCodeBlocks = (codeBlocks === undefined) ? true : !!codeBlocks;
|
||||
names.forEach(function forName(name) {
|
||||
var escapedName = escapeForRegExp(name);
|
||||
var escapedName = shared.escapeForRegExp(name);
|
||||
var namePattern = "\\S*\\b(" + escapedName + ")\\b\\S*";
|
||||
var anyNameRe = new RegExp(namePattern, "gi");
|
||||
function forToken(token) {
|
||||
|
|
@ -1200,24 +996,24 @@ module.exports = [
|
|||
var match = null;
|
||||
while ((match = anyNameRe.exec(line)) !== null) {
|
||||
var fullMatch = match[0];
|
||||
if (!bareUrlRe.test(fullMatch)) {
|
||||
if (!shared.bareUrlRe.test(fullMatch)) {
|
||||
var wordMatch = fullMatch
|
||||
.replace(/^\W*/, "").replace(/\W*$/, "");
|
||||
if (names.indexOf(wordMatch) === -1) {
|
||||
var lineNumber = token.lineNumber + index + fenceOffset;
|
||||
var range = [ match.index + 1, wordMatch.length ];
|
||||
addErrorDetailIf(onError, lineNumber,
|
||||
shared.addErrorDetailIf(onError, lineNumber,
|
||||
name, match[1], null, range);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
forEachInlineChild(params, "text", forToken);
|
||||
shared.forEachInlineChild(params, "text", forToken);
|
||||
if (includeCodeBlocks) {
|
||||
forEachInlineChild(params, "code_inline", forToken);
|
||||
filterTokens(params, "code_block", forToken);
|
||||
filterTokens(params, "fence", forToken);
|
||||
shared.forEachInlineChild(params, "code_inline", forToken);
|
||||
shared.filterTokens(params, "code_block", forToken);
|
||||
shared.filterTokens(params, "fence", forToken);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -1227,9 +1023,9 @@ module.exports = [
|
|||
"desc": "Images should have alternate text (alt text)",
|
||||
"tags": [ "accessibility", "images" ],
|
||||
"func": function MD045(params, onError) {
|
||||
forEachInlineChild(params, "image", function forToken(token) {
|
||||
shared.forEachInlineChild(params, "image", function forToken(token) {
|
||||
if (token.content === "") {
|
||||
addError(onError, token.lineNumber);
|
||||
shared.addError(onError, token.lineNumber);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
225
lib/shared.js
225
lib/shared.js
|
|
@ -13,6 +13,11 @@ var inlineCommentRe =
|
|||
/<!--\s*markdownlint-(dis|en)able((?:\s+[a-z0-9_-]+)*)\s*-->/ig;
|
||||
module.exports.inlineCommentRe = inlineCommentRe;
|
||||
|
||||
// Regular expressions for range matching
|
||||
module.exports.atxHeaderSpaceRe = /^#+\s*\S/;
|
||||
module.exports.bareUrlRe = /(?:http|ftp)s?:\/\/[^\s]*/i;
|
||||
module.exports.listItemMarkerRe = /^[\s>]*(?:[*+-]|\d+\.)\s+/;
|
||||
|
||||
// readFile options for reading with the UTF-8 encoding
|
||||
module.exports.utf8Encoding = { "encoding": "utf8" };
|
||||
|
||||
|
|
@ -23,10 +28,9 @@ function trimLeft(str) {
|
|||
module.exports.trimLeft = trimLeft;
|
||||
|
||||
// Trims whitespace from the right (end) of a string
|
||||
function trimRight(str) {
|
||||
module.exports.trimRight = function trimRight(str) {
|
||||
return str.replace(/\s*$/, "");
|
||||
}
|
||||
module.exports.trimRight = trimRight;
|
||||
};
|
||||
|
||||
// Applies key/value pairs from src to dst, returning dst
|
||||
function assign(dst, src) {
|
||||
|
|
@ -48,7 +52,7 @@ module.exports.clone = function clone(obj) {
|
|||
// See https://www.w3.org/TR/html5/syntax.html#comments for details
|
||||
var htmlCommentBegin = "<!--";
|
||||
var htmlCommentEnd = "-->";
|
||||
function clearHtmlCommentText(text) {
|
||||
module.exports.clearHtmlCommentText = function clearHtmlCommentText(text) {
|
||||
var i = 0;
|
||||
while ((i = text.indexOf(htmlCommentBegin, i)) !== -1) {
|
||||
var j = text.indexOf(htmlCommentEnd, i);
|
||||
|
|
@ -72,5 +76,216 @@ function clearHtmlCommentText(text) {
|
|||
i = j + htmlCommentEnd.length;
|
||||
}
|
||||
return text;
|
||||
};
|
||||
|
||||
// Escapes a string for use in a RegExp
|
||||
module.exports.escapeForRegExp = function escapeForRegExp(str) {
|
||||
return str.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
|
||||
};
|
||||
|
||||
// Returns the indent for a token
|
||||
module.exports.indentFor = function indentFor(token) {
|
||||
var line = token.line.replace(/^[\s>]*(> |>)/, "");
|
||||
return line.length - trimLeft(line).length;
|
||||
};
|
||||
|
||||
// Returns the heading style for a heading token
|
||||
module.exports.headingStyleFor = function headingStyleFor(token) {
|
||||
if ((token.map[1] - token.map[0]) === 1) {
|
||||
if (/#\s*$/.test(token.line)) {
|
||||
return "atx_closed";
|
||||
}
|
||||
return "atx";
|
||||
}
|
||||
return "setext";
|
||||
};
|
||||
|
||||
// Returns the unordered list style for a list item token
|
||||
module.exports.unorderedListStyleFor = function unorderedListStyleFor(token) {
|
||||
switch (trimLeft(token.line).substr(0, 1)) {
|
||||
case "-":
|
||||
return "dash";
|
||||
case "+":
|
||||
return "plus";
|
||||
// case "*":
|
||||
default:
|
||||
return "asterisk";
|
||||
}
|
||||
};
|
||||
|
||||
// Calls the provided function for each matching token
|
||||
function filterTokens(params, type, callback) {
|
||||
(params.tokenLists[type] || []).forEach(callback);
|
||||
}
|
||||
module.exports.clearHtmlCommentText = clearHtmlCommentText;
|
||||
module.exports.filterTokens = filterTokens;
|
||||
|
||||
// Calls the provided function for each line (with context)
|
||||
module.exports.forEachLine = function forEachLine(params, callback) {
|
||||
if (!params.forEachLine) {
|
||||
var lineMetadata = new Array(params.lines.length);
|
||||
var fenceStart = null;
|
||||
var inFence = false;
|
||||
// Find fenced code by pattern (parser ignores "``` close fence")
|
||||
params.lines.forEach(function forLine(line, lineIndex) {
|
||||
var metadata = 0;
|
||||
var match = /^(`{3,}|~{3,})/.exec(line);
|
||||
var fence = match && match[1];
|
||||
if (fence &&
|
||||
(!inFence || (fence.substr(0, fenceStart.length) === fenceStart))) {
|
||||
metadata = inFence ? 2 : 6;
|
||||
fenceStart = inFence ? null : fence;
|
||||
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;
|
||||
}
|
||||
});
|
||||
// Find tables normally
|
||||
filterTokens(params, "table_open", function forToken(token) {
|
||||
for (var i = token.map[0]; i < token.map[1]; i++) {
|
||||
lineMetadata[i] += 8;
|
||||
}
|
||||
});
|
||||
params.forEachLine = lineMetadata;
|
||||
}
|
||||
// Invoke callback
|
||||
params.lines.forEach(function forLine(line, lineIndex) {
|
||||
var metadata = params.forEachLine[lineIndex];
|
||||
callback(
|
||||
line,
|
||||
lineIndex,
|
||||
!!(metadata & 7),
|
||||
(((metadata & 6) >> 1) || 2) - 2,
|
||||
!!(metadata & 8));
|
||||
});
|
||||
};
|
||||
|
||||
// Calls the provided function for each specified inline child token
|
||||
module.exports.forEachInlineChild =
|
||||
function forEachInlineChild(params, type, callback) {
|
||||
filterTokens(params, "inline", function forToken(token) {
|
||||
token.children.forEach(function forChild(child) {
|
||||
if (child.type === type) {
|
||||
callback(child);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// Calls the provided function for each heading's content
|
||||
module.exports.forEachHeading = 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);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// Returns (nested) lists as a flat array (in order)
|
||||
module.exports.flattenLists = 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 = {
|
||||
"unordered": (token.type === "bullet_list_open"),
|
||||
"parentsUnordered": !current ||
|
||||
(current.unordered && current.parentsUnordered),
|
||||
"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];
|
||||
lists.splice(current.insert, 0, current);
|
||||
delete current.insert;
|
||||
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;
|
||||
}
|
||||
});
|
||||
params.flattenLists = lists;
|
||||
}
|
||||
return params.flattenLists;
|
||||
};
|
||||
|
||||
// Adds a generic error object via the onError callback
|
||||
function addError(onError, lineNumber, detail, context, range) {
|
||||
onError({
|
||||
"lineNumber": lineNumber,
|
||||
"detail": detail,
|
||||
"context": context,
|
||||
"range": range
|
||||
});
|
||||
}
|
||||
module.exports.addError = addError;
|
||||
|
||||
// Adds an error object with details conditionally via the onError callback
|
||||
module.exports.addErrorDetailIf = function addErrorDetailIf(
|
||||
onError, lineNumber, expected, actual, detail, range) {
|
||||
if (expected !== actual) {
|
||||
addError(
|
||||
onError,
|
||||
lineNumber,
|
||||
"Expected: " + expected + "; Actual: " + actual +
|
||||
(detail ? "; " + detail : ""),
|
||||
null,
|
||||
range);
|
||||
}
|
||||
};
|
||||
|
||||
// Adds an error object with context via the onError callback
|
||||
module.exports.addErrorContext =
|
||||
function addErrorContext(onError, lineNumber, context, left, right, range) {
|
||||
if (context.length <= 30) {
|
||||
// Nothing to do
|
||||
} else if (left && right) {
|
||||
context = context.substr(0, 15) + "..." + context.substr(-15);
|
||||
} else if (right) {
|
||||
context = "..." + context.substr(-30);
|
||||
} else {
|
||||
context = context.substr(0, 30) + "...";
|
||||
}
|
||||
addError(onError, lineNumber, null, context, range);
|
||||
};
|
||||
|
||||
// Returns a range object for a line by applying a RegExp
|
||||
module.exports.rangeFromRegExp = function rangeFromRegExp(line, regexp) {
|
||||
var range = null;
|
||||
var match = line.match(regexp);
|
||||
if (match) {
|
||||
var column = match.index + 1;
|
||||
var length = match[0].length;
|
||||
if (match[2]) {
|
||||
column += match[1].length;
|
||||
length -= match[1].length;
|
||||
}
|
||||
range = [ column, length ];
|
||||
}
|
||||
return range;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue