Update MD013 to allow excluding code and tables.

This commit is contained in:
David Anson 2016-01-09 22:20:36 -08:00
parent 36eaa821d9
commit b7342485d9
5 changed files with 106 additions and 8 deletions

View file

@ -49,7 +49,7 @@ function forEachLine(params, callback) {
var fence = match && match[1];
if (fence &&
(!inFence || (fence.substr(0, fenceStart.length) === fenceStart))) {
metadata = inFence ? -2 : 2;
metadata = inFence ? 2 : 6;
fenceStart = inFence ? null : fence;
inFence = !inFence;
} else if (inFence) {
@ -63,12 +63,23 @@ function forEachLine(params, callback) {
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, (metadata >> 1));
callback(
line,
lineIndex,
!!(metadata & 7),
(((metadata & 6) >> 1) || 2) - 2,
!!(metadata & 8));
});
}
@ -326,12 +337,19 @@ module.exports = [
"tags": [ "line_length" ],
"func": function MD013(params, errors) {
var lineLength = params.options.line_length || 80;
var codeBlocks = params.options.code_blocks;
var includeCodeBlocks = (codeBlocks === undefined) ? true : !!codeBlocks;
var tables = params.options.tables;
var includeTables = (tables === undefined) ? true : !!tables;
var re = new RegExp("^.{" + lineLength + "}.*\\s");
params.lines.forEach(function forLine(line, lineIndex) {
if (re.test(line)) {
errors.push(lineIndex + 1);
}
});
forEachLine(params,
function forLine(line, lineIndex, inCode, onFence, inTable) {
if ((includeCodeBlocks || !inCode) &&
(includeTables || !inTable) &&
re.test(line)) {
errors.push(lineIndex + 1);
}
});
}
},