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
530
lib/rules.js
530
lib/rules.js
File diff suppressed because it is too large
Load diff
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";
|
||||||
|
}
|
||||||
|
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