mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +01:00
Convert filterTokens from array to callback for ~7% time reduction.
This commit is contained in:
parent
9bf0b1a554
commit
17763331a6
1 changed files with 102 additions and 113 deletions
215
lib/rules.js
215
lib/rules.js
|
|
@ -31,10 +31,12 @@ function unorderedListStyleFor(token) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filters a list of tokens by type
|
// Calls the provided function for each matching token
|
||||||
function filterTokens(tokens, typeA, typeB) {
|
function filterTokens(tokens, type, callback) {
|
||||||
return tokens.filter(function filterToken(token) {
|
tokens.forEach(function forToken(token) {
|
||||||
return ((token.type === typeA) || (token.type === typeB));
|
if (token.type === type) {
|
||||||
|
callback(token);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -42,12 +44,11 @@ function filterTokens(tokens, typeA, typeB) {
|
||||||
function forEachLine(params, callback) {
|
function forEachLine(params, callback) {
|
||||||
// Identify lines in code blocks
|
// Identify lines in code blocks
|
||||||
var codeLines = [];
|
var codeLines = [];
|
||||||
filterTokens(params.tokens, "code_block")
|
filterTokens(params.tokens, "code_block", function forToken(token) {
|
||||||
.forEach(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++) {
|
codeLines.push(i);
|
||||||
codeLines.push(i);
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
// Identify lines in code fences (with info about transitions)
|
// Identify lines in code fences (with info about transitions)
|
||||||
var inFence = false;
|
var inFence = false;
|
||||||
params.lines.forEach(function forLine(line, lineIndex) {
|
params.lines.forEach(function forLine(line, lineIndex) {
|
||||||
|
|
@ -62,13 +63,11 @@ function forEachLine(params, callback) {
|
||||||
|
|
||||||
// Calls the provided function for each specified inline child token
|
// Calls the provided function for each specified inline child token
|
||||||
function forEachInlineChild(params, type, callback) {
|
function forEachInlineChild(params, type, callback) {
|
||||||
filterTokens(params.tokens, "inline")
|
filterTokens(params.tokens, "inline", function forToken(token) {
|
||||||
.forEach(function forToken(token) {
|
filterTokens(token.children, type, function forChild(child) {
|
||||||
filterTokens(token.children, type)
|
callback(child, token);
|
||||||
.forEach(function forChild(child) {
|
|
||||||
callback(child, token);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calls the provided function for each heading's content
|
// Calls the provided function for each heading's content
|
||||||
|
|
@ -131,14 +130,13 @@ module.exports = [
|
||||||
"tags": [ "headers" ],
|
"tags": [ "headers" ],
|
||||||
"func": function MD001(params, errors) {
|
"func": function MD001(params, errors) {
|
||||||
var prevLevel = 0;
|
var prevLevel = 0;
|
||||||
filterTokens(params.tokens, "heading_open")
|
filterTokens(params.tokens, "heading_open", function forToken(token) {
|
||||||
.forEach(function forToken(token) {
|
var level = parseInt(token.tag.slice(1), 10);
|
||||||
var level = parseInt(token.tag.slice(1), 10);
|
if (prevLevel && (level > prevLevel + 1)) {
|
||||||
if (prevLevel && (level > prevLevel + 1)) {
|
errors.push(token.lineNumber);
|
||||||
errors.push(token.lineNumber);
|
}
|
||||||
}
|
prevLevel = level;
|
||||||
prevLevel = level;
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -165,11 +163,10 @@ module.exports = [
|
||||||
"tags": [ "headers" ],
|
"tags": [ "headers" ],
|
||||||
"func": function MD003(params, errors) {
|
"func": function MD003(params, errors) {
|
||||||
var style = params.options.style || "consistent";
|
var style = params.options.style || "consistent";
|
||||||
var headings = filterTokens(params.tokens, "heading_open");
|
filterTokens(params.tokens, "heading_open", function forToken(token) {
|
||||||
if ((style === "consistent") && headings.length) {
|
if (style === "consistent") {
|
||||||
style = headingStyleFor(headings[0]);
|
style = headingStyleFor(token);
|
||||||
}
|
}
|
||||||
headings.forEach(function forToken(token) {
|
|
||||||
if (headingStyleFor(token) !== style) {
|
if (headingStyleFor(token) !== style) {
|
||||||
errors.push(token.lineNumber);
|
errors.push(token.lineNumber);
|
||||||
}
|
}
|
||||||
|
|
@ -321,8 +318,8 @@ 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, errors) {
|
"func": function MD014(params, errors) {
|
||||||
filterTokens(params.tokens, "code_block", "fence")
|
[ "code_block", "fence" ].forEach(function forType(type) {
|
||||||
.forEach(function forToken(token) {
|
filterTokens(params.tokens, type, function forToken(token) {
|
||||||
if (token.content && token.content.split(shared.newLineRe)
|
if (token.content && token.content.split(shared.newLineRe)
|
||||||
.filter(function filterLine(line) {
|
.filter(function filterLine(line) {
|
||||||
return line;
|
return line;
|
||||||
|
|
@ -332,6 +329,7 @@ module.exports = [
|
||||||
errors.push(token.lineNumber);
|
errors.push(token.lineNumber);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -353,13 +351,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, errors) {
|
"func": function MD019(params, errors) {
|
||||||
filterTokens(params.tokens, "heading_open")
|
filterTokens(params.tokens, "heading_open", function forToken(token) {
|
||||||
.forEach(function forToken(token) {
|
if ((headingStyleFor(token) === "atx") &&
|
||||||
if ((headingStyleFor(token) === "atx") &&
|
/^#+\s\s/.test(token.line)) {
|
||||||
/^#+\s\s/.test(token.line)) {
|
errors.push(token.lineNumber);
|
||||||
errors.push(token.lineNumber);
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -382,13 +379,12 @@ 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, errors) {
|
"func": function MD021(params, errors) {
|
||||||
filterTokens(params.tokens, "heading_open")
|
filterTokens(params.tokens, "heading_open", function forToken(token) {
|
||||||
.forEach(function forToken(token) {
|
if ((headingStyleFor(token) === "atx_closed") &&
|
||||||
if ((headingStyleFor(token) === "atx_closed") &&
|
(/^#+\s\s/.test(token.line) || /\s\s#+$/.test(token.line))) {
|
||||||
(/^#+\s\s/.test(token.line) || /\s\s#+$/.test(token.line))) {
|
errors.push(token.lineNumber);
|
||||||
errors.push(token.lineNumber);
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -434,12 +430,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, errors) {
|
"func": function MD023(params, errors) {
|
||||||
filterTokens(params.tokens, "heading_open")
|
filterTokens(params.tokens, "heading_open", function forToken(token) {
|
||||||
.forEach(function forToken(token) {
|
if (/^\s/.test(token.line)) {
|
||||||
if (/^\s/.test(token.line)) {
|
errors.push(token.lineNumber);
|
||||||
errors.push(token.lineNumber);
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -465,16 +460,15 @@ module.exports = [
|
||||||
"tags": [ "headers" ],
|
"tags": [ "headers" ],
|
||||||
"func": function MD025(params, errors) {
|
"func": function MD025(params, errors) {
|
||||||
var hasTopLevelHeading = false;
|
var hasTopLevelHeading = false;
|
||||||
filterTokens(params.tokens, "heading_open")
|
filterTokens(params.tokens, "heading_open", function forToken(token) {
|
||||||
.forEach(function forToken(token) {
|
if (token.tag === "h1") {
|
||||||
if (token.tag === "h1") {
|
if (hasTopLevelHeading) {
|
||||||
if (hasTopLevelHeading) {
|
errors.push(token.lineNumber);
|
||||||
errors.push(token.lineNumber);
|
} else if (token.lineNumber === 1) {
|
||||||
} else if (token.lineNumber === 1) {
|
hasTopLevelHeading = true;
|
||||||
hasTopLevelHeading = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -624,10 +618,9 @@ module.exports = [
|
||||||
"desc": "Inline HTML",
|
"desc": "Inline HTML",
|
||||||
"tags": [ "html" ],
|
"tags": [ "html" ],
|
||||||
"func": function MD033(params, errors) {
|
"func": function MD033(params, errors) {
|
||||||
filterTokens(params.tokens, "html_block")
|
filterTokens(params.tokens, "html_block", function forToken(token) {
|
||||||
.forEach(function forToken(token) {
|
errors.push(token.lineNumber);
|
||||||
errors.push(token.lineNumber);
|
});
|
||||||
});
|
|
||||||
forEachInlineChild(params, "html_inline", function forToken(token) {
|
forEachInlineChild(params, "html_inline", function forToken(token) {
|
||||||
errors.push(token.lineNumber);
|
errors.push(token.lineNumber);
|
||||||
});
|
});
|
||||||
|
|
@ -639,21 +632,20 @@ module.exports = [
|
||||||
"desc": "Bare URL used",
|
"desc": "Bare URL used",
|
||||||
"tags": [ "links", "url" ],
|
"tags": [ "links", "url" ],
|
||||||
"func": function MD034(params, errors) {
|
"func": function MD034(params, errors) {
|
||||||
filterTokens(params.tokens, "inline")
|
filterTokens(params.tokens, "inline", function forToken(token) {
|
||||||
.forEach(function forToken(token) {
|
var inLink = false;
|
||||||
var inLink = false;
|
token.children.forEach(function forChild(child) {
|
||||||
token.children.forEach(function forChild(child) {
|
if (child.type === "link_open") {
|
||||||
if (child.type === "link_open") {
|
inLink = true;
|
||||||
inLink = true;
|
} else if (child.type === "link_close") {
|
||||||
} else if (child.type === "link_close") {
|
inLink = false;
|
||||||
inLink = false;
|
} else if ((child.type === "text") &&
|
||||||
} else if ((child.type === "text") &&
|
!inLink &&
|
||||||
!inLink &&
|
/https?:\/\//.test(child.content)) {
|
||||||
/https?:\/\//.test(child.content)) {
|
errors.push(child.lineNumber);
|
||||||
errors.push(child.lineNumber);
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -663,11 +655,10 @@ module.exports = [
|
||||||
"tags": [ "hr" ],
|
"tags": [ "hr" ],
|
||||||
"func": function MD035(params, errors) {
|
"func": function MD035(params, errors) {
|
||||||
var style = params.options.style || "consistent";
|
var style = params.options.style || "consistent";
|
||||||
var horizontalRules = filterTokens(params.tokens, "hr");
|
filterTokens(params.tokens, "hr", function forToken(token) {
|
||||||
if ((style === "consistent") && horizontalRules.length) {
|
if (style === "consistent") {
|
||||||
style = horizontalRules[0].line;
|
style = token.line;
|
||||||
}
|
}
|
||||||
horizontalRules.forEach(function forToken(token) {
|
|
||||||
if (token.line !== style) {
|
if (token.line !== style) {
|
||||||
errors.push(token.lineNumber);
|
errors.push(token.lineNumber);
|
||||||
}
|
}
|
||||||
|
|
@ -739,32 +730,31 @@ module.exports = [
|
||||||
"desc": "Spaces inside link text",
|
"desc": "Spaces inside link text",
|
||||||
"tags": [ "whitespace", "links" ],
|
"tags": [ "whitespace", "links" ],
|
||||||
"func": function MD039(params, errors) {
|
"func": function MD039(params, errors) {
|
||||||
filterTokens(params.tokens, "inline")
|
filterTokens(params.tokens, "inline", function forToken(token) {
|
||||||
.forEach(function forToken(token) {
|
var inLink = false;
|
||||||
var inLink = false;
|
var index = 0;
|
||||||
var index = 0;
|
var lastChildRightSpaceLineNumber = 0;
|
||||||
var lastChildRightSpaceLineNumber = 0;
|
token.children.forEach(function forChild(child) {
|
||||||
token.children.forEach(function forChild(child) {
|
if (child.type === "link_open") {
|
||||||
if (child.type === "link_open") {
|
inLink = true;
|
||||||
inLink = true;
|
index = 0;
|
||||||
index = 0;
|
} else if (child.type === "link_close") {
|
||||||
} else if (child.type === "link_close") {
|
inLink = false;
|
||||||
inLink = false;
|
if (lastChildRightSpaceLineNumber) {
|
||||||
if (lastChildRightSpaceLineNumber) {
|
errors.push(lastChildRightSpaceLineNumber);
|
||||||
errors.push(lastChildRightSpaceLineNumber);
|
|
||||||
}
|
|
||||||
} else if (inLink) {
|
|
||||||
if ((index === 0) &&
|
|
||||||
(child.content.trimLeft().length !== child.content.length)) {
|
|
||||||
errors.push(child.lineNumber);
|
|
||||||
}
|
|
||||||
lastChildRightSpaceLineNumber =
|
|
||||||
(child.content.trimRight().length !== child.content.length) ?
|
|
||||||
child.lineNumber : 0;
|
|
||||||
index++;
|
|
||||||
}
|
}
|
||||||
});
|
} else if (inLink) {
|
||||||
|
if ((index === 0) &&
|
||||||
|
(child.content.trimLeft().length !== child.content.length)) {
|
||||||
|
errors.push(child.lineNumber);
|
||||||
|
}
|
||||||
|
lastChildRightSpaceLineNumber =
|
||||||
|
(child.content.trimRight().length !== child.content.length) ?
|
||||||
|
child.lineNumber : 0;
|
||||||
|
index++;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -773,12 +763,11 @@ 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, errors) {
|
"func": function MD040(params, errors) {
|
||||||
filterTokens(params.tokens, "fence")
|
filterTokens(params.tokens, "fence", function forToken(token) {
|
||||||
.forEach(function forToken(token) {
|
if (!token.info.trim()) {
|
||||||
if (!token.info.trim()) {
|
errors.push(token.lineNumber);
|
||||||
errors.push(token.lineNumber);
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue