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");
|
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 = [
|
module.exports = [
|
||||||
{
|
{
|
||||||
"names": [ "MD001", "header-increment" ],
|
"names": [ "MD001", "header-increment" ],
|
||||||
|
|
@ -238,10 +11,10 @@ module.exports = [
|
||||||
"tags": [ "headers" ],
|
"tags": [ "headers" ],
|
||||||
"func": function MD001(params, onError) {
|
"func": function MD001(params, onError) {
|
||||||
var prevLevel = 0;
|
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);
|
var level = parseInt(token.tag.slice(1), 10);
|
||||||
if (prevLevel && (level > prevLevel)) {
|
if (prevLevel && (level > prevLevel)) {
|
||||||
addErrorDetailIf(onError, token.lineNumber,
|
shared.addErrorDetailIf(onError, token.lineNumber,
|
||||||
"h" + (prevLevel + 1), "h" + level);
|
"h" + (prevLevel + 1), "h" + level);
|
||||||
}
|
}
|
||||||
prevLevel = level;
|
prevLevel = level;
|
||||||
|
|
@ -258,7 +31,7 @@ module.exports = [
|
||||||
var tag = "h" + level;
|
var tag = "h" + level;
|
||||||
params.tokens.every(function forToken(token) {
|
params.tokens.every(function forToken(token) {
|
||||||
if (token.type === "heading_open") {
|
if (token.type === "heading_open") {
|
||||||
addErrorDetailIf(onError, token.lineNumber, tag, token.tag);
|
shared.addErrorDetailIf(onError, token.lineNumber, tag, token.tag);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -272,8 +45,8 @@ module.exports = [
|
||||||
"tags": [ "headers" ],
|
"tags": [ "headers" ],
|
||||||
"func": function MD003(params, onError) {
|
"func": function MD003(params, onError) {
|
||||||
var style = params.config.style || "consistent";
|
var style = params.config.style || "consistent";
|
||||||
filterTokens(params, "heading_open", function forToken(token) {
|
shared.filterTokens(params, "heading_open", function forToken(token) {
|
||||||
var styleForToken = headingStyleFor(token);
|
var styleForToken = shared.headingStyleFor(token);
|
||||||
if (style === "consistent") {
|
if (style === "consistent") {
|
||||||
style = styleForToken;
|
style = styleForToken;
|
||||||
}
|
}
|
||||||
|
|
@ -294,7 +67,7 @@ module.exports = [
|
||||||
} else if (style === "setext_with_atx_closed") {
|
} else if (style === "setext_with_atx_closed") {
|
||||||
expected = h12 ? "setext" : "atx_closed";
|
expected = h12 ? "setext" : "atx_closed";
|
||||||
}
|
}
|
||||||
addErrorDetailIf(onError, token.lineNumber,
|
shared.addErrorDetailIf(onError, token.lineNumber,
|
||||||
expected, styleForToken);
|
expected, styleForToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -310,27 +83,27 @@ module.exports = [
|
||||||
var style = params.config.style || "consistent";
|
var style = params.config.style || "consistent";
|
||||||
var expectedStyle = style;
|
var expectedStyle = style;
|
||||||
var nestingStyles = [];
|
var nestingStyles = [];
|
||||||
flattenLists(params).forEach(function forList(list) {
|
shared.flattenLists(params).forEach(function forList(list) {
|
||||||
if (list.unordered) {
|
if (list.unordered) {
|
||||||
if (expectedStyle === "consistent") {
|
if (expectedStyle === "consistent") {
|
||||||
expectedStyle = unorderedListStyleFor(list.items[0]);
|
expectedStyle = shared.unorderedListStyleFor(list.items[0]);
|
||||||
}
|
}
|
||||||
list.items.forEach(function forItem(item) {
|
list.items.forEach(function forItem(item) {
|
||||||
var itemStyle = unorderedListStyleFor(item);
|
var itemStyle = shared.unorderedListStyleFor(item);
|
||||||
if (style === "sublist") {
|
if (style === "sublist") {
|
||||||
var nesting = list.nesting;
|
var nesting = list.nesting;
|
||||||
if (!nestingStyles[nesting] &&
|
if (!nestingStyles[nesting] &&
|
||||||
(itemStyle !== nestingStyles[nesting - 1])) {
|
(itemStyle !== nestingStyles[nesting - 1])) {
|
||||||
nestingStyles[nesting] = itemStyle;
|
nestingStyles[nesting] = itemStyle;
|
||||||
} else {
|
} else {
|
||||||
addErrorDetailIf(onError, item.lineNumber,
|
shared.addErrorDetailIf(onError, item.lineNumber,
|
||||||
nestingStyles[nesting], itemStyle, null,
|
nestingStyles[nesting], itemStyle, null,
|
||||||
rangeFromRegExp(item.line, listItemMarkerRe));
|
shared.rangeFromRegExp(item.line, shared.listItemMarkerRe));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
addErrorDetailIf(onError, item.lineNumber,
|
shared.addErrorDetailIf(onError, item.lineNumber,
|
||||||
expectedStyle, itemStyle, null,
|
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",
|
"desc": "Inconsistent indentation for list items at the same level",
|
||||||
"tags": [ "bullet", "ul", "indentation" ],
|
"tags": [ "bullet", "ul", "indentation" ],
|
||||||
"func": function MD005(params, onError) {
|
"func": function MD005(params, onError) {
|
||||||
flattenLists(params).forEach(function forList(list) {
|
shared.flattenLists(params).forEach(function forList(list) {
|
||||||
var indent = indentFor(list.items[0]);
|
var indent = shared.indentFor(list.items[0]);
|
||||||
list.items.forEach(function forItem(item) {
|
list.items.forEach(function forItem(item) {
|
||||||
addErrorDetailIf(onError, item.lineNumber, indent, indentFor(item),
|
shared.addErrorDetailIf(onError, item.lineNumber, indent,
|
||||||
null, rangeFromRegExp(item.line, listItemMarkerRe));
|
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",
|
"desc": "Consider starting bulleted lists at the beginning of the line",
|
||||||
"tags": [ "bullet", "ul", "indentation" ],
|
"tags": [ "bullet", "ul", "indentation" ],
|
||||||
"func": function MD006(params, onError) {
|
"func": function MD006(params, onError) {
|
||||||
flattenLists(params).forEach(function forList(list) {
|
shared.flattenLists(params).forEach(function forList(list) {
|
||||||
if (list.unordered && !list.nesting) {
|
if (list.unordered && !list.nesting) {
|
||||||
addErrorDetailIf(onError, list.open.lineNumber,
|
shared.addErrorDetailIf(onError, list.open.lineNumber,
|
||||||
0, indentFor(list.open), null,
|
0, shared.indentFor(list.open), null,
|
||||||
rangeFromRegExp(list.open.line, listItemMarkerRe));
|
shared.rangeFromRegExp(list.open.line, shared.listItemMarkerRe));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -375,13 +149,13 @@ module.exports = [
|
||||||
"func": function MD007(params, onError) {
|
"func": function MD007(params, onError) {
|
||||||
var optionsIndent = params.config.indent || 2;
|
var optionsIndent = params.config.indent || 2;
|
||||||
var prevIndent = 0;
|
var prevIndent = 0;
|
||||||
flattenLists(params).forEach(function forList(list) {
|
shared.flattenLists(params).forEach(function forList(list) {
|
||||||
if (list.unordered && list.parentsUnordered) {
|
if (list.unordered && list.parentsUnordered) {
|
||||||
var indent = indentFor(list.open);
|
var indent = shared.indentFor(list.open);
|
||||||
if (indent > prevIndent) {
|
if (indent > prevIndent) {
|
||||||
addErrorDetailIf(onError, list.open.lineNumber,
|
shared.addErrorDetailIf(onError, list.open.lineNumber,
|
||||||
prevIndent + optionsIndent, indent, null,
|
prevIndent + optionsIndent, indent, null,
|
||||||
rangeFromRegExp(list.open.line, listItemMarkerRe));
|
shared.rangeFromRegExp(list.open.line, shared.listItemMarkerRe));
|
||||||
}
|
}
|
||||||
prevIndent = indent;
|
prevIndent = indent;
|
||||||
}
|
}
|
||||||
|
|
@ -396,24 +170,25 @@ module.exports = [
|
||||||
"func": function MD009(params, onError) {
|
"func": function MD009(params, onError) {
|
||||||
var brSpaces = params.config.br_spaces || 0;
|
var brSpaces = params.config.br_spaces || 0;
|
||||||
var listItemEmptyLines = params.config.list_item_empty_lines;
|
var listItemEmptyLines = params.config.list_item_empty_lines;
|
||||||
|
var trailingSpaceRe = /\s+$/;
|
||||||
var allowListItemEmptyLines =
|
var allowListItemEmptyLines =
|
||||||
(listItemEmptyLines === undefined) ? false : !!listItemEmptyLines;
|
(listItemEmptyLines === undefined) ? false : !!listItemEmptyLines;
|
||||||
var listItemLineNumbers = [];
|
var listItemLineNumbers = [];
|
||||||
if (allowListItemEmptyLines) {
|
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++) {
|
for (var i = token.map[0]; i < token.map[1]; i++) {
|
||||||
listItemLineNumbers.push(i + 1);
|
listItemLineNumbers.push(i + 1);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
forEachLine(params, function forLine(line, lineIndex) {
|
shared.forEachLine(params, function forLine(line, lineIndex) {
|
||||||
var lineNumber = lineIndex + 1;
|
var lineNumber = lineIndex + 1;
|
||||||
if (trailingSpaceRe.test(line) &&
|
if (trailingSpaceRe.test(line) &&
|
||||||
(listItemLineNumbers.indexOf(lineNumber) === -1)) {
|
(listItemLineNumbers.indexOf(lineNumber) === -1)) {
|
||||||
var expected = (brSpaces < 2) ? 0 : brSpaces;
|
var expected = (brSpaces < 2) ? 0 : brSpaces;
|
||||||
addErrorDetailIf(onError, lineNumber,
|
shared.addErrorDetailIf(onError, lineNumber,
|
||||||
expected, line.length - shared.trimRight(line).length, null,
|
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) {
|
"func": function MD010(params, onError) {
|
||||||
var codeBlocks = params.config.code_blocks;
|
var codeBlocks = params.config.code_blocks;
|
||||||
var includeCodeBlocks = (codeBlocks === undefined) ? true : !!codeBlocks;
|
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)) {
|
if (tabRe.test(line) && (!inCode || includeCodeBlocks)) {
|
||||||
addError(onError, lineIndex + 1,
|
shared.addError(onError, lineIndex + 1,
|
||||||
"Column: " + (line.indexOf("\t") + 1), null,
|
"Column: " + (line.indexOf("\t") + 1), null,
|
||||||
rangeFromRegExp(line, tabRe));
|
shared.rangeFromRegExp(line, tabRe));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -441,11 +217,12 @@ module.exports = [
|
||||||
"desc": "Reversed link syntax",
|
"desc": "Reversed link syntax",
|
||||||
"tags": [ "links" ],
|
"tags": [ "links" ],
|
||||||
"func": function MD011(params, onError) {
|
"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);
|
var match = reversedLinkRe.exec(token.content);
|
||||||
if (match) {
|
if (match) {
|
||||||
addError(onError, token.lineNumber, match[0], null,
|
shared.addError(onError, token.lineNumber, match[0], null,
|
||||||
rangeFromRegExp(token.line, reversedLinkRe));
|
shared.rangeFromRegExp(token.line, reversedLinkRe));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -458,10 +235,10 @@ module.exports = [
|
||||||
"func": function MD012(params, onError) {
|
"func": function MD012(params, onError) {
|
||||||
var maximum = params.config.maximum || 1;
|
var maximum = params.config.maximum || 1;
|
||||||
var count = 0;
|
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;
|
count = (inCode || line.trim().length) ? 0 : count + 1;
|
||||||
if (maximum < count) {
|
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",
|
"desc": "Line length",
|
||||||
"tags": [ "line_length" ],
|
"tags": [ "line_length" ],
|
||||||
"func": function MD013(params, onError) {
|
"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 codeBlocks = params.config.code_blocks;
|
||||||
var includeCodeBlocks = (codeBlocks === undefined) ? true : !!codeBlocks;
|
var includeCodeBlocks = (codeBlocks === undefined) ? true : !!codeBlocks;
|
||||||
var tables = params.config.tables;
|
var tables = params.config.tables;
|
||||||
|
|
@ -481,7 +258,7 @@ module.exports = [
|
||||||
var includeHeaders = (headers === undefined) ? true : !!headers;
|
var includeHeaders = (headers === undefined) ? true : !!headers;
|
||||||
var headerLineNumbers = [];
|
var headerLineNumbers = [];
|
||||||
if (!includeHeaders) {
|
if (!includeHeaders) {
|
||||||
forEachHeading(params, function forHeading(heading) {
|
shared.forEachHeading(params, function forHeading(heading) {
|
||||||
headerLineNumbers.push(heading.lineNumber);
|
headerLineNumbers.push(heading.lineNumber);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -495,7 +272,7 @@ module.exports = [
|
||||||
"text": "T"
|
"text": "T"
|
||||||
};
|
};
|
||||||
var linkOnlyLineNumbers = [];
|
var linkOnlyLineNumbers = [];
|
||||||
filterTokens(params, "inline", function forToken(token) {
|
shared.filterTokens(params, "inline", function forToken(token) {
|
||||||
var childTokenTypes = "";
|
var childTokenTypes = "";
|
||||||
token.children.forEach(function forChild(child) {
|
token.children.forEach(function forChild(child) {
|
||||||
if (child.type !== "text" || child.content !== "") {
|
if (child.type !== "text" || child.content !== "") {
|
||||||
|
|
@ -508,7 +285,7 @@ module.exports = [
|
||||||
});
|
});
|
||||||
var longLineRe = new RegExp("^(.{" + lineLength + "})(.*\\s.*)$");
|
var longLineRe = new RegExp("^(.{" + lineLength + "})(.*\\s.*)$");
|
||||||
var labelRe = /^\s*\[.*[^\\]]:/;
|
var labelRe = /^\s*\[.*[^\\]]:/;
|
||||||
forEachLine(params,
|
shared.forEachLine(params,
|
||||||
function forLine(line, lineIndex, inCode, onFence, inTable) {
|
function forLine(line, lineIndex, inCode, onFence, inTable) {
|
||||||
var lineNumber = lineIndex + 1;
|
var lineNumber = lineIndex + 1;
|
||||||
if ((includeCodeBlocks || !inCode) &&
|
if ((includeCodeBlocks || !inCode) &&
|
||||||
|
|
@ -517,8 +294,8 @@ module.exports = [
|
||||||
(linkOnlyLineNumbers.indexOf(lineNumber) < 0) &&
|
(linkOnlyLineNumbers.indexOf(lineNumber) < 0) &&
|
||||||
longLineRe.test(line) &&
|
longLineRe.test(line) &&
|
||||||
!labelRe.test(line)) {
|
!labelRe.test(line)) {
|
||||||
addErrorDetailIf(onError, lineNumber, lineLength, line.length, null,
|
shared.addErrorDetailIf(onError, lineNumber, lineLength,
|
||||||
rangeFromRegExp(line, longLineRe));
|
line.length, null, shared.rangeFromRegExp(line, longLineRe));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -529,16 +306,17 @@ module.exports = [
|
||||||
"desc": "Dollar signs used before commands without showing output",
|
"desc": "Dollar signs used before commands without showing output",
|
||||||
"tags": [ "code" ],
|
"tags": [ "code" ],
|
||||||
"func": function MD014(params, onError) {
|
"func": function MD014(params, onError) {
|
||||||
|
var dollarCommandRe = /^(\s*)(\$\s)/;
|
||||||
[ "code_block", "fence" ].forEach(function forType(type) {
|
[ "code_block", "fence" ].forEach(function forType(type) {
|
||||||
filterTokens(params, type, function forToken(token) {
|
shared.filterTokens(params, type, function forToken(token) {
|
||||||
var allBlank = true;
|
var allBlank = true;
|
||||||
if (token.content && token.content.split(shared.newLineRe)
|
if (token.content && token.content.split(shared.newLineRe)
|
||||||
.every(function forLine(line) {
|
.every(function forLine(line) {
|
||||||
return !line || (allBlank = false) || dollarCommandRe.test(line);
|
return !line || (allBlank = false) || dollarCommandRe.test(line);
|
||||||
}) && !allBlank) {
|
}) && !allBlank) {
|
||||||
addErrorContext(onError, token.lineNumber,
|
shared.addErrorContext(onError, token.lineNumber,
|
||||||
token.content.split(shared.newLineRe)[0].trim(), null, null,
|
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",
|
"desc": "No space after hash on atx style header",
|
||||||
"tags": [ "headers", "atx", "spaces" ],
|
"tags": [ "headers", "atx", "spaces" ],
|
||||||
"func": function MD018(params, onError) {
|
"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)) {
|
if (!inCode && /^#+[^#\s]/.test(line) && !/#$/.test(line)) {
|
||||||
addErrorContext(onError, lineIndex + 1, line.trim(), null, null,
|
shared.addErrorContext(onError, lineIndex + 1, line.trim(), null,
|
||||||
rangeFromRegExp(line, atxHeaderSpaceRe));
|
null, shared.rangeFromRegExp(line, shared.atxHeaderSpaceRe));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -564,11 +342,12 @@ module.exports = [
|
||||||
"desc": "Multiple spaces after hash on atx style header",
|
"desc": "Multiple spaces after hash on atx style header",
|
||||||
"tags": [ "headers", "atx", "spaces" ],
|
"tags": [ "headers", "atx", "spaces" ],
|
||||||
"func": function MD019(params, onError) {
|
"func": function MD019(params, onError) {
|
||||||
filterTokens(params, "heading_open", function forToken(token) {
|
shared.filterTokens(params, "heading_open", function forToken(token) {
|
||||||
if ((headingStyleFor(token) === "atx") &&
|
if ((shared.headingStyleFor(token) === "atx") &&
|
||||||
/^#+\s\s/.test(token.line)) {
|
/^#+\s\s/.test(token.line)) {
|
||||||
addErrorContext(onError, token.lineNumber, token.line.trim(),
|
shared.addErrorContext(onError, token.lineNumber, token.line.trim(),
|
||||||
null, null, rangeFromRegExp(token.line, atxHeaderSpaceRe));
|
null, null,
|
||||||
|
shared.rangeFromRegExp(token.line, shared.atxHeaderSpaceRe));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -579,13 +358,14 @@ module.exports = [
|
||||||
"desc": "No space inside hashes on closed atx style header",
|
"desc": "No space inside hashes on closed atx style header",
|
||||||
"tags": [ "headers", "atx_closed", "spaces" ],
|
"tags": [ "headers", "atx_closed", "spaces" ],
|
||||||
"func": function MD020(params, onError) {
|
"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)) {
|
if (!inCode && /^#+[^#]*[^\\]#+$/.test(line)) {
|
||||||
var left = /^#+[^#\s]/.test(line);
|
var left = /^#+[^#\s]/.test(line);
|
||||||
var right = /[^#\s]#+$/.test(line);
|
var right = /[^#\s]#+$/.test(line);
|
||||||
if (left || right) {
|
if (left || right) {
|
||||||
addErrorContext(onError, lineIndex + 1, line.trim(), left, right,
|
shared.addErrorContext(onError, lineIndex + 1, line.trim(), left,
|
||||||
rangeFromRegExp(line, atxClosedHeaderNoSpaceRe));
|
right, shared.rangeFromRegExp(line, atxClosedHeaderNoSpaceRe));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -597,13 +377,15 @@ module.exports = [
|
||||||
"desc": "Multiple spaces inside hashes on closed atx style header",
|
"desc": "Multiple spaces inside hashes on closed atx style header",
|
||||||
"tags": [ "headers", "atx_closed", "spaces" ],
|
"tags": [ "headers", "atx_closed", "spaces" ],
|
||||||
"func": function MD021(params, onError) {
|
"func": function MD021(params, onError) {
|
||||||
filterTokens(params, "heading_open", function forToken(token) {
|
var atxClosedHeaderSpaceRe = /(?:^#+\s\s+?\S)|(?:\S\s\s+?#+\s*$)/;
|
||||||
if (headingStyleFor(token) === "atx_closed") {
|
shared.filterTokens(params, "heading_open", function forToken(token) {
|
||||||
|
if (shared.headingStyleFor(token) === "atx_closed") {
|
||||||
var left = /^#+\s\s/.test(token.line);
|
var left = /^#+\s\s/.test(token.line);
|
||||||
var right = /\s\s#+$/.test(token.line);
|
var right = /\s\s#+$/.test(token.line);
|
||||||
if (left || right) {
|
if (left || right) {
|
||||||
addErrorContext(onError, token.lineNumber, token.line.trim(),
|
shared.addErrorContext(onError, token.lineNumber, token.line.trim(),
|
||||||
left, right, rangeFromRegExp(token.line, atxClosedHeaderSpaceRe));
|
left, right,
|
||||||
|
shared.rangeFromRegExp(token.line, atxClosedHeaderSpaceRe));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -621,7 +403,8 @@ module.exports = [
|
||||||
params.tokens.forEach(function forToken(token) {
|
params.tokens.forEach(function forToken(token) {
|
||||||
if (token.type === "heading_open") {
|
if (token.type === "heading_open") {
|
||||||
if ((token.map[0] - prevMaxLineIndex) === 0) {
|
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") {
|
} else if (token.type === "heading_close") {
|
||||||
needBlankLine = true;
|
needBlankLine = true;
|
||||||
|
|
@ -629,7 +412,7 @@ module.exports = [
|
||||||
if (token.map) {
|
if (token.map) {
|
||||||
if (needBlankLine) {
|
if (needBlankLine) {
|
||||||
if ((token.map[0] - prevMaxLineIndex) === 0) {
|
if ((token.map[0] - prevMaxLineIndex) === 0) {
|
||||||
addErrorContext(onError, prevHeadingLineNumber,
|
shared.addErrorContext(onError, prevHeadingLineNumber,
|
||||||
params.lines[prevHeadingLineNumber - 1].trim());
|
params.lines[prevHeadingLineNumber - 1].trim());
|
||||||
}
|
}
|
||||||
needBlankLine = false;
|
needBlankLine = false;
|
||||||
|
|
@ -648,10 +431,11 @@ module.exports = [
|
||||||
"desc": "Headers must start at the beginning of the line",
|
"desc": "Headers must start at the beginning of the line",
|
||||||
"tags": [ "headers", "spaces" ],
|
"tags": [ "headers", "spaces" ],
|
||||||
"func": function MD023(params, onError) {
|
"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)) {
|
if (spaceBeforeHeaderRe.test(token.line)) {
|
||||||
addErrorContext(onError, token.lineNumber, token.line, null, null,
|
shared.addErrorContext(onError, token.lineNumber, token.line, null,
|
||||||
rangeFromRegExp(token.line, spaceBeforeHeaderRe));
|
null, shared.rangeFromRegExp(token.line, spaceBeforeHeaderRe));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -663,11 +447,12 @@ module.exports = [
|
||||||
"tags": [ "headers" ],
|
"tags": [ "headers" ],
|
||||||
"func": function MD024(params, onError) {
|
"func": function MD024(params, onError) {
|
||||||
var knownContent = [];
|
var knownContent = [];
|
||||||
forEachHeading(params, function forHeading(heading, content) {
|
shared.forEachHeading(params, function forHeading(heading, content) {
|
||||||
if (knownContent.indexOf(content) === -1) {
|
if (knownContent.indexOf(content) === -1) {
|
||||||
knownContent.push(content);
|
knownContent.push(content);
|
||||||
} else {
|
} 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 level = params.config.level || 1;
|
||||||
var tag = "h" + level;
|
var tag = "h" + level;
|
||||||
var hasTopLevelHeading = false;
|
var hasTopLevelHeading = false;
|
||||||
filterTokens(params, "heading_open", function forToken(token) {
|
shared.filterTokens(params, "heading_open", function forToken(token) {
|
||||||
if (token.tag === tag) {
|
if (token.tag === tag) {
|
||||||
if (hasTopLevelHeading) {
|
if (hasTopLevelHeading) {
|
||||||
addErrorContext(onError, token.lineNumber, token.line.trim());
|
shared.addErrorContext(onError, token.lineNumber,
|
||||||
|
token.line.trim());
|
||||||
} else if (token.lineNumber === 1) {
|
} else if (token.lineNumber === 1) {
|
||||||
hasTopLevelHeading = true;
|
hasTopLevelHeading = true;
|
||||||
}
|
}
|
||||||
|
|
@ -699,13 +485,13 @@ module.exports = [
|
||||||
"tags": [ "headers" ],
|
"tags": [ "headers" ],
|
||||||
"func": function MD026(params, onError) {
|
"func": function MD026(params, onError) {
|
||||||
var punctuation = params.config.punctuation || ".,;:!?";
|
var punctuation = params.config.punctuation || ".,;:!?";
|
||||||
var re = new RegExp("[" + punctuation + "]$");
|
var trailingPunctuationRe = new RegExp("[" + punctuation + "]$");
|
||||||
forEachHeading(params, function forHeading(heading, content) {
|
shared.forEachHeading(params, function forHeading(heading, content) {
|
||||||
var match = re.exec(content);
|
var match = trailingPunctuationRe.exec(content);
|
||||||
if (match) {
|
if (match) {
|
||||||
addError(onError, heading.lineNumber,
|
shared.addError(onError, heading.lineNumber,
|
||||||
"Punctuation: '" + match[0] + "'", null,
|
"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",
|
"desc": "Multiple spaces after blockquote symbol",
|
||||||
"tags": [ "blockquote", "whitespace", "indentation" ],
|
"tags": [ "blockquote", "whitespace", "indentation" ],
|
||||||
"func": function MD027(params, onError) {
|
"func": function MD027(params, onError) {
|
||||||
|
var spaceAfterBlockQuote = /^\s*(?:>\s+)+\S/;
|
||||||
var blockquoteNesting = 0;
|
var blockquoteNesting = 0;
|
||||||
var listItemNesting = 0;
|
var listItemNesting = 0;
|
||||||
params.tokens.forEach(function forToken(token) {
|
params.tokens.forEach(function forToken(token) {
|
||||||
|
|
@ -732,15 +519,15 @@ module.exports = [
|
||||||
/^(\s*>)+\s\s+>/.test(token.line) :
|
/^(\s*>)+\s\s+>/.test(token.line) :
|
||||||
/^(\s*>)+\s\s/.test(token.line);
|
/^(\s*>)+\s\s/.test(token.line);
|
||||||
if (multipleSpaces) {
|
if (multipleSpaces) {
|
||||||
addErrorContext(onError, token.lineNumber, token.line, null, null,
|
shared.addErrorContext(onError, token.lineNumber, token.line, null,
|
||||||
rangeFromRegExp(token.line, spaceAfterBlockQuote));
|
null, shared.rangeFromRegExp(token.line, spaceAfterBlockQuote));
|
||||||
}
|
}
|
||||||
token.content.split(shared.newLineRe)
|
token.content.split(shared.newLineRe)
|
||||||
.forEach(function forLine(line, offset) {
|
.forEach(function forLine(line, offset) {
|
||||||
if (/^\s/.test(line)) {
|
if (/^\s/.test(line)) {
|
||||||
addErrorContext(onError, token.lineNumber + offset,
|
shared.addErrorContext(onError, token.lineNumber + offset,
|
||||||
"> " + line, null, null,
|
"> " + line, null, null,
|
||||||
rangeFromRegExp(line, spaceAfterBlockQuote));
|
shared.rangeFromRegExp(line, spaceAfterBlockQuote));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -757,7 +544,7 @@ module.exports = [
|
||||||
params.tokens.forEach(function forToken(token) {
|
params.tokens.forEach(function forToken(token) {
|
||||||
if ((token.type === "blockquote_open") &&
|
if ((token.type === "blockquote_open") &&
|
||||||
(prevToken.type === "blockquote_close")) {
|
(prevToken.type === "blockquote_close")) {
|
||||||
addError(onError, token.lineNumber - 1);
|
shared.addError(onError, token.lineNumber - 1);
|
||||||
}
|
}
|
||||||
prevToken = token;
|
prevToken = token;
|
||||||
});
|
});
|
||||||
|
|
@ -771,7 +558,7 @@ module.exports = [
|
||||||
"func": function MD029(params, onError) {
|
"func": function MD029(params, onError) {
|
||||||
var style = params.config.style || "one_or_ordered";
|
var style = params.config.style || "one_or_ordered";
|
||||||
var numberRe = /^[\s>]*([^.)]*)[.)]/;
|
var numberRe = /^[\s>]*([^.)]*)[.)]/;
|
||||||
flattenLists(params).forEach(function forList(list) {
|
shared.flattenLists(params).forEach(function forList(list) {
|
||||||
if (!list.unordered) {
|
if (!list.unordered) {
|
||||||
var listStyle = style;
|
var listStyle = style;
|
||||||
if (listStyle === "one_or_ordered") {
|
if (listStyle === "one_or_ordered") {
|
||||||
|
|
@ -782,10 +569,10 @@ module.exports = [
|
||||||
var number = 1;
|
var number = 1;
|
||||||
list.items.forEach(function forItem(item) {
|
list.items.forEach(function forItem(item) {
|
||||||
var match = numberRe.exec(item.line);
|
var match = numberRe.exec(item.line);
|
||||||
addErrorDetailIf(onError, item.lineNumber,
|
shared.addErrorDetailIf(onError, item.lineNumber,
|
||||||
String(number), !match || match[1],
|
String(number), !match || match[1],
|
||||||
"Style: " + (listStyle === "one" ? "1/1/1" : "1/2/3"),
|
"Style: " + (listStyle === "one" ? "1/1/1" : "1/2/3"),
|
||||||
rangeFromRegExp(item.line, listItemMarkerRe));
|
shared.rangeFromRegExp(item.line, shared.listItemMarkerRe));
|
||||||
if (listStyle === "ordered") {
|
if (listStyle === "ordered") {
|
||||||
number++;
|
number++;
|
||||||
}
|
}
|
||||||
|
|
@ -804,7 +591,7 @@ module.exports = [
|
||||||
var olSingle = params.config.ol_single || 1;
|
var olSingle = params.config.ol_single || 1;
|
||||||
var ulMulti = params.config.ul_multi || 1;
|
var ulMulti = params.config.ul_multi || 1;
|
||||||
var olMulti = params.config.ol_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 lineCount = list.lastLineIndex - list.open.map[0];
|
||||||
var allSingle = lineCount === list.items.length;
|
var allSingle = lineCount === list.items.length;
|
||||||
var expectedSpaces = list.unordered ?
|
var expectedSpaces = list.unordered ?
|
||||||
|
|
@ -812,9 +599,9 @@ module.exports = [
|
||||||
(allSingle ? olSingle : olMulti);
|
(allSingle ? olSingle : olMulti);
|
||||||
list.items.forEach(function forItem(item) {
|
list.items.forEach(function forItem(item) {
|
||||||
var match = /^[\s>]*\S+(\s+)/.exec(item.line);
|
var match = /^[\s>]*\S+(\s+)/.exec(item.line);
|
||||||
addErrorDetailIf(onError, item.lineNumber,
|
shared.addErrorDetailIf(onError, item.lineNumber,
|
||||||
expectedSpaces, (match ? match[1].length : 0), null,
|
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" ],
|
"tags": [ "code", "blank_lines" ],
|
||||||
"func": function MD031(params, onError) {
|
"func": function MD031(params, onError) {
|
||||||
var lines = params.lines;
|
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) ||
|
if (((onFence > 0) && (i - 1 >= 0) && lines[i - 1].length) ||
|
||||||
((onFence < 0) && (i + 1 < lines.length) && 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",
|
"desc": "Lists should be surrounded by blank lines",
|
||||||
"tags": [ "bullet", "ul", "ol", "blank_lines" ],
|
"tags": [ "bullet", "ul", "ol", "blank_lines" ],
|
||||||
"func": function MD032(params, onError) {
|
"func": function MD032(params, onError) {
|
||||||
|
var listItemMarkerInterruptsRe = /^[\s>]*(?:[*+-]|1\.)\s+/;
|
||||||
var blankOrListRe = /^[\s>]*($|\s)/;
|
var blankOrListRe = /^[\s>]*($|\s)/;
|
||||||
var inList = false;
|
var inList = false;
|
||||||
var prevLine = "";
|
var prevLine = "";
|
||||||
forEachLine(params, function forLine(line, lineIndex, inCode, onFence) {
|
shared.forEachLine(params,
|
||||||
|
function forLine(line, lineIndex, inCode, onFence) {
|
||||||
if (!inCode || onFence) {
|
if (!inCode || onFence) {
|
||||||
var lineTrim = line.trim();
|
var lineTrim = line.trim();
|
||||||
var listMarker = listItemMarkerRe.test(lineTrim);
|
var listMarker = shared.listItemMarkerRe.test(lineTrim);
|
||||||
if (listMarker && !inList && !blankOrListRe.test(prevLine)) {
|
if (listMarker && !inList && !blankOrListRe.test(prevLine)) {
|
||||||
// Check whether this list prefix can interrupt a paragraph
|
// Check whether this list prefix can interrupt a paragraph
|
||||||
if (listItemMarkerInterruptsRe.test(lineTrim)) {
|
if (listItemMarkerInterruptsRe.test(lineTrim)) {
|
||||||
addErrorContext(onError, lineIndex + 1, lineTrim);
|
shared.addErrorContext(onError, lineIndex + 1, lineTrim);
|
||||||
} else {
|
} else {
|
||||||
listMarker = false;
|
listMarker = false;
|
||||||
}
|
}
|
||||||
} else if (!listMarker && inList && !blankOrListRe.test(line)) {
|
} else if (!listMarker && inList && !blankOrListRe.test(line)) {
|
||||||
addErrorContext(onError, lineIndex, lineTrim);
|
shared.addErrorContext(onError, lineIndex, lineTrim);
|
||||||
}
|
}
|
||||||
inList = listMarker;
|
inList = listMarker;
|
||||||
}
|
}
|
||||||
prevLine = line;
|
prevLine = line;
|
||||||
});
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -873,6 +663,7 @@ module.exports = [
|
||||||
.map(function forElement(element) {
|
.map(function forElement(element) {
|
||||||
return element.toLowerCase();
|
return element.toLowerCase();
|
||||||
});
|
});
|
||||||
|
var htmlRe = /<[^>]*>/;
|
||||||
function forToken(token) {
|
function forToken(token) {
|
||||||
token.content.split(shared.newLineRe)
|
token.content.split(shared.newLineRe)
|
||||||
.forEach(function forLine(line, offset) {
|
.forEach(function forLine(line, offset) {
|
||||||
|
|
@ -887,14 +678,14 @@ module.exports = [
|
||||||
return allowedElements.indexOf(element) === -1;
|
return allowedElements.indexOf(element) === -1;
|
||||||
});
|
});
|
||||||
if (allowed.length) {
|
if (allowed.length) {
|
||||||
addError(onError, token.lineNumber + offset,
|
shared.addError(onError, token.lineNumber + offset,
|
||||||
"Element: " + allowed[0], null,
|
"Element: " + allowed[0], null,
|
||||||
rangeFromRegExp(token.line, htmlRe));
|
shared.rangeFromRegExp(token.line, htmlRe));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
filterTokens(params, "html_block", forToken);
|
shared.filterTokens(params, "html_block", forToken);
|
||||||
forEachInlineChild(params, "html_inline", forToken);
|
shared.forEachInlineChild(params, "html_inline", forToken);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -903,7 +694,7 @@ module.exports = [
|
||||||
"desc": "Bare URL used",
|
"desc": "Bare URL used",
|
||||||
"tags": [ "links", "url" ],
|
"tags": [ "links", "url" ],
|
||||||
"func": function MD034(params, onError) {
|
"func": function MD034(params, onError) {
|
||||||
filterTokens(params, "inline", function forToken(token) {
|
shared.filterTokens(params, "inline", function forToken(token) {
|
||||||
var inLink = false;
|
var inLink = false;
|
||||||
token.children.forEach(function forChild(child) {
|
token.children.forEach(function forChild(child) {
|
||||||
var match = null;
|
var match = null;
|
||||||
|
|
@ -913,9 +704,9 @@ module.exports = [
|
||||||
inLink = false;
|
inLink = false;
|
||||||
} else if ((child.type === "text") &&
|
} else if ((child.type === "text") &&
|
||||||
!inLink &&
|
!inLink &&
|
||||||
(match = bareUrlRe.exec(child.content))) {
|
(match = shared.bareUrlRe.exec(child.content))) {
|
||||||
addErrorContext(onError, child.lineNumber, match[0], null, null,
|
shared.addErrorContext(onError, child.lineNumber, match[0], null,
|
||||||
rangeFromRegExp(child.line, bareUrlRe));
|
null, shared.rangeFromRegExp(child.line, shared.bareUrlRe));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
@ -928,12 +719,12 @@ module.exports = [
|
||||||
"tags": [ "hr" ],
|
"tags": [ "hr" ],
|
||||||
"func": function MD035(params, onError) {
|
"func": function MD035(params, onError) {
|
||||||
var style = params.config.style || "consistent";
|
var style = params.config.style || "consistent";
|
||||||
filterTokens(params, "hr", function forToken(token) {
|
shared.filterTokens(params, "hr", function forToken(token) {
|
||||||
var lineTrim = token.line.trim();
|
var lineTrim = token.line.trim();
|
||||||
if (style === "consistent") {
|
if (style === "consistent") {
|
||||||
style = lineTrim;
|
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[0].type === "em_open")) &&
|
||||||
(children[1].type === "text") &&
|
(children[1].type === "text") &&
|
||||||
!re.test(children[1].content)) {
|
!re.test(children[1].content)) {
|
||||||
addErrorContext(onError, t.lineNumber, children[1].content);
|
shared.addErrorContext(onError, t.lineNumber,
|
||||||
|
children[1].content);
|
||||||
}
|
}
|
||||||
return base;
|
return base;
|
||||||
};
|
};
|
||||||
|
|
@ -991,7 +783,7 @@ module.exports = [
|
||||||
"desc": "Spaces inside emphasis markers",
|
"desc": "Spaces inside emphasis markers",
|
||||||
"tags": [ "whitespace", "emphasis" ],
|
"tags": [ "whitespace", "emphasis" ],
|
||||||
"func": function MD037(params, onError) {
|
"func": function MD037(params, onError) {
|
||||||
forEachInlineChild(params, "text", function forToken(token) {
|
shared.forEachInlineChild(params, "text", function forToken(token) {
|
||||||
var left = true;
|
var left = true;
|
||||||
var match = /\s(\*\*?|__?)\s.+\1/.exec(token.content);
|
var match = /\s(\*\*?|__?)\s.+\1/.exec(token.content);
|
||||||
if (!match) {
|
if (!match) {
|
||||||
|
|
@ -1003,7 +795,7 @@ module.exports = [
|
||||||
var line = params.lines[token.lineNumber - 1];
|
var line = params.lines[token.lineNumber - 1];
|
||||||
var column = line.indexOf(text) + 1;
|
var column = line.indexOf(text) + 1;
|
||||||
var length = text.length;
|
var length = text.length;
|
||||||
addErrorContext(onError, token.lineNumber,
|
shared.addErrorContext(onError, token.lineNumber,
|
||||||
text, left, !left, [ column, length ]);
|
text, left, !left, [ column, length ]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -1016,7 +808,7 @@ module.exports = [
|
||||||
"tags": [ "whitespace", "code" ],
|
"tags": [ "whitespace", "code" ],
|
||||||
"func": function MD038(params, onError) {
|
"func": function MD038(params, onError) {
|
||||||
var inlineCodeSpansRe = /(?:^|[^\\])((`+)((?:.*?[^`])|)\2(?!`))/g;
|
var inlineCodeSpansRe = /(?:^|[^\\])((`+)((?:.*?[^`])|)\2(?!`))/g;
|
||||||
forEachInlineChild(params, "code_inline",
|
shared.forEachInlineChild(params, "code_inline",
|
||||||
function forToken(token) {
|
function forToken(token) {
|
||||||
var line = params.lines[token.lineNumber - 1];
|
var line = params.lines[token.lineNumber - 1];
|
||||||
var match = null;
|
var match = null;
|
||||||
|
|
@ -1027,10 +819,10 @@ module.exports = [
|
||||||
var column = match.index + 1 + (match[0].length - length);
|
var column = match.index + 1 + (match[0].length - length);
|
||||||
var range = [ column, length ];
|
var range = [ column, length ];
|
||||||
if (/^\s([^`]|$)/.test(content)) {
|
if (/^\s([^`]|$)/.test(content)) {
|
||||||
addErrorContext(onError, token.lineNumber,
|
shared.addErrorContext(onError, token.lineNumber,
|
||||||
inlineCodeSpan, true, false, range);
|
inlineCodeSpan, true, false, range);
|
||||||
} else if (/[^`]\s$/.test(content)) {
|
} else if (/[^`]\s$/.test(content)) {
|
||||||
addErrorContext(onError, token.lineNumber,
|
shared.addErrorContext(onError, token.lineNumber,
|
||||||
inlineCodeSpan, false, true, range);
|
inlineCodeSpan, false, true, range);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1043,7 +835,8 @@ module.exports = [
|
||||||
"desc": "Spaces inside link text",
|
"desc": "Spaces inside link text",
|
||||||
"tags": [ "whitespace", "links" ],
|
"tags": [ "whitespace", "links" ],
|
||||||
"func": function MD039(params, onError) {
|
"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 inLink = false;
|
||||||
var linkText = "";
|
var linkText = "";
|
||||||
token.children.forEach(function forChild(child) {
|
token.children.forEach(function forChild(child) {
|
||||||
|
|
@ -1055,9 +848,9 @@ module.exports = [
|
||||||
var left = shared.trimLeft(linkText).length !== linkText.length;
|
var left = shared.trimLeft(linkText).length !== linkText.length;
|
||||||
var right = shared.trimRight(linkText).length !== linkText.length;
|
var right = shared.trimRight(linkText).length !== linkText.length;
|
||||||
if (left || right) {
|
if (left || right) {
|
||||||
addErrorContext(onError, token.lineNumber,
|
shared.addErrorContext(onError, token.lineNumber,
|
||||||
"[" + linkText + "]", left, right,
|
"[" + linkText + "]", left, right,
|
||||||
rangeFromRegExp(token.line, spaceInsideLinkRe));
|
shared.rangeFromRegExp(token.line, spaceInLinkRe));
|
||||||
}
|
}
|
||||||
} else if (inLink) {
|
} else if (inLink) {
|
||||||
linkText += child.content;
|
linkText += child.content;
|
||||||
|
|
@ -1072,9 +865,9 @@ module.exports = [
|
||||||
"desc": "Fenced code blocks should have a language specified",
|
"desc": "Fenced code blocks should have a language specified",
|
||||||
"tags": [ "code", "language" ],
|
"tags": [ "code", "language" ],
|
||||||
"func": function MD040(params, onError) {
|
"func": function MD040(params, onError) {
|
||||||
filterTokens(params, "fence", function forToken(token) {
|
shared.filterTokens(params, "fence", function forToken(token) {
|
||||||
if (!token.info.trim()) {
|
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.type === "heading_open") {
|
||||||
if (!((token.lineNumber === 1) || (index > 0)) ||
|
if (!((token.lineNumber === 1) || (index > 0)) ||
|
||||||
(token.tag !== tag)) {
|
(token.tag !== tag)) {
|
||||||
addErrorContext(onError, token.lineNumber, token.line);
|
shared.addErrorContext(onError, token.lineNumber, token.line);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
} else if (token.type === "html_block") {
|
} else if (token.type === "html_block") {
|
||||||
|
|
@ -1104,7 +897,7 @@ module.exports = [
|
||||||
!params.frontMatterLines.some(function forLine(line) {
|
!params.frontMatterLines.some(function forLine(line) {
|
||||||
return frontMatterTitleRe.test(line);
|
return frontMatterTitleRe.test(line);
|
||||||
})) {
|
})) {
|
||||||
addErrorContext(onError, token.lineNumber, token.line);
|
shared.addErrorContext(onError, token.lineNumber, token.line);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
|
@ -1116,7 +909,8 @@ module.exports = [
|
||||||
"desc": "No empty links",
|
"desc": "No empty links",
|
||||||
"tags": [ "links" ],
|
"tags": [ "links" ],
|
||||||
"func": function MD042(params, onError) {
|
"func": function MD042(params, onError) {
|
||||||
filterTokens(params, "inline", function forToken(token) {
|
var emptyLinkRe = /\[[^\]]*](?:\((?:#?|(?:<>))\))/;
|
||||||
|
shared.filterTokens(params, "inline", function forToken(token) {
|
||||||
var inLink = false;
|
var inLink = false;
|
||||||
var linkText = "";
|
var linkText = "";
|
||||||
var emptyLink = false;
|
var emptyLink = false;
|
||||||
|
|
@ -1132,9 +926,9 @@ module.exports = [
|
||||||
} else if (child.type === "link_close") {
|
} else if (child.type === "link_close") {
|
||||||
inLink = false;
|
inLink = false;
|
||||||
if (emptyLink) {
|
if (emptyLink) {
|
||||||
addErrorContext(onError, child.lineNumber,
|
shared.addErrorContext(onError, child.lineNumber,
|
||||||
"[" + linkText + "]()", null, null,
|
"[" + linkText + "]()", null, null,
|
||||||
rangeFromRegExp(child.line, emptyLinkRe));
|
shared.rangeFromRegExp(child.line, emptyLinkRe));
|
||||||
}
|
}
|
||||||
} else if (inLink) {
|
} else if (inLink) {
|
||||||
linkText += child.content;
|
linkText += child.content;
|
||||||
|
|
@ -1158,7 +952,7 @@ module.exports = [
|
||||||
var i = 0;
|
var i = 0;
|
||||||
var optional = false;
|
var optional = false;
|
||||||
var errorCount = 0;
|
var errorCount = 0;
|
||||||
forEachHeading(params, function forHeading(heading, content) {
|
shared.forEachHeading(params, function forHeading(heading, content) {
|
||||||
if (!errorCount) {
|
if (!errorCount) {
|
||||||
var actual = levels[heading.tag] + " " + content;
|
var actual = levels[heading.tag] + " " + content;
|
||||||
var expected = requiredHeaders[i++] || "[None]";
|
var expected = requiredHeaders[i++] || "[None]";
|
||||||
|
|
@ -1169,13 +963,15 @@ module.exports = [
|
||||||
} else if (optional) {
|
} else if (optional) {
|
||||||
i--;
|
i--;
|
||||||
} else {
|
} else {
|
||||||
addErrorDetailIf(onError, heading.lineNumber, expected, actual);
|
shared.addErrorDetailIf(onError, heading.lineNumber,
|
||||||
|
expected, actual);
|
||||||
errorCount++;
|
errorCount++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if ((i < requiredHeaders.length) && !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 codeBlocks = params.config.code_blocks;
|
||||||
var includeCodeBlocks = (codeBlocks === undefined) ? true : !!codeBlocks;
|
var includeCodeBlocks = (codeBlocks === undefined) ? true : !!codeBlocks;
|
||||||
names.forEach(function forName(name) {
|
names.forEach(function forName(name) {
|
||||||
var escapedName = escapeForRegExp(name);
|
var escapedName = shared.escapeForRegExp(name);
|
||||||
var namePattern = "\\S*\\b(" + escapedName + ")\\b\\S*";
|
var namePattern = "\\S*\\b(" + escapedName + ")\\b\\S*";
|
||||||
var anyNameRe = new RegExp(namePattern, "gi");
|
var anyNameRe = new RegExp(namePattern, "gi");
|
||||||
function forToken(token) {
|
function forToken(token) {
|
||||||
|
|
@ -1200,24 +996,24 @@ module.exports = [
|
||||||
var match = null;
|
var match = null;
|
||||||
while ((match = anyNameRe.exec(line)) !== null) {
|
while ((match = anyNameRe.exec(line)) !== null) {
|
||||||
var fullMatch = match[0];
|
var fullMatch = match[0];
|
||||||
if (!bareUrlRe.test(fullMatch)) {
|
if (!shared.bareUrlRe.test(fullMatch)) {
|
||||||
var wordMatch = fullMatch
|
var wordMatch = fullMatch
|
||||||
.replace(/^\W*/, "").replace(/\W*$/, "");
|
.replace(/^\W*/, "").replace(/\W*$/, "");
|
||||||
if (names.indexOf(wordMatch) === -1) {
|
if (names.indexOf(wordMatch) === -1) {
|
||||||
var lineNumber = token.lineNumber + index + fenceOffset;
|
var lineNumber = token.lineNumber + index + fenceOffset;
|
||||||
var range = [ match.index + 1, wordMatch.length ];
|
var range = [ match.index + 1, wordMatch.length ];
|
||||||
addErrorDetailIf(onError, lineNumber,
|
shared.addErrorDetailIf(onError, lineNumber,
|
||||||
name, match[1], null, range);
|
name, match[1], null, range);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
forEachInlineChild(params, "text", forToken);
|
shared.forEachInlineChild(params, "text", forToken);
|
||||||
if (includeCodeBlocks) {
|
if (includeCodeBlocks) {
|
||||||
forEachInlineChild(params, "code_inline", forToken);
|
shared.forEachInlineChild(params, "code_inline", forToken);
|
||||||
filterTokens(params, "code_block", forToken);
|
shared.filterTokens(params, "code_block", forToken);
|
||||||
filterTokens(params, "fence", forToken);
|
shared.filterTokens(params, "fence", forToken);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -1227,9 +1023,9 @@ module.exports = [
|
||||||
"desc": "Images should have alternate text (alt text)",
|
"desc": "Images should have alternate text (alt text)",
|
||||||
"tags": [ "accessibility", "images" ],
|
"tags": [ "accessibility", "images" ],
|
||||||
"func": function MD045(params, onError) {
|
"func": function MD045(params, onError) {
|
||||||
forEachInlineChild(params, "image", function forToken(token) {
|
shared.forEachInlineChild(params, "image", function forToken(token) {
|
||||||
if (token.content === "") {
|
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;
|
/<!--\s*markdownlint-(dis|en)able((?:\s+[a-z0-9_-]+)*)\s*-->/ig;
|
||||||
module.exports.inlineCommentRe = inlineCommentRe;
|
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
|
// readFile options for reading with the UTF-8 encoding
|
||||||
module.exports.utf8Encoding = { "encoding": "utf8" };
|
module.exports.utf8Encoding = { "encoding": "utf8" };
|
||||||
|
|
||||||
|
|
@ -23,10 +28,9 @@ function trimLeft(str) {
|
||||||
module.exports.trimLeft = trimLeft;
|
module.exports.trimLeft = trimLeft;
|
||||||
|
|
||||||
// Trims whitespace from the right (end) of a string
|
// Trims whitespace from the right (end) of a string
|
||||||
function trimRight(str) {
|
module.exports.trimRight = function trimRight(str) {
|
||||||
return str.replace(/\s*$/, "");
|
return str.replace(/\s*$/, "");
|
||||||
}
|
};
|
||||||
module.exports.trimRight = trimRight;
|
|
||||||
|
|
||||||
// Applies key/value pairs from src to dst, returning dst
|
// Applies key/value pairs from src to dst, returning dst
|
||||||
function assign(dst, src) {
|
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
|
// See https://www.w3.org/TR/html5/syntax.html#comments for details
|
||||||
var htmlCommentBegin = "<!--";
|
var htmlCommentBegin = "<!--";
|
||||||
var htmlCommentEnd = "-->";
|
var htmlCommentEnd = "-->";
|
||||||
function clearHtmlCommentText(text) {
|
module.exports.clearHtmlCommentText = function clearHtmlCommentText(text) {
|
||||||
var i = 0;
|
var i = 0;
|
||||||
while ((i = text.indexOf(htmlCommentBegin, i)) !== -1) {
|
while ((i = text.indexOf(htmlCommentBegin, i)) !== -1) {
|
||||||
var j = text.indexOf(htmlCommentEnd, i);
|
var j = text.indexOf(htmlCommentEnd, i);
|
||||||
|
|
@ -72,5 +76,216 @@ function clearHtmlCommentText(text) {
|
||||||
i = j + htmlCommentEnd.length;
|
i = j + htmlCommentEnd.length;
|
||||||
}
|
}
|
||||||
return text;
|
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";
|
||||||
}
|
}
|
||||||
module.exports.clearHtmlCommentText = clearHtmlCommentText;
|
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.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