Update MD013/line-length to include strict mode (fixes #237).

This commit is contained in:
David Anson 2019-12-12 21:22:45 -08:00
parent 5ae5e448b0
commit b39e5e309e
7 changed files with 78 additions and 16 deletions

View file

@ -453,7 +453,7 @@ Tags: line_length
Aliases: line-length
Parameters: line_length, heading_line_length, code_block_line_length, code_blocks, tables, headings, headers (number; default 80, boolean; default true)
Parameters: line_length, heading_line_length, code_block_line_length, code_blocks, tables, headings, headers, strict (number; default 80, boolean; default true)
> If `headings` is not provided, `headers` (deprecated) will be used.
@ -465,7 +465,8 @@ up into multiple lines. To set a different maximum length for headings, use
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.
being forced to break them in the middle. To disable this exception, set the
`strict` parameter to `true`.
You have the option to exclude this rule for code blocks, tables, or headings.
To do so, set the `code_blocks`, `tables`, or `headings` parameter(s) to false.

View file

@ -385,12 +385,8 @@ module.exports.rangeFromRegExp = function rangeFromRegExp(line, regexp) {
let range = null;
const match = line.match(regexp);
if (match) {
let column = match.index + 1;
let length = match[0].length;
if (match[2]) {
column += match[1].length;
length -= match[1].length;
}
const column = match.index + 1;
const length = match[0].length;
range = [ column, length ];
}
return range;

View file

@ -3,11 +3,12 @@
"use strict";
const { addErrorDetailIf, filterTokens, forEachHeading, forEachLine,
includesSorted, rangeFromRegExp } = require("../helpers");
includesSorted } = require("../helpers");
const { lineMetadata } = require("./cache");
const longLineRePrefix = "^(.{";
const longLineRePostfix = "})(.*\\s.*)$";
const longLineRePrefix = "^.{";
const longLineRePostfixRelaxed = "}.*\\s.*$";
const longLineRePostfixStrict = "}.+$";
const labelRe = /^\s*\[.*[^\\]]:/;
const linkOnlyLineRe = /^[es]*lT?L[ES]*$/;
const tokenTypeMap = {
@ -28,6 +29,9 @@ module.exports = {
const lineLength = params.config.line_length || 80;
const headingLineLength = params.config.heading_line_length || lineLength;
const codeLineLength = params.config.code_block_line_length || lineLength;
const strict = !!params.config.strict;
const longLineRePostfix =
strict ? longLineRePostfixStrict : longLineRePostfixRelaxed;
const longLineRe =
new RegExp(longLineRePrefix + lineLength + longLineRePostfix);
const longHeadingLineRe =
@ -71,11 +75,18 @@ module.exports = {
if ((includeCodeBlocks || !inCode) &&
(includeTables || !inTable) &&
(includeHeadings || !isHeading) &&
!includesSorted(linkOnlyLineNumbers, lineNumber) &&
lengthRe.test(line) &&
!labelRe.test(line)) {
addErrorDetailIf(onError, lineNumber, length, line.length,
null, null, rangeFromRegExp(line, lengthRe));
(strict ||
(!includesSorted(linkOnlyLineNumbers, lineNumber) &&
!labelRe.test(line))) &&
lengthRe.test(line)) {
addErrorDetailIf(
onError,
lineNumber,
length,
line.length,
null,
null,
[ length + 1, line.length - length ]);
}
});
}

View file

@ -171,6 +171,11 @@ rules.forEach(function forRule(rule) {
"description": "Include headings",
"type": "boolean",
"default": true
},
"strict": {
"description": "Strict length checking",
"type": "boolean",
"default": false
}
};
break;

View file

@ -418,6 +418,11 @@
"description": "Include headings",
"type": "boolean",
"default": true
},
"strict": {
"description": "Strict length checking",
"type": "boolean",
"default": false
}
},
"additionalProperties": false
@ -464,6 +469,11 @@
"description": "Include headings",
"type": "boolean",
"default": true
},
"strict": {
"description": "Strict length checking",
"type": "boolean",
"default": false
}
},
"additionalProperties": false

View file

@ -0,0 +1,8 @@
{
"default": true,
"MD013": {
"strict": true,
"heading_line_length": 30,
"code_block_line_length": 20
}
}

31
test/long-lines-strict.md Normal file
View file

@ -0,0 +1,31 @@
# Long Lines Strict
12345678901234567890123456789012345678901234567890123456789012345678901234567890
This line is too long. text text text text text text text text text text text text
This line is way too long. text text text text text text text text text text text text
This line is barely too long. text text text text text text text text text text t
This line is just okay. text text text text text text text text text text text t
This line is not a problem. text text text text text text text text text text t
## This heading is way too long
## This heading is long but ok
## This heading is short + ok
```text
This code is too long
This code is a-okay.
This code is short.
```
{MD013:5}
{MD013:7}
{MD013:9}
{MD013:15}
{MD013:22}