diff --git a/doc/Rules.md b/doc/Rules.md index 20e6c6d5..b02045b1 100644 --- a/doc/Rules.md +++ b/doc/Rules.md @@ -266,7 +266,7 @@ lines inside code blocks. 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 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 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 Tags: code diff --git a/lib/rules.js b/lib/rules.js index 2bf6f0d5..7a387af3 100644 --- a/lib/rules.js +++ b/lib/rules.js @@ -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); + } + }); } }, diff --git a/test/long_lines_code-default.md b/test/long_lines_code-default.md new file mode 100644 index 00000000..f90eab79 --- /dev/null +++ b/test/long_lines_code-default.md @@ -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} diff --git a/test/long_lines_code.json b/test/long_lines_code.json new file mode 100644 index 00000000..5e1da5fb --- /dev/null +++ b/test/long_lines_code.json @@ -0,0 +1,7 @@ +{ + "default": true, + "MD013": { + "code_blocks": false, + "tables": false + } +} diff --git a/test/long_lines_code.md b/test/long_lines_code.md new file mode 100644 index 00000000..cf3d2f1d --- /dev/null +++ b/test/long_lines_code.md @@ -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"}