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 shortcuts = new Set();
const definitions = new Map(); const definitions = new Map();
const duplicateDefinitions = []; const duplicateDefinitions = [];
const definitionLineIndices = [];
// Define helper functions // Define helper functions
const normalizeLabel = (s) => s.toLowerCase().trim().replace(/\s+/g, " "); const normalizeLabel = (s) => s.toLowerCase().trim().replace(/\s+/g, " ");
const exclusions = []; const exclusions = [];
@ -804,6 +805,11 @@ function getReferenceLinkImageData(lineMetadata) {
} }
const labelLength = linkReferenceDefinitionMatch[0].length; const labelLength = linkReferenceDefinitionMatch[0].length;
exclusions.push([0, lineOffsets[lineIndex], labelLength]); 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, references,
shortcuts, shortcuts,
definitions, definitions,
duplicateDefinitions duplicateDefinitions,
definitionLineIndices
}; };
} }
module.exports.getReferenceLinkImageData = getReferenceLinkImageData; module.exports.getReferenceLinkImageData = getReferenceLinkImageData;
@ -2832,8 +2839,8 @@ module.exports = {
"use strict"; "use strict";
// @ts-check // @ts-check
const { addErrorDetailIf, filterTokens, forEachHeading, forEachLine, includesSorted, linkReferenceDefinitionRe } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"); const { addErrorDetailIf, filterTokens, forEachHeading, forEachLine, includesSorted } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { lineMetadata } = __webpack_require__(/*! ./cache */ "../lib/cache.js"); const { lineMetadata, referenceLinkImageData } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
const longLineRePrefix = "^.{"; const longLineRePrefix = "^.{";
const longLineRePostfixRelaxed = "}.*\\s.*$"; const longLineRePostfixRelaxed = "}.*\\s.*$";
const longLineRePostfixStrict = "}.+$"; const longLineRePostfixStrict = "}.+$";
@ -2888,6 +2895,7 @@ module.exports = {
linkOnlyLineNumbers.push(token.lineNumber); linkOnlyLineNumbers.push(token.lineNumber);
} }
}); });
const { definitionLineIndices } = referenceLinkImageData();
forEachLine(lineMetadata(), (line, lineIndex, inCode, onFence, inTable) => { forEachLine(lineMetadata(), (line, lineIndex, inCode, onFence, inTable) => {
const lineNumber = lineIndex + 1; const lineNumber = lineIndex + 1;
const isHeading = includesSorted(headingLineNumbers, lineNumber); const isHeading = includesSorted(headingLineNumbers, lineNumber);
@ -2900,10 +2908,10 @@ module.exports = {
if ((includeCodeBlocks || !inCode) && if ((includeCodeBlocks || !inCode) &&
(includeTables || !inTable) && (includeTables || !inTable) &&
(includeHeadings || !isHeading) && (includeHeadings || !isHeading) &&
!includesSorted(definitionLineIndices, lineIndex) &&
(strict || (strict ||
(!(stern && sternModeRe.test(line)) && (!(stern && sternModeRe.test(line)) &&
!includesSorted(linkOnlyLineNumbers, lineNumber) && !includesSorted(linkOnlyLineNumbers, lineNumber))) &&
!linkReferenceDefinitionRe.test(line))) &&
lengthRe.test(line)) { lengthRe.test(line)) {
addErrorDetailIf(onError, lineNumber, length, line.length, null, null, [length + 1, line.length - length]); 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` `code_block_line_length`
This rule has an exception when 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 line length. This allows you to include items such as long URLs without being
being forced to break them in the middle. To disable this exception, set the forced to break them in the middle. To disable this exception, set the `strict`
`strict` parameter to `true` to report an issue when any line is too long. parameter to `true` and an issue will be reported when any line is too long. To
To warn for lines that are too long and could be fixed but allow lines without warn for lines that are too long and could be fixed but allow long lines
spaces, set the `stern` parameter to `true`. without spaces, set the `stern` parameter to `true`.
For example (assuming normal behavior): 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 requirement for document readability, and tentatively compatible with code
rules. Still, some languages do not lend themselves to short lines. 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. Rationale: Extremely long lines can be difficult to work with in some editors.
More information: <https://cirosantilli.com/markdown-style-guide#line-wrapping>. 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` `code_block_line_length`
This rule has an exception when 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 line length. This allows you to include items such as long URLs without being
being forced to break them in the middle. To disable this exception, set the forced to break them in the middle. To disable this exception, set the `strict`
`strict` parameter to `true` to report an issue when any line is too long. parameter to `true` and an issue will be reported when any line is too long. To
To warn for lines that are too long and could be fixed but allow lines without warn for lines that are too long and could be fixed but allow long lines
spaces, set the `stern` parameter to `true`. without spaces, set the `stern` parameter to `true`.
For example (assuming normal behavior): 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 requirement for document readability, and tentatively compatible with code
rules. Still, some languages do not lend themselves to short lines. 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. Rationale: Extremely long lines can be difficult to work with in some editors.
More information: <https://cirosantilli.com/markdown-style-guide#line-wrapping>. 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` `code_block_line_length`
This rule has an exception when 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 line length. This allows you to include items such as long URLs without being
being forced to break them in the middle. To disable this exception, set the forced to break them in the middle. To disable this exception, set the `strict`
`strict` parameter to `true` to report an issue when any line is too long. parameter to `true` and an issue will be reported when any line is too long. To
To warn for lines that are too long and could be fixed but allow lines without warn for lines that are too long and could be fixed but allow long lines
spaces, set the `stern` parameter to `true`. without spaces, set the `stern` parameter to `true`.
For example (assuming normal behavior): 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 requirement for document readability, and tentatively compatible with code
rules. Still, some languages do not lend themselves to short lines. 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. Rationale: Extremely long lines can be difficult to work with in some editors.
More information: <https://cirosantilli.com/markdown-style-guide#line-wrapping>. More information: <https://cirosantilli.com/markdown-style-guide#line-wrapping>.

View file

@ -797,6 +797,7 @@ function getReferenceLinkImageData(lineMetadata) {
const shortcuts = new Set(); const shortcuts = new Set();
const definitions = new Map(); const definitions = new Map();
const duplicateDefinitions = []; const duplicateDefinitions = [];
const definitionLineIndices = [];
// Define helper functions // Define helper functions
const normalizeLabel = (s) => s.toLowerCase().trim().replace(/\s+/g, " "); const normalizeLabel = (s) => s.toLowerCase().trim().replace(/\s+/g, " ");
const exclusions = []; const exclusions = [];
@ -838,6 +839,11 @@ function getReferenceLinkImageData(lineMetadata) {
} }
const labelLength = linkReferenceDefinitionMatch[0].length; const labelLength = linkReferenceDefinitionMatch[0].length;
exclusions.push([ 0, lineOffsets[lineIndex], labelLength ]); 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, references,
shortcuts, shortcuts,
definitions, definitions,
duplicateDefinitions duplicateDefinitions,
definitionLineIndices
}; };
} }
module.exports.getReferenceLinkImageData = getReferenceLinkImageData; module.exports.getReferenceLinkImageData = getReferenceLinkImageData;

View file

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