mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 22:10:13 +01:00
Refactor rules for clarity/brevity.
This commit is contained in:
parent
c864ac1b96
commit
5d641bbf24
1 changed files with 55 additions and 59 deletions
64
lib/rules.js
64
lib/rules.js
|
|
@ -1,10 +1,10 @@
|
|||
"use strict";
|
||||
|
||||
function indentFrom(token) {
|
||||
function indentFor(token) {
|
||||
return token.line.length - token.line.trimLeft().length;
|
||||
}
|
||||
|
||||
function headingStyleFrom(token) {
|
||||
function headingStyleFor(token) {
|
||||
if ((token.lines[1] - token.lines[0]) === 1) {
|
||||
if (token.line.match(/#\s*$/)) {
|
||||
return "atx_closed";
|
||||
|
|
@ -14,7 +14,7 @@ function headingStyleFrom(token) {
|
|||
return "setext";
|
||||
}
|
||||
|
||||
function unorderedListStyleFrom(token) {
|
||||
function unorderedListStyleFor(token) {
|
||||
switch (token.line.trimLeft().substr(0, 1)) {
|
||||
case "*":
|
||||
return "asterisk";
|
||||
|
|
@ -27,6 +27,12 @@ function unorderedListStyleFrom(token) {
|
|||
}
|
||||
}
|
||||
|
||||
function filterTokens(tokens, tokenA, tokenB) {
|
||||
return tokens.filter(function filterToken(token) {
|
||||
return ((token.type === tokenA) || (token.type === tokenB));
|
||||
});
|
||||
}
|
||||
|
||||
function padAndTrim(lines) {
|
||||
return [].concat(
|
||||
"",
|
||||
|
|
@ -42,9 +48,8 @@ module.exports = [
|
|||
"desc": "Header levels should only increment by one level at a time",
|
||||
"func": function MD001(params, errors) {
|
||||
var prevLevel = 0;
|
||||
params.tokens.filter(function filterToken(token) {
|
||||
return (token.type === "heading_open");
|
||||
}).forEach(function forToken(token) {
|
||||
filterTokens(params.tokens, "heading_open")
|
||||
.forEach(function forToken(token) {
|
||||
if (prevLevel && (token.hLevel > prevLevel + 1)) {
|
||||
errors.push(token.lineNumber);
|
||||
}
|
||||
|
|
@ -74,14 +79,12 @@ module.exports = [
|
|||
"desc": "Header style",
|
||||
"func": function MD003(params, errors) {
|
||||
var style = params.options.style || "consistent";
|
||||
var headings = params.tokens.filter(function filterToken(token) {
|
||||
return (token.type === "heading_open");
|
||||
});
|
||||
var headings = filterTokens(params.tokens, "heading_open");
|
||||
if ((style === "consistent") && headings.length) {
|
||||
style = headingStyleFrom(headings[0]);
|
||||
style = headingStyleFor(headings[0]);
|
||||
}
|
||||
headings.forEach(function forToken(token) {
|
||||
if (headingStyleFrom(token) !== style) {
|
||||
if (headingStyleFor(token) !== style) {
|
||||
errors.push(token.lineNumber);
|
||||
}
|
||||
});
|
||||
|
|
@ -93,14 +96,12 @@ module.exports = [
|
|||
"desc": "Unordered list style",
|
||||
"func": function MD004(params, errors) {
|
||||
var style = params.options.style || "consistent";
|
||||
var listItems = params.tokens.filter(function filterToken(token) {
|
||||
return (token.type === "list_item_open");
|
||||
});
|
||||
var listItems = filterTokens(params.tokens, "list_item_open");
|
||||
if ((style === "consistent") && listItems.length) {
|
||||
style = unorderedListStyleFrom(listItems[0]);
|
||||
style = unorderedListStyleFor(listItems[0]);
|
||||
}
|
||||
listItems.forEach(function forToken(token) {
|
||||
if (unorderedListStyleFrom(token) !== style) {
|
||||
if (unorderedListStyleFor(token) !== style) {
|
||||
errors.push(token.lineNumber);
|
||||
}
|
||||
});
|
||||
|
|
@ -112,10 +113,9 @@ module.exports = [
|
|||
"desc": "Inconsistent indentation for list items at the same level",
|
||||
"func": function MD005(params, errors) {
|
||||
var indentLevels = [];
|
||||
params.tokens.filter(function filterToken(token) {
|
||||
return (token.type === "list_item_open");
|
||||
}).forEach(function forToken(token) {
|
||||
var indentLevel = indentFrom(token);
|
||||
filterTokens(params.tokens, "list_item_open")
|
||||
.forEach(function forToken(token) {
|
||||
var indentLevel = indentFor(token);
|
||||
if (!indentLevels[token.level]) {
|
||||
indentLevels[token.level] = indentLevel;
|
||||
} else if (indentLevel !== indentLevels[token.level]) {
|
||||
|
|
@ -142,7 +142,7 @@ module.exports = [
|
|||
return false;
|
||||
}
|
||||
}).forEach(function forToken(token) {
|
||||
if (indentFrom(token) !== 0) {
|
||||
if (indentFor(token) !== 0) {
|
||||
errors.push(token.lineNumber);
|
||||
}
|
||||
});
|
||||
|
|
@ -155,10 +155,9 @@ module.exports = [
|
|||
"func": function MD007(params, errors) {
|
||||
var optionsIndent = params.options.indent || 2;
|
||||
var prevIndent = 0;
|
||||
params.tokens.filter(function filterToken(token) {
|
||||
return (token.type === "bullet_list_open");
|
||||
}).forEach(function forToken(token) {
|
||||
var indent = indentFrom(token);
|
||||
filterTokens(params.tokens, "bullet_list_open")
|
||||
.forEach(function forToken(token) {
|
||||
var indent = indentFor(token);
|
||||
if ((indent > prevIndent) &&
|
||||
((indent - prevIndent) !== optionsIndent)) {
|
||||
errors.push(token.lineNumber);
|
||||
|
|
@ -198,12 +197,10 @@ module.exports = [
|
|||
"name": "MD011",
|
||||
"desc": "Reversed link syntax",
|
||||
"func": function MD011(params, errors) {
|
||||
params.tokens.filter(function filterToken(token) {
|
||||
return (token.type === "inline");
|
||||
}).forEach(function forToken(token) {
|
||||
token.children.filter(function filterChild(child) {
|
||||
return (child.type === "text");
|
||||
}).forEach(function forChild(child) {
|
||||
filterTokens(params.tokens, "inline")
|
||||
.forEach(function forToken(token) {
|
||||
filterTokens(token.children, "text")
|
||||
.forEach(function forChild(child) {
|
||||
if (child.content.match(/\([^)]+\)\[[^\]]+\]/)) {
|
||||
errors.push(token.lineNumber);
|
||||
}
|
||||
|
|
@ -217,9 +214,8 @@ module.exports = [
|
|||
"desc": "Multiple consecutive blank lines",
|
||||
"func": function MD012(params, errors) {
|
||||
var exclusions = [];
|
||||
params.tokens.filter(function filterToken(token) {
|
||||
return ((token.type === "code_block") || (token.type === "fence"));
|
||||
}).forEach(function forToken(token) {
|
||||
filterTokens(params.tokens, "code_block", "fence")
|
||||
.forEach(function forToken(token) {
|
||||
for (var i = token.lines[0] ; i < token.lines[1] ; i++) {
|
||||
exclusions.push(i);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue