Update MD013/line-length to permit long link/image reference definitions in all modes because they can not be easily split.

This commit is contained in:
David Anson 2022-11-08 21:40:33 -08:00
parent fe1e93e20d
commit 99a3f164a9
11 changed files with 206 additions and 25 deletions

View file

@ -764,6 +764,7 @@ function getReferenceLinkImageData(lineMetadata) {
const shortcuts = new Set();
const definitions = new Map();
const duplicateDefinitions = [];
const definitionLineIndices = [];
// Define helper functions
const normalizeLabel = (s) => s.toLowerCase().trim().replace(/\s+/g, " ");
const exclusions = [];
@ -804,6 +805,11 @@ function getReferenceLinkImageData(lineMetadata) {
}
const labelLength = linkReferenceDefinitionMatch[0].length;
exclusions.push([0, lineOffsets[lineIndex], labelLength]);
const hasDefinition = line.slice(labelLength).trim().length > 0;
definitionLineIndices.push(lineIndex);
if (!hasDefinition) {
definitionLineIndices.push(lineIndex + 1);
}
}
}
});
@ -876,7 +882,8 @@ function getReferenceLinkImageData(lineMetadata) {
references,
shortcuts,
definitions,
duplicateDefinitions
duplicateDefinitions,
definitionLineIndices
};
}
module.exports.getReferenceLinkImageData = getReferenceLinkImageData;
@ -2832,8 +2839,8 @@ module.exports = {
"use strict";
// @ts-check
const { addErrorDetailIf, filterTokens, forEachHeading, forEachLine, includesSorted, linkReferenceDefinitionRe } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { lineMetadata } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
const { addErrorDetailIf, filterTokens, forEachHeading, forEachLine, includesSorted } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { lineMetadata, referenceLinkImageData } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
const longLineRePrefix = "^.{";
const longLineRePostfixRelaxed = "}.*\\s.*$";
const longLineRePostfixStrict = "}.+$";
@ -2888,6 +2895,7 @@ module.exports = {
linkOnlyLineNumbers.push(token.lineNumber);
}
});
const { definitionLineIndices } = referenceLinkImageData();
forEachLine(lineMetadata(), (line, lineIndex, inCode, onFence, inTable) => {
const lineNumber = lineIndex + 1;
const isHeading = includesSorted(headingLineNumbers, lineNumber);
@ -2900,10 +2908,10 @@ module.exports = {
if ((includeCodeBlocks || !inCode) &&
(includeTables || !inTable) &&
(includeHeadings || !isHeading) &&
!includesSorted(definitionLineIndices, lineIndex) &&
(strict ||
(!(stern && sternModeRe.test(line)) &&
!includesSorted(linkOnlyLineNumbers, lineNumber) &&
!linkReferenceDefinitionRe.test(line))) &&
!includesSorted(linkOnlyLineNumbers, lineNumber))) &&
lengthRe.test(line)) {
addErrorDetailIf(onError, lineNumber, length, line.length, null, null, [length + 1, line.length - length]);
}

View file

@ -7,11 +7,11 @@ up into multiple lines. To set a different maximum length for headings, use
`code_block_line_length`
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` 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`.
line length. This allows you to 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` and an issue will be reported when any line is too long. To
warn for lines that are too long and could be fixed but allow long lines
without spaces, set the `stern` parameter to `true`.
For example (assuming normal behavior):
@ -32,5 +32,9 @@ 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.
Lines with link/image reference definitions are always exempted from this rule
(even in `strict` mode) because there is generally no way to split such lines
without breaking the URL.
Rationale: Extremely long lines can be difficult to work with in some editors.
More information: <https://cirosantilli.com/markdown-style-guide#line-wrapping>.

View file

@ -557,11 +557,11 @@ up into multiple lines. To set a different maximum length for headings, use
`code_block_line_length`
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` 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`.
line length. This allows you to 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` and an issue will be reported when any line is too long. To
warn for lines that are too long and could be fixed but allow long lines
without spaces, set the `stern` parameter to `true`.
For example (assuming normal behavior):
@ -582,6 +582,10 @@ 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.
Lines with link/image reference definitions are always exempted from this rule
(even in `strict` mode) because there is generally no way to split such lines
without breaking the URL.
Rationale: Extremely long lines can be difficult to work with in some editors.
More information: <https://cirosantilli.com/markdown-style-guide#line-wrapping>.

View file

@ -25,11 +25,11 @@ up into multiple lines. To set a different maximum length for headings, use
`code_block_line_length`
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` 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`.
line length. This allows you to 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` and an issue will be reported when any line is too long. To
warn for lines that are too long and could be fixed but allow long lines
without spaces, set the `stern` parameter to `true`.
For example (assuming normal behavior):
@ -50,5 +50,9 @@ 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.
Lines with link/image reference definitions are always exempted from this rule
(even in `strict` mode) because there is generally no way to split such lines
without breaking the URL.
Rationale: Extremely long lines can be difficult to work with in some editors.
More information: <https://cirosantilli.com/markdown-style-guide#line-wrapping>.

View file

@ -797,6 +797,7 @@ function getReferenceLinkImageData(lineMetadata) {
const shortcuts = new Set();
const definitions = new Map();
const duplicateDefinitions = [];
const definitionLineIndices = [];
// Define helper functions
const normalizeLabel = (s) => s.toLowerCase().trim().replace(/\s+/g, " ");
const exclusions = [];
@ -838,6 +839,11 @@ function getReferenceLinkImageData(lineMetadata) {
}
const labelLength = linkReferenceDefinitionMatch[0].length;
exclusions.push([ 0, lineOffsets[lineIndex], labelLength ]);
const hasDefinition = line.slice(labelLength).trim().length > 0;
definitionLineIndices.push(lineIndex);
if (!hasDefinition) {
definitionLineIndices.push(lineIndex + 1);
}
}
}
});
@ -914,7 +920,8 @@ function getReferenceLinkImageData(lineMetadata) {
references,
shortcuts,
definitions,
duplicateDefinitions
duplicateDefinitions,
definitionLineIndices
};
}
module.exports.getReferenceLinkImageData = getReferenceLinkImageData;

View file

@ -3,8 +3,8 @@
"use strict";
const { addErrorDetailIf, filterTokens, forEachHeading, forEachLine,
includesSorted, linkReferenceDefinitionRe } = require("../helpers");
const { lineMetadata } = require("./cache");
includesSorted } = require("../helpers");
const { lineMetadata, referenceLinkImageData } = require("./cache");
const longLineRePrefix = "^.{";
const longLineRePostfixRelaxed = "}.*\\s.*$";
@ -67,6 +67,7 @@ module.exports = {
linkOnlyLineNumbers.push(token.lineNumber);
}
});
const { definitionLineIndices } = referenceLinkImageData();
forEachLine(lineMetadata(), (line, lineIndex, inCode, onFence, inTable) => {
const lineNumber = lineIndex + 1;
const isHeading = includesSorted(headingLineNumbers, lineNumber);
@ -79,10 +80,10 @@ module.exports = {
if ((includeCodeBlocks || !inCode) &&
(includeTables || !inTable) &&
(includeHeadings || !isHeading) &&
!includesSorted(definitionLineIndices, lineIndex) &&
(strict ||
(!(stern && sternModeRe.test(line)) &&
!includesSorted(linkOnlyLineNumbers, lineNumber) &&
!linkReferenceDefinitionRe.test(line))) &&
!includesSorted(linkOnlyLineNumbers, lineNumber))) &&
lengthRe.test(line)) {
addErrorDetailIf(
onError,

View file

@ -0,0 +1,23 @@
# Long Lines Long Reference Definitions Stern
[Link][short-reference-definition]
[Link][long-reference-definition]
[Link][long-reference-definition-split]
![Image][long-reference-definition-image]
[short-reference-definition]: https://example.com/short
[long-reference-definition]: https://example.com/long/long/long/long/long/long/long/long/long/long/long/long/long
[long-reference-definition-split]:
https://example.com/long/long/long/long/long/long/long/long/long/long/long/long/long/long
[long-reference-definition-image]: https://example.com/long/long/long/long/long/long/long/long/long/long/long/long/long/image
<!-- markdownlint-disable-next-line link-image-reference-definitions -->
[long-reference-definition]: https://example.com/long/long/long/long/long/long/long/long/long/long/long/long/long
<!-- markdownlint-configure-file {
"line-length": {
"stern": true
}
} -->

View file

@ -0,0 +1,23 @@
# Long Lines Long Reference Definitions Strict
[Link][short-reference-definition]
[Link][long-reference-definition]
[Link][long-reference-definition-split]
![Image][long-reference-definition-image]
[short-reference-definition]: https://example.com/short
[long-reference-definition]: https://example.com/long/long/long/long/long/long/long/long/long/long/long/long/long
[long-reference-definition-split]:
https://example.com/long/long/long/long/long/long/long/long/long/long/long/long/long/long
[long-reference-definition-image]: https://example.com/long/long/long/long/long/long/long/long/long/long/long/long/long/image
<!-- markdownlint-disable-next-line link-image-reference-definitions -->
[long-reference-definition]: https://example.com/long/long/long/long/long/long/long/long/long/long/long/long/long
<!-- markdownlint-configure-file {
"line-length": {
"strict": true
}
} -->

View file

@ -0,0 +1,17 @@
# Long Lines Long Reference Definitions
[Link][short-reference-definition]
[Link][long-reference-definition]
[Link][long-reference-definition-split]
![Image][long-reference-definition-image]
[short-reference-definition]: https://example.com/short
[long-reference-definition]: https://example.com/long/long/long/long/long/long/long/long/long/long/long/long/long
[long-reference-definition-split]:
https://example.com/long/long/long/long/long/long/long/long/long/long/long/long/long/long
[long-reference-definition-image]: https://example.com/long/long/long/long/long/long/long/long/long/long/long/long/long/image
<!-- markdownlint-disable-next-line link-image-reference-definitions -->
[long-reference-definition]: https://example.com/long/long/long/long/long/long/long/long/long/long/long/long/long

View file

@ -27784,6 +27784,96 @@ Generated by [AVA](https://avajs.dev).
`,
}
## long-lines-long-reference-definitions-stern.md
> Snapshot 1
{
errors: [],
fixed: `# Long Lines Long Reference Definitions Stern␊
[Link][short-reference-definition]␊
[Link][long-reference-definition]␊
[Link][long-reference-definition-split]␊
![Image][long-reference-definition-image]␊
[short-reference-definition]: https://example.com/short␊
[long-reference-definition]: https://example.com/long/long/long/long/long/long/long/long/long/long/long/long/long␊
[long-reference-definition-split]:␊
https://example.com/long/long/long/long/long/long/long/long/long/long/long/long/long/long␊
[long-reference-definition-image]: https://example.com/long/long/long/long/long/long/long/long/long/long/long/long/long/image␊
<!-- markdownlint-disable-next-line link-image-reference-definitions -->
[long-reference-definition]: https://example.com/long/long/long/long/long/long/long/long/long/long/long/long/long␊
<!-- markdownlint-configure-file {␊
"line-length": {␊
"stern": true␊
}␊
} -->␊
`,
}
## long-lines-long-reference-definitions-strict.md
> Snapshot 1
{
errors: [],
fixed: `# Long Lines Long Reference Definitions Strict␊
[Link][short-reference-definition]␊
[Link][long-reference-definition]␊
[Link][long-reference-definition-split]␊
![Image][long-reference-definition-image]␊
[short-reference-definition]: https://example.com/short␊
[long-reference-definition]: https://example.com/long/long/long/long/long/long/long/long/long/long/long/long/long␊
[long-reference-definition-split]:␊
https://example.com/long/long/long/long/long/long/long/long/long/long/long/long/long/long␊
[long-reference-definition-image]: https://example.com/long/long/long/long/long/long/long/long/long/long/long/long/long/image␊
<!-- markdownlint-disable-next-line link-image-reference-definitions -->
[long-reference-definition]: https://example.com/long/long/long/long/long/long/long/long/long/long/long/long/long␊
<!-- markdownlint-configure-file {␊
"line-length": {␊
"strict": true␊
}␊
} -->␊
`,
}
## long-lines-long-reference-definitions.md
> Snapshot 1
{
errors: [],
fixed: `# Long Lines Long Reference Definitions␊
[Link][short-reference-definition]␊
[Link][long-reference-definition]␊
[Link][long-reference-definition-split]␊
![Image][long-reference-definition-image]␊
[short-reference-definition]: https://example.com/short␊
[long-reference-definition]: https://example.com/long/long/long/long/long/long/long/long/long/long/long/long/long␊
[long-reference-definition-split]:␊
https://example.com/long/long/long/long/long/long/long/long/long/long/long/long/long/long␊
[long-reference-definition-image]: https://example.com/long/long/long/long/long/long/long/long/long/long/long/long/long/image␊
<!-- markdownlint-disable-next-line link-image-reference-definitions -->
[long-reference-definition]: https://example.com/long/long/long/long/long/long/long/long/long/long/long/long/long␊
`,
}
## long-lines-short-code.md
> Snapshot 1