Add options.resultVersion for more detailed error reporting (fixes #23).

This commit is contained in:
David Anson 2016-10-16 21:46:02 -07:00
parent 3a356467cd
commit 0ca8bc7bb6
11 changed files with 454 additions and 160 deletions

View file

@ -174,8 +174,8 @@
"max-len": "error",
"max-lines": "off",
"max-nested-callbacks": "error",
"max-params": ["error", 5],
"max-statements": ["error", 21],
"max-params": ["error", 6],
"max-statements": ["error", 26],
"max-statements-per-line": "error",
"new-cap": "error",
"new-parens": "error",

View file

@ -275,6 +275,20 @@ See the [style](style) directory for more samples.
See [markdownlint-config-schema.json](schema/markdownlint-config-schema.json)
for the [JSON Schema](http://json-schema.org/) of the `options.config` object.
#### options.resultVersion
Type: `Number`
Specifies which version of the `result` object to return (see the "Usage" section
below for examples).
Passing a `resultVersion` of `0` corresponds to the original, simple format where
each error is identified by rule name and line number. This is the default.
Passing a `resultVersion` of `1` corresponds to a more detailed format where each
error includes information about the line number, rule name, alias, description,
as well as any additional detail or context that is available.
### callback
Type: `Function` taking (`Error`, `Object`)
@ -322,7 +336,8 @@ bad.md: 1: MD018 No space after hash on atx style header
bad.md: 3: MD018 No space after hash on atx style header
```
Or invoke `markdownlint.sync` for a synchronous call:
Or invoke `markdownlint.sync` for a synchronous call and/or pass `true` to `toString`
to use rule aliases instead of names:
```js
var result = markdownlint.sync(options);
@ -354,19 +369,62 @@ Output:
```json
{
"good.string": {},
"bad.string": {
"MD010": [ 3 ],
"MD018": [ 1, 3 ]
},
"good.md": {},
"bad.md": {
"MD010": [ 3 ],
"MD018": [ 1, 3 ]
MD010: [ 3 ],
MD018: [ 1, 3 ]
}
}
```
For more detailed error reporting, set `options.resultVersion` to `1`:
```js
var options = {
"files": [ "good.md", "bad.md" ],
"resultVersion": 1
};
```
With that, the output of `result.toString` looks like:
```text
bad.string: 3: MD010/no-hard-tabs Hard tabs [Column: 19]
bad.string: 1: MD018/no-missing-space-atx No space after hash on atx style header [Context: "#bad.string"]
bad.string: 3: MD018/no-missing-space-atx No space after hash on atx style header [Context: "#This string fails some rules."]
bad.md: 3: MD010/no-hard-tabs Hard tabs [Column: 17]
bad.md: 1: MD018/no-missing-space-atx No space after hash on atx style header [Context: "#bad.md"]
bad.md: 3: MD018/no-missing-space-atx No space after hash on atx style header [Context: "#This file fails some rules."]
```
And the `result` object becomes:
```json
{
"good.md": [],
"bad.md": [
{ lineNumber: 3,
ruleName: "MD010",
ruleAlias: "no-hard-tabs",
ruleDescription: "Hard tabs",
errorDetail: "Column: 17",
errorContext: null },
{ lineNumber: 1,
ruleName: "MD018",
ruleAlias: "no-missing-space-atx",
ruleDescription: "No space after hash on atx style header",
errorDetail: null,
errorContext: "#bad.md" },
{ lineNumber: 3,
ruleName: "MD018",
ruleAlias: "no-missing-space-atx",
ruleDescription: "No space after hash on atx style header",
errorDetail: null,
errorContext: "#This file fails\tsome rules." }
]
}
```
Integration with the [gulp](http://gulpjs.com/) build system is straightforward:
```js

View file

@ -42,6 +42,11 @@ textarea {
padding: 0;
resize: none;
}
.detail {
font-size: 80%;
font-style: italic;
white-space: pre-wrap;
}
.flex-rows {
display: flex;
flex-direction: column;

View file

@ -15,6 +15,14 @@
var rulesMd = "https://github.com/DavidAnson/markdownlint" +
"/blob/master/doc/Rules.md";
// Sanitize string for HTML display
function sanitize(str) {
return str
.replace(/&/g, "&")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;");
}
// Handle input
function onMarkdownInput() {
// Markdown
@ -26,10 +34,7 @@
var padding = lines.length.toString().replace(/\d/g, " ");
numbered.innerHTML = lines
.map(function mapNumberedLine(line, index) {
line = line
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;");
line = sanitize(line);
index++;
var paddedIndex = (padding + index).slice(-padding.length);
return "<span id='l" + index + "'><em>" + paddedIndex + "</em>: " +
@ -42,19 +47,27 @@
},
"config": {
"MD013": false
}
},
"resultVersion": 1
};
var results = window.markdownlint.sync(options).toString();
violations.innerHTML = results.split(newLineRe)
.map(function mapResultLine(line) {
return line.replace(/^content: (\d+): (MD\d\d\d) (.*)$/,
function replacer(match, p1, p2, p3) {
var ruleRef = rulesMd + "#" + p2.toLowerCase() + "---" +
p3.toLowerCase().replace(/ /g, "-");
return "<a href='#" + p1 + "'><em>" + p1 + "</em></a> - " +
"<a href='" + ruleRef + "'>" + p2 + "</a> " + p3;
});
}).join("<br/>");
var results = window.markdownlint.sync(options);
violations.innerHTML = results.content.map(function mapResult(result) {
var ruleRef = rulesMd + "#" + result.ruleName.toLowerCase() + "---" +
result.ruleDescription.toLowerCase().replace(/ /g, "-");
return "<a href='#" + result.lineNumber + "'><em>" + result.lineNumber +
"</em></a> - <a href='" + ruleRef + "'>" + result.ruleName + "</a> " +
result.ruleDescription +
(result.errorDetail ?
" [<span class='detail'>" +
sanitize(result.errorDetail) +
"</span>]" :
"") +
(result.errorContext ?
" [<span class='detail'>Context: \"" +
sanitize(result.errorContext) +
"\"</span>]" :
"");
}).join("<br/>");
}
// Load from a string or File object

View file

@ -1,5 +1,5 @@
CACHE MANIFEST
# 2016-07-05
# 2016-10-23
default.css
default.htm
default.js

View file

@ -24,6 +24,14 @@ markdownlint(options, function callback(err, result) {
}
});
// Again, using resultVersion 1 for more detail
options.resultVersion = 1;
markdownlint(options, function callback(err, result) {
if (!err) {
console.dir(result, { "colors": true });
}
});
// Make a synchronous call, passing true to toString()
var result = markdownlint.sync(options);
console.log(result.toString(true));

View file

@ -38,34 +38,51 @@ Results.prototype.toString = function resultsToString(useAlias) {
var results = [];
Object.keys(that).forEach(function forFile(file) {
var fileResults = that[file];
Object.keys(fileResults).forEach(function forRule(ruleName) {
var rule = ruleNameToRule[ruleName];
var ruleResults = fileResults[ruleName];
ruleResults.forEach(function forLine(lineNumber) {
var result =
if (Array.isArray(fileResults)) {
fileResults.forEach(function forResult(result) {
results.push(
file + ": " +
lineNumber + ": " +
(useAlias ? rule.aliases[0] : rule.name) + " " +
rule.desc;
results.push(result);
result.lineNumber + ": " +
result.ruleName + "/" +
result.ruleAlias + " " +
result.ruleDescription +
(result.errorDetail ?
" [" + result.errorDetail + "]" :
"") +
(result.errorContext ?
" [Context: \"" + result.errorContext + "\"]" :
""));
});
});
} else {
Object.keys(fileResults).forEach(function forRule(ruleName) {
var rule = ruleNameToRule[ruleName];
var ruleResults = fileResults[ruleName];
ruleResults.forEach(function forLine(lineNumber) {
var result =
file + ": " +
lineNumber + ": " +
(useAlias ? rule.aliases[0] : rule.name) + " " +
rule.desc;
results.push(result);
});
});
}
});
return results.join("\n");
};
// Array.sort comparison for number objects
function numberComparison(a, b) {
return a - b;
// Array.sort comparison for objects in errors array
function lineNumberComparison(a, b) {
return a.lineNumber - b.lineNumber;
}
// Function to return unique values from a sorted array
function uniqueFilterForSorted(value, index, array) {
return (index === 0) || (value > array[index - 1]);
// Function to return unique values from a sorted errors array
function uniqueFilterForSortedErrors(value, index, array) {
return (index === 0) || (value.lineNumber > array[index - 1].lineNumber);
}
// Lints a single string
function lintContent(content, config, frontMatter) { // eslint-disable-line
function lintContent(content, config, frontMatter, resultVersion) {
// Remove front matter (if present at beginning of content)
var frontMatterLines = 0;
if (frontMatter) {
@ -163,7 +180,7 @@ function lintContent(content, config, frontMatter) { // eslint-disable-line
}
});
}
var enabledRulesPerLineNumber = [ null ];
var enabledRulesPerLineNumber = new Array(1 + frontMatterLines);
lines.forEach(function forLine(line) {
var match = shared.inlineCommentRe.exec(line);
if (match) {
@ -182,25 +199,69 @@ function lintContent(content, config, frontMatter) { // eslint-disable-line
"lines": lines
};
// Run each rule
var result = {};
var result = (resultVersion === 1) ? [] : {};
rules.forEach(function forRule(rule) {
// Configure rule
params.options = mergedRules[rule.name];
var errors = [];
function addError(lineNumber, detail, context) {
errors.push({
"lineNumber": lineNumber + frontMatterLines,
"detail": detail || null,
"context": context || null
});
}
errors.add = function add(lineNumber) {
addError(lineNumber);
};
errors.addDetail = function addDetail(lineNumber, detail) {
addError(lineNumber, detail);
};
errors.addDetailIf = function addDetailIf(lineNumber, expected, actual) {
if (expected !== actual) {
addError(lineNumber, "Expected: " + expected + "; Actual: " + actual);
}
};
errors.addContext = function addContext(lineNumber, context, left, right) {
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(lineNumber, null, context);
};
rule.func(params, errors);
// Record any errors (significant performance benefit from length check)
if (errors.length) {
errors.sort(numberComparison);
errors.sort(lineNumberComparison);
var filteredErrors = errors
.filter(uniqueFilterForSorted)
.filter(function removeDisabledRules(lineNumber) {
return enabledRulesPerLineNumber[lineNumber][rule.name];
.filter(uniqueFilterForSortedErrors)
.filter(function removeDisabledRules(error) {
return enabledRulesPerLineNumber[error.lineNumber][rule.name];
})
.map(function adjustLineNumbers(error) {
return error + frontMatterLines;
.map(function formatResults(error) {
if (resultVersion === 1) {
return {
"lineNumber": error.lineNumber,
"ruleName": rule.name,
"ruleAlias": rule.aliases[0],
"ruleDescription": rule.desc,
"errorDetail": error.detail,
"errorContext": error.context
};
}
return error.lineNumber;
});
if (filteredErrors.length) {
result[rule.name] = filteredErrors;
if (resultVersion === 1) {
result.push.apply(result, filteredErrors);
} else {
result[rule.name] = filteredErrors;
}
}
}
});
@ -208,12 +269,18 @@ function lintContent(content, config, frontMatter) { // eslint-disable-line
}
// Lints a single file
function lintFile(file, config, frontMatter, synchronous, callback) {
function lintFile(
file,
config,
frontMatter,
resultVersion,
synchronous,
callback) {
function lintContentWrapper(err, content) {
if (err) {
return callback(err);
}
var result = lintContent(content, config, frontMatter);
var result = lintContent(content, config, frontMatter, resultVersion);
callback(null, result);
}
// Make a/synchronous call to read file
@ -253,13 +320,14 @@ function markdownlint(options, callback) {
var frontMatter = (options.frontMatter === undefined) ?
shared.frontMatterRe : options.frontMatter;
var config = options.config || { "default": true };
var resultVersion = options.resultVersion || 0;
var synchronous = (callback === markdownlintSynchronousCallback);
var results = new Results();
// Helper to lint the next file in the array
function lintFilesArray() {
var file = files.shift();
if (file) {
lintFile(file, config, frontMatter, synchronous,
lintFile(file, config, frontMatter, resultVersion, synchronous,
function lintedFile(err, result) {
if (err) {
return callback(err);
@ -274,7 +342,11 @@ function markdownlint(options, callback) {
}
// Lint strings
Object.keys(strings).forEach(function forKey(key) {
var result = lintContent(strings[key] || "", config, frontMatter);
var result = lintContent(
strings[key] || "",
config,
frontMatter,
resultVersion);
results[key] = result;
});
// Lint files

View file

@ -2,6 +2,11 @@
var shared = require("./shared");
// 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>]*(> |>)/, "");
@ -161,8 +166,9 @@ module.exports = [
var prevLevel = 0;
filterTokens(params, "heading_open", function forToken(token) {
var level = parseInt(token.tag.slice(1), 10);
if (prevLevel && (level > prevLevel + 1)) {
errors.push(token.lineNumber);
if (prevLevel && (level > prevLevel)) {
errors.addDetailIf(token.lineNumber,
"h" + (prevLevel + 1), "h" + level);
}
prevLevel = level;
});
@ -179,9 +185,7 @@ module.exports = [
var tag = "h" + level;
params.tokens.every(function forToken(token) {
if (token.type === "heading_open") {
if (token.tag !== tag) {
errors.push(token.lineNumber);
}
errors.addDetailIf(token.lineNumber, tag, token.tag);
return false;
}
return true;
@ -203,17 +207,22 @@ module.exports = [
}
if (styleForToken !== style) {
var h12 = /h[12]/.test(token.tag);
var hOther = /h[^12]/.test(token.tag);
var setextWithAtx =
(style === "setext_with_atx") &&
((h12 && (styleForToken === "setext")) ||
(hOther && (styleForToken === "atx")));
(!h12 && (styleForToken === "atx")));
var setextWithAtxClosed =
(style === "setext_with_atx_closed") &&
((h12 && (styleForToken === "setext")) ||
(hOther && (styleForToken === "atx_closed")));
(!h12 && (styleForToken === "atx_closed")));
if (!setextWithAtx && !setextWithAtxClosed) {
errors.push(token.lineNumber);
var expected = style;
if (style === "setext_with_atx") {
expected = h12 ? "setext" : "atx";
} else if (style === "setext_with_atx_closed") {
expected = h12 ? "setext" : "atx_closed";
}
errors.addDetailIf(token.lineNumber, expected, styleForToken);
}
}
});
@ -241,11 +250,12 @@ module.exports = [
if (!nestingStyles[nesting] &&
(itemStyle !== nestingStyles[nesting - 1])) {
nestingStyles[nesting] = itemStyle;
} else if (itemStyle !== nestingStyles[nesting]) {
errors.push(item.lineNumber);
} else {
errors.addDetailIf(item.lineNumber,
nestingStyles[nesting], itemStyle);
}
} else if (itemStyle !== expectedStyle) {
errors.push(item.lineNumber);
} else {
errors.addDetailIf(item.lineNumber, expectedStyle, itemStyle);
}
});
}
@ -262,9 +272,7 @@ module.exports = [
flattenLists(params).forEach(function forList(list) {
var indent = indentFor(list.items[0]);
list.items.forEach(function forItem(item) {
if (indentFor(item) !== indent) {
errors.push(item.lineNumber);
}
errors.addDetailIf(item.lineNumber, indent, indentFor(item));
});
});
}
@ -277,8 +285,8 @@ module.exports = [
"aliases": [ "ul-start-left" ],
"func": function MD006(params, errors) {
flattenLists(params).forEach(function forList(list) {
if (list.unordered && !list.nesting && indentFor(list.open)) {
errors.push(list.open.lineNumber);
if (list.unordered && !list.nesting) {
errors.addDetailIf(list.open.lineNumber, 0, indentFor(list.open));
}
});
}
@ -295,9 +303,9 @@ module.exports = [
flattenLists(params).forEach(function forList(list) {
if (list.unordered && list.parentsUnordered) {
var indent = indentFor(list.open);
if ((indent > prevIndent) &&
((indent - prevIndent) !== optionsIndent)) {
errors.push(list.open.lineNumber);
if (indent > prevIndent) {
errors.addDetailIf(list.open.lineNumber,
prevIndent + optionsIndent, indent);
}
prevIndent = indent;
}
@ -313,10 +321,10 @@ module.exports = [
"func": function MD009(params, errors) {
var brSpaces = params.options.br_spaces || 0;
params.lines.forEach(function forLine(line, lineIndex) {
if (/\s$/.test(line) &&
((brSpaces < 2) ||
(line.length - line.trimRight().length !== brSpaces))) {
errors.push(lineIndex + 1);
if (/\s$/.test(line)) {
var expected = (brSpaces < 2) ? 0 : brSpaces;
errors.addDetailIf(lineIndex + 1,
expected, line.length - line.trimRight().length);
}
});
}
@ -332,7 +340,8 @@ module.exports = [
var includeCodeBlocks = (codeBlocks === undefined) ? true : !!codeBlocks;
forEachLine(params, function forLine(line, lineIndex, inCode) {
if (/\t/.test(line) && (!inCode || includeCodeBlocks)) {
errors.push(lineIndex + 1);
errors.addDetail(lineIndex + 1,
"Column: " + (line.indexOf("\t") + 1));
}
});
}
@ -345,8 +354,9 @@ module.exports = [
"aliases": [ "no-reversed-links" ],
"func": function MD011(params, errors) {
forEachInlineChild(params, "text", function forToken(token) {
if (/\([^)]+\)\[[^\]^][^\]]*\]/.test(token.content)) {
errors.push(token.lineNumber);
var match = /\([^)]+\)\[[^\]^][^\]]*\]/.exec(token.content);
if (match) {
errors.addDetail(token.lineNumber, match[0]);
}
});
}
@ -363,7 +373,7 @@ module.exports = [
forEachLine(params, function forLine(line, lineIndex, inCode) {
count = (inCode || line.trim().length) ? 0 : count + 1;
if (maximum < count) {
errors.push(lineIndex + 1);
errors.addDetailIf(lineIndex + 1, maximum, count);
}
});
}
@ -386,7 +396,7 @@ module.exports = [
if ((includeCodeBlocks || !inCode) &&
(includeTables || !inTable) &&
re.test(line)) {
errors.push(lineIndex + 1);
errors.addDetailIf(lineIndex + 1, lineLength, line.length);
}
});
}
@ -405,7 +415,8 @@ module.exports = [
.every(function forLine(line) {
return !line || (allBlank = false) || /^\$\s/.test(line);
}) && !allBlank) {
errors.push(token.lineNumber);
errors.addContext(token.lineNumber,
token.content.split(shared.newLineRe)[0].trim());
}
});
});
@ -420,7 +431,7 @@ module.exports = [
"func": function MD018(params, errors) {
forEachLine(params, function forLine(line, lineIndex, inCode) {
if (!inCode && /^#+[^#\s]/.test(line) && !/#$/.test(line)) {
errors.push(lineIndex + 1);
errors.addContext(lineIndex + 1, line.trim());
}
});
}
@ -435,7 +446,7 @@ module.exports = [
filterTokens(params, "heading_open", function forToken(token) {
if ((headingStyleFor(token) === "atx") &&
/^#+\s\s/.test(token.line)) {
errors.push(token.lineNumber);
errors.addContext(token.lineNumber, token.line.trim());
}
});
}
@ -448,9 +459,12 @@ module.exports = [
"aliases": [ "no-missing-space-closed-atx" ],
"func": function MD020(params, errors) {
forEachLine(params, function forLine(line, lineIndex, inCode) {
if (!inCode && /^#+[^#]*[^\\]#+$/.test(line) &&
(/^#+[^#\s]/.test(line) || /[^#\s]#+$/.test(line))) {
errors.push(lineIndex + 1);
if (!inCode && /^#+[^#]*[^\\]#+$/.test(line)) {
var left = /^#+[^#\s]/.test(line);
var right = /[^#\s]#+$/.test(line);
if (left || right) {
errors.addContext(lineIndex + 1, line.trim(), left, right);
}
}
});
}
@ -463,9 +477,12 @@ module.exports = [
"aliases": [ "no-multiple-space-closed-atx" ],
"func": function MD021(params, errors) {
filterTokens(params, "heading_open", function forToken(token) {
if ((headingStyleFor(token) === "atx_closed") &&
(/^#+\s\s/.test(token.line) || /\s\s#+$/.test(token.line))) {
errors.push(token.lineNumber);
if (headingStyleFor(token) === "atx_closed") {
var left = /^#+\s\s/.test(token.line);
var right = /\s\s#+$/.test(token.line);
if (left || right) {
errors.addContext(token.lineNumber, token.line.trim(), left, right);
}
}
});
}
@ -483,7 +500,7 @@ module.exports = [
params.tokens.forEach(function forToken(token) {
if (token.type === "heading_open") {
if ((token.map[0] - prevMaxLineIndex) === 0) {
errors.push(token.lineNumber);
errors.addContext(token.lineNumber, token.line.trim());
}
} else if (token.type === "heading_close") {
needBlankLine = true;
@ -491,7 +508,8 @@ module.exports = [
if (token.map) {
if (needBlankLine) {
if ((token.map[0] - prevMaxLineIndex) === 0) {
errors.push(prevHeadingLineNumber);
errors.addContext(prevHeadingLineNumber,
params.lines[prevHeadingLineNumber - 1].trim());
}
needBlankLine = false;
}
@ -512,7 +530,7 @@ module.exports = [
"func": function MD023(params, errors) {
filterTokens(params, "heading_open", function forToken(token) {
if (/^\s/.test(token.line)) {
errors.push(token.lineNumber);
errors.addContext(token.lineNumber, token.line);
}
});
}
@ -529,7 +547,7 @@ module.exports = [
if (knownContent.indexOf(content) === -1) {
knownContent.push(content);
} else {
errors.push(heading.lineNumber);
errors.addContext(heading.lineNumber, heading.line.trim());
}
});
}
@ -547,7 +565,7 @@ module.exports = [
filterTokens(params, "heading_open", function forToken(token) {
if (token.tag === tag) {
if (hasTopLevelHeading) {
errors.push(token.lineNumber);
errors.addContext(token.lineNumber, token.line.trim());
} else if (token.lineNumber === 1) {
hasTopLevelHeading = true;
}
@ -565,8 +583,10 @@ module.exports = [
var punctuation = params.options.punctuation || ".,;:!?";
var re = new RegExp("[" + punctuation + "]$");
forEachHeading(params, function forHeading(heading, content) {
if (re.test(content)) {
errors.push(heading.lineNumber);
var match = re.exec(content);
if (match) {
errors.addDetail(heading.lineNumber,
"Punctuation: '" + match[0] + "'");
}
});
}
@ -585,11 +605,13 @@ module.exports = [
} else if (token.type === "blockquote_close") {
blockquoteNesting--;
} else if ((token.type === "inline") && (blockquoteNesting > 0)) {
if (/^(\s*>)+\s\s/.test(token.line)) {
errors.addContext(token.lineNumber, token.line);
}
token.content.split(shared.newLineRe)
.forEach(function forLine(line, offset) {
if (/^\s/.test(line) ||
(!offset && /^(\s*>)+\s\s/.test(token.line))) {
errors.push(token.lineNumber + offset);
if (/^\s/.test(line)) {
errors.addContext(token.lineNumber + offset, "> " + line);
}
});
}
@ -607,7 +629,7 @@ module.exports = [
params.tokens.forEach(function forToken(token) {
if ((token.type === "blockquote_open") &&
(prevToken.type === "blockquote_close")) {
errors.push(token.lineNumber - 1);
errors.add(token.lineNumber - 1);
}
prevToken = token;
});
@ -625,10 +647,9 @@ module.exports = [
if (!list.unordered) {
var number = 1;
list.items.forEach(function forItem(item) {
var re = new RegExp("^[\\s>]*" + String(number) + "[\\.)]");
if (!re.test(item.line)) {
errors.push(item.lineNumber);
}
var match = /^[\s>]*([^\.)]*)[\.)]/.exec(item.line);
errors.addDetailIf(item.lineNumber,
String(number), !match || match[1]);
if (style === "ordered") {
number++;
}
@ -656,9 +677,8 @@ module.exports = [
(allSingle ? olSingle : olMulti);
list.items.forEach(function forItem(item) {
var match = /^[\s>]*\S+(\s+)/.exec(item.line);
if (!match || (match[1].length !== expectedSpaces)) {
errors.push(item.lineNumber);
}
errors.addDetailIf(item.lineNumber,
expectedSpaces, !match || match[1].length);
});
});
}
@ -674,7 +694,7 @@ module.exports = [
forEachLine(params, function forLine(line, i, inCode, onFence) {
if (((onFence > 0) && (i - 1 >= 0) && lines[i - 1].length) ||
((onFence < 0) && (i + 1 < lines.length) && lines[i + 1].length)) {
errors.push(i + 1);
errors.addContext(i + 1, lines[i].trim());
}
});
}
@ -690,11 +710,12 @@ module.exports = [
var prevLine = "";
forEachLine(params, function forLine(line, lineIndex, inCode, onFence) {
if (!inCode || onFence) {
var listMarker = /^([\*\+\-]|(\d+\.))\s/.test(line.trim());
var lineTrim = line.trim();
var listMarker = /^([\*\+\-]|(\d+\.))\s/.test(lineTrim);
if (listMarker && !inList && !/^($|\s)/.test(prevLine)) {
errors.push(lineIndex + 1);
errors.addContext(lineIndex + 1, lineTrim);
} else if (!listMarker && inList && !/^($|\s)/.test(line)) {
errors.push(lineIndex);
errors.addContext(lineIndex, lineTrim);
}
inList = listMarker;
}
@ -723,11 +744,12 @@ module.exports = [
.map(function forElement(element) {
return element.slice(1).toLowerCase();
})
.every(function forElement(element) {
return allowedElements.indexOf(element) !== -1;
.filter(function forElement(element) {
return allowedElements.indexOf(element) === -1;
});
if (!allowed) {
errors.push(token.lineNumber + offset);
if (allowed.length) {
errors.addDetail(token.lineNumber + offset,
"Element: " + allowed[0]);
}
});
}
@ -745,14 +767,15 @@ module.exports = [
filterTokens(params, "inline", function forToken(token) {
var inLink = false;
token.children.forEach(function forChild(child) {
var match = null;
if (child.type === "link_open") {
inLink = true;
} else if (child.type === "link_close") {
inLink = false;
} else if ((child.type === "text") &&
!inLink &&
/https?:\/\//.test(child.content)) {
errors.push(child.lineNumber);
(match = /(http|ftp)s?:\/\/[^\s]*/.exec(child.content))) {
errors.addContext(child.lineNumber, match[0]);
}
});
});
@ -767,12 +790,11 @@ module.exports = [
"func": function MD035(params, errors) {
var style = params.options.style || "consistent";
filterTokens(params, "hr", function forToken(token) {
var lineTrim = token.line.trim();
if (style === "consistent") {
style = token.line;
}
if (token.line !== style) {
errors.push(token.lineNumber);
style = lineTrim;
}
errors.addDetailIf(token.lineNumber, style, lineTrim);
});
}
},
@ -794,7 +816,7 @@ module.exports = [
(t.children[0].type === "em_open")) &&
(t.children[1].type === "text") &&
!re.test(t.children[1].content)) {
errors.push(t.lineNumber);
errors.addContext(t.lineNumber, t.children[1].content);
}
};
} else if (token.type === "blockquote_open") {
@ -825,9 +847,12 @@ module.exports = [
"aliases": [ "no-space-in-emphasis" ],
"func": function MD037(params, errors) {
forEachInlineChild(params, "text", function forToken(token) {
if (/\s(\*\*?|__?)\s.+\1/.test(token.content) ||
/(\*\*?|__?).+\s\1\s/.test(token.content)) {
errors.push(token.lineNumber);
var left = /\s(\*\*?|__?)\s.+\1/.exec(token.content);
var right = /(\*\*?|__?).+\s\1\s/.exec(token.content);
if (left) {
errors.addContext(token.lineNumber, left[0].trim());
} else if (right) {
errors.addContext(token.lineNumber, right[0].trim(), false, true);
}
});
}
@ -841,8 +866,15 @@ module.exports = [
"func": function MD038(params, errors) {
forEachInlineChild(params, "code_inline",
function forToken(token, inline) {
if (inline.content.indexOf("`" + token.content + "`") === -1) {
errors.push(token.lineNumber);
var escapedContent = escapeForRegExp(token.content);
var left = (new RegExp("`\\s+" + escapedContent + "\\s*`"))
.exec(inline.content);
var right = (new RegExp("`\\s*" + escapedContent + "\\s+`"))
.exec(inline.content);
if (left) {
errors.addContext(token.lineNumber, left[0]);
} else if (right) {
errors.addContext(token.lineNumber, right[0], false, true);
}
});
}
@ -856,26 +888,20 @@ module.exports = [
"func": function MD039(params, errors) {
filterTokens(params, "inline", function forToken(token) {
var inLink = false;
var index = 0;
var lastChildRightSpaceLineNumber = 0;
var linkText = "";
token.children.forEach(function forChild(child) {
if (child.type === "link_open") {
inLink = true;
index = 0;
linkText = "";
} else if (child.type === "link_close") {
inLink = false;
if (lastChildRightSpaceLineNumber) {
errors.push(lastChildRightSpaceLineNumber);
var left = linkText.trimLeft().length !== linkText.length;
var right = linkText.trimRight().length !== linkText.length;
if (left || right) {
errors.addContext(token.lineNumber, linkText, left, right);
}
} 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) ?
0 : child.lineNumber;
index++;
linkText += child.content;
}
});
});
@ -890,7 +916,7 @@ module.exports = [
"func": function MD040(params, errors) {
filterTokens(params, "fence", function forToken(token) {
if (!token.info.trim()) {
errors.push(token.lineNumber);
errors.addContext(token.lineNumber, token.line);
}
});
}
@ -917,7 +943,7 @@ module.exports = [
if (!firstHeader ||
(firstHeader.lineNumber !== 1) ||
(firstHeader.tag !== tag)) {
errors.push(1);
errors.addContext(1, params.lines[0]);
}
}
},
@ -928,10 +954,26 @@ module.exports = [
"tags": [ "links" ],
"aliases": [ "no-empty-links" ],
"func": function MD042(params, errors) {
forEachInlineChild(params, "link_open", function forToken(token) {
token.attrs.forEach(function forAttr(attr) {
if (attr[0] === "href" && (!attr[1] || (attr[1] === "#"))) {
errors.push(token.lineNumber);
filterTokens(params, "inline", function forToken(token) {
var inLink = false;
var linkText = "";
var emptyLink = false;
token.children.forEach(function forChild(child) {
if (child.type === "link_open") {
inLink = true;
linkText = "";
child.attrs.forEach(function forAttr(attr) {
if (attr[0] === "href" && (!attr[1] || (attr[1] === "#"))) {
emptyLink = true;
}
});
} else if (child.type === "link_close") {
inLink = false;
if (emptyLink) {
errors.addContext(child.lineNumber, "[" + linkText + "]");
}
} else if (inLink) {
linkText += child.content;
}
});
});
@ -963,12 +1005,12 @@ module.exports = [
} else if (optional) {
i--;
} else {
errors.push(heading.lineNumber);
errors.addDetailIf(heading.lineNumber, expected, actual);
}
}
});
if ((i < requiredHeaders.length) && !errors.length) {
errors.push(params.lines.length);
errors.addContext(params.lines.length, requiredHeaders[i]);
}
}
}

View file

@ -4,6 +4,8 @@ Header 1
Header 2
--------
## Header 2 {MD003}
### Header 3
#### Header 4 {MD003} ####

View file

@ -4,6 +4,8 @@ Header 1
Header 2
--------
## Header 2 {MD003} ##
### Header 3 ###
#### Header 4 {MD003}

View file

@ -80,7 +80,7 @@ module.exports.projectFiles = function projectFiles(test) {
test.expect(2);
var options = {
"files": [ "README.md" ],
"config": { "MD013": false }
"config": { "MD013": { "line_length": 150 } }
};
markdownlint(options, function callback(err, actual) {
test.ifError(err);
@ -90,7 +90,7 @@ module.exports.projectFiles = function projectFiles(test) {
});
};
module.exports.resultFormatting = function resultFormatting(test) {
module.exports.resultFormattingV0 = function resultFormattingV0(test) {
test.expect(4);
var options = {
"files": [
@ -142,14 +142,15 @@ module.exports.resultFormatting = function resultFormatting(test) {
});
};
module.exports.resultFormattingSync = function resultFormattingSync(test) {
module.exports.resultFormattingSyncV0 = function resultFormattingSyncV0(test) {
test.expect(3);
var options = {
"files": [
"./test/atx_header_spacing.md",
"./test/first_header_bad_atx.md"
],
"config": defaultConfig
"config": defaultConfig,
"resultVersion": 0
};
var actualResult = markdownlint.sync(options);
var expectedResult = {
@ -192,6 +193,93 @@ module.exports.resultFormattingSync = function resultFormattingSync(test) {
test.done();
};
module.exports.resultFormattingV1 = function resultFormattingV1(test) {
test.expect(3);
var options = {
"strings": {
"truncate":
"# Multiple spaces inside hashes on closed atx style header #"
},
"files": [
"./test/atx_header_spacing.md",
"./test/first_header_bad_atx.md"
],
"config": defaultConfig,
"resultVersion": 1
};
markdownlint(options, function callback(err, actualResult) {
test.ifError(err);
var expectedResult = {
"truncate": [
{ "lineNumber": 1,
"ruleName": "MD021",
"ruleAlias": "no-multiple-space-closed-atx",
"ruleDescription":
"Multiple spaces inside hashes on closed atx style header",
"errorDetail": null,
"errorContext": "# Multiple spa...style header #" }
],
"./test/atx_header_spacing.md": [
{ "lineNumber": 3,
"ruleName": "MD002",
"ruleAlias": "first-header-h1",
"ruleDescription": "First header should be a top level header",
"errorDetail": "Expected: h1; Actual: h2",
"errorContext": null },
{ "lineNumber": 1,
"ruleName": "MD018",
"ruleAlias": "no-missing-space-atx",
"ruleDescription": "No space after hash on atx style header",
"errorDetail": null,
"errorContext": "#Header 1 {MD018}" },
{ "lineNumber": 3,
"ruleName": "MD019",
"ruleAlias": "no-multiple-space-atx",
"ruleDescription": "Multiple spaces after hash on atx style header",
"errorDetail": null,
"errorContext": "## Header 2 {MD019}" },
{ "lineNumber": 5,
"ruleName": "MD019",
"ruleAlias": "no-multiple-space-atx",
"ruleDescription": "Multiple spaces after hash on atx style header",
"errorDetail": null,
"errorContext": "## Header 3 {MD019}" }
],
"./test/first_header_bad_atx.md": [
{ "lineNumber": 1,
"ruleName": "MD002",
"ruleAlias": "first-header-h1",
"ruleDescription": "First header should be a top level header",
"errorDetail": "Expected: h1; Actual: h2",
"errorContext": null }
]
};
test.deepEqual(actualResult, expectedResult, "Undetected issues.");
var actualMessage = actualResult.toString();
var expectedMessage =
"truncate: 1: MD021/no-multiple-space-closed-atx" +
" Multiple spaces inside hashes on closed atx style header" +
" [Context: \"# Multiple spa...style header #\"]\n" +
"./test/atx_header_spacing.md: 3: MD002/first-header-h1" +
" First header should be a top level header" +
" [Expected: h1; Actual: h2]\n" +
"./test/atx_header_spacing.md: 1: MD018/no-missing-space-atx" +
" No space after hash on atx style header" +
" [Context: \"#Header 1 {MD018}\"]\n" +
"./test/atx_header_spacing.md: 3: MD019/no-multiple-space-atx" +
" Multiple spaces after hash on atx style header" +
" [Context: \"## Header 2 {MD019}\"]\n" +
"./test/atx_header_spacing.md: 5: MD019/no-multiple-space-atx" +
" Multiple spaces after hash on atx style header" +
" [Context: \"## Header 3 {MD019}\"]\n" +
"./test/first_header_bad_atx.md: 1: MD002/first-header-h1" +
" First header should be a top level header" +
" [Expected: h1; Actual: h2]";
test.equal(actualMessage, expectedMessage, "Incorrect message.");
test.done();
});
};
module.exports.stringInputLineEndings = function stringInputLineEndings(test) {
test.expect(2);
var options = {
@ -639,6 +727,9 @@ module.exports.readmeHeaders = function readmeHeaders(test) {
"files": "README.md",
"config": {
"default": false,
"MD013": {
"line_length": 150
},
"MD043": {
"headers": [
"# markdownlint",
@ -655,6 +746,7 @@ module.exports.readmeHeaders = function readmeHeaders(test) {
"#### options.strings",
"#### options.frontMatter",
"#### options.config",
"#### options.resultVersion",
"### callback",
"### result",
"## Usage",
@ -689,7 +781,7 @@ module.exports.filesArrayAsString = function filesArrayAsString(test) {
test.expect(2);
markdownlint({
"files": "README.md",
"config": { "MD013": false }
"config": { "MD013": { "line_length": 150 } }
}, function callback(err, actual) {
test.ifError(err);
var expected = { "README.md": {} };