Update MD013/line-length to support stern parameter (fixes #256).

This commit is contained in:
David Anson 2020-03-22 14:06:29 -07:00
parent 11ea2ad5f1
commit de86a26e4e
6 changed files with 101 additions and 5 deletions

View file

@ -474,7 +474,7 @@ Tags: line_length
Aliases: line-length
Parameters: line_length, heading_line_length, code_block_line_length, code_blocks, tables, headings, headers, strict (number; default 80, boolean; default true)
Parameters: line_length, heading_line_length, code_block_line_length, code_blocks, tables, headings, headers, strict, stern (number; default 80 for *_length, boolean; default true (except strict/stern which default false))
> If `headings` is not provided, `headers` (deprecated) will be used.
@ -484,10 +484,24 @@ up into multiple lines. To set a different maximum length for headings, use
`heading_line_length`. To set a different maximum length for code blocks, use
`code_block_line_length`
This rule has an exception where there is no whitespace beyond the configured
This rule has an exception when 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. To disable this exception, set the
`strict` parameter to `true`.
`strict` parameter to `true` to report an issue when any line is too long.
To warn for lines that are too long and could be fixed but allow lines without
spaces, set the `stern` parameter to `true`.
For example (assuming normal behavior):
```markdown
IF THIS LINE IS THE MAXIMUM LENGTH
This line is okay because there are-no-spaces-beyond-that-length
And this line is a violation because there are
This-line-is-also-okay-because-there-are-no-spaces
```
In `strict` or `stern` modes, the two middle lines above are a violation. The
third line is a violation in `strict` mode, but allowed in `stern` mode.
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

@ -11,6 +11,7 @@ const longLineRePostfixRelaxed = "}.*\\s.*$";
const longLineRePostfixStrict = "}.+$";
const labelRe = /^\s*\[.*[^\\]]:/;
const linkOrImageOnlyLineRe = /^[es]*(lT?L|I)[ES]*$/;
const sternModeRe = /^([#>\s]*\s)?\S*$/;
const tokenTypeMap = {
"em_open": "e",
"em_close": "E",
@ -33,8 +34,9 @@ module.exports = {
const codeLineLength =
Number(params.config.code_block_line_length || lineLength);
const strict = !!params.config.strict;
const stern = !!params.config.stern;
const longLineRePostfix =
strict ? longLineRePostfixStrict : longLineRePostfixRelaxed;
(strict || stern) ? longLineRePostfixStrict : longLineRePostfixRelaxed;
const longLineRe =
new RegExp(longLineRePrefix + lineLength + longLineRePostfix);
const longHeadingLineRe =
@ -79,7 +81,8 @@ module.exports = {
(includeTables || !inTable) &&
(includeHeadings || !isHeading) &&
(strict ||
(!includesSorted(linkOnlyLineNumbers, lineNumber) &&
(!(stern && sternModeRe.test(line)) &&
!includesSorted(linkOnlyLineNumbers, lineNumber) &&
!labelRe.test(line))) &&
lengthRe.test(line)) {
addErrorDetailIf(

View file

@ -181,6 +181,11 @@ rules.forEach(function forRule(rule) {
"description": "Strict length checking",
"type": "boolean",
"default": false
},
"stern": {
"description": "Stern length checking",
"type": "boolean",
"default": false
}
};
break;

View file

@ -433,6 +433,11 @@
"description": "Strict length checking",
"type": "boolean",
"default": false
},
"stern": {
"description": "Stern length checking",
"type": "boolean",
"default": false
}
},
"additionalProperties": false
@ -484,6 +489,11 @@
"description": "Strict length checking",
"type": "boolean",
"default": false
},
"stern": {
"description": "Stern length checking",
"type": "boolean",
"default": false
}
},
"additionalProperties": false

View file

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

56
test/long-lines-stern.md Normal file
View file

@ -0,0 +1,56 @@
# Long Lines Stern
12345678901234567890123456789012345678901234567890123456789012345678901234567890
This line is 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 line is too long. texttexttexttexttexttexttexttexttexttexttexttexttexttexttexttext
ThisLineIsOkaytexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttext
## This heading is way too long
## This heading is long but ok
## This heading is short + ok
## ThisTooLongHeadingIsOkaytext
```text
This code is too long
This code is a-okay.
This code is short.
ThisTooLongCodeIsOkay.
```
* This list item line is too long. text text text text text text text text text text
* This list item line is okay. text text text text text text text text text
This list item line is okay. text text text text text text text text text
This list item line is too long. text text text text text text text text text text
ThisTooLongListItemLineIsOkaytexttexttexttexttexttexttexttexttexttexttexttexttext
> This blockquote line is too long. text text text text text text text text text text
> This blockquote line is okay. text text text text text text text text text
> ThisTooLongBlockquoteLineIsOkaytexttexttexttexttexttexttexttexttexttexttexttexttext
>
> > This double blockquote line is too long. text text text text text text text text
> > This double blockquote line is okay. text text text text text text text
> > ThisTooLongDoubleBlockquoteLineIsOkaytexttexttexttexttexttexttexttexttexttexttext
ThisLineIsTooLongButIsNotReportedBecauseItLooksLikePartOfAListItemtexttexttexttext
{MD013:5}
{MD013:7}
{MD013:13}
{MD013:17}
{MD013:26}
{MD013:32}
{MD013:35}
{MD013:38}
{MD013:42}