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

@ -266,7 +266,7 @@ lines inside code blocks.
Tags: line_length Tags: line_length
Parameters: line_length (number; default 80) Parameters: line_length, code_blocks, tables (number; default 80, boolean; default true)
This rule is triggered when there are lines that are longer than the This rule is triggered when there are lines that are longer than the
configured line length (default: 80 characters). To fix this, split the line configured line length (default: 80 characters). To fix this, split the line
@ -276,6 +276,13 @@ This rule has an exception where there is no whitespace beyond the configured
line length. This allows you to still include items such as long URLs without line length. This allows you to still include items such as long URLs without
being forced to break them in the middle. being forced to break them in the middle.
You also have the option to exclude this rule for code blocks and tables. To
do this, set the `code_blocks` and/or `tables` parameters to false.
Code blocks are included in this rule by default since it is often a
requirement for document readability, and tentatively compatible with code
rules. Still, some languages do not lend themselves to short lines.
## MD014 - Dollar signs used before commands without showing output ## MD014 - Dollar signs used before commands without showing output
Tags: code Tags: code

View file

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

View file

@ -0,0 +1,28 @@
This is a short line.
This is a very very very very very very very very very very very very very very very very very very very very long line. {MD013}
This is a short line.
```text
Here is a short line in a code block.
Here is a very very very very very very very very very very very very very very very very very very very long line in a code block. {MD013}
```
This is a short line.
| First Header | Second Header | Third Header | Fourth Header | Fifth Header | Sixth Header | {MD013}
| ------------- | ------------- | ------------- | ------------- | ------------- | ------------- | {MD013}
| Content Cell | Content Cell | Content Cell | Content Cell | Content Cell | Content Cell | {MD013}
| ============= | ============= | ============= | ============= | ============= | ============= | {MD013}
| Footer Cell | Footer Cell | Footer Cell | Footer Cell | Footer Cell | Footer Cell | {MD013}
This is a very very very very very very very very very very very very very very very very very very very very long line. {MD013}
Another line.
| First Header | Second Header | Third Header | Fourth Header | Fifth Header | Sixth Header | {MD013}
| ------------- | ------------- | ------------- | ------------- | ------------- | ------------- | {MD013}
| Content Cell | Content Cell | Content Cell | Content Cell | Content Cell | Content Cell | {MD013}
| ============= | ============= | ============= | ============= | ============= | ============= | {MD013}
| Footer Cell | Footer Cell | Footer Cell | Footer Cell | Footer Cell | Footer Cell | {MD013}

View file

@ -0,0 +1,7 @@
{
"default": true,
"MD013": {
"code_blocks": false,
"tables": false
}
}

38
test/long_lines_code.md Normal file
View file

@ -0,0 +1,38 @@
This is a short line.
This is a very very very very very very very very very very very very very very very very very very very very long line. {MD013}
This is a short line.
```text
Here is a short line in a code block.
Here is a very very very very very very very very very very very very very very very very very very very long line in a code block.
```
This is a short line.
| First Header | Second Header | Third Header | Fourth Header | Fifth Header | Sixth Header |
| ------------- | ------------- | ------------- | ------------- | ------------- | ------------- |
| Content Cell | Content Cell | Content Cell | Content Cell | Content Cell | Content Cell |
| Content Cell | Content Cell | Content Cell | Content Cell | Content Cell | Content Cell |
| ------------- | ------------- | ------------- | ------------- | ------------- | ------------- |
| Content Cell | Content Cell | Content Cell | Content Cell | Content Cell | Content Cell |
| Content Cell | Content Cell | Content Cell | Content Cell | Content Cell | Content Cell |
| ============= | ============= | ============= | ============= | ============= | ============= |
| Footer Cell | Footer Cell | Footer Cell | Footer Cell | Footer Cell | Footer Cell |
{: rules="groups"}
This is a very very very very very very very very very very very very very very very very very very very very long line. {MD013}
Another line.
| First Header | Second Header | Third Header | Fourth Header | Fifth Header | Sixth Header |
| ------------- | ------------- | ------------- | ------------- | ------------- | ------------- |
| Content Cell | Content Cell | Content Cell | Content Cell | Content Cell | Content Cell |
| Content Cell | Content Cell | Content Cell | Content Cell | Content Cell | Content Cell |
| ------------- | ------------- | ------------- | ------------- | ------------- | ------------- |
| Content Cell | Content Cell | Content Cell | Content Cell | Content Cell | Content Cell |
| Content Cell | Content Cell | Content Cell | Content Cell | Content Cell | Content Cell |
| ============= | ============= | ============= | ============= | ============= | ============= |
| Footer Cell | Footer Cell | Footer Cell | Footer Cell | Footer Cell | Footer Cell |
{: rules="groups"}