Update MD053/link-image-reference-definitions to handle multi-line references inside blockquotes (fixes #544).

This commit is contained in:
David Anson 2022-08-01 18:48:01 -07:00
parent 5544ea54d7
commit 2c3e8c938b
6 changed files with 68 additions and 21 deletions

View file

@ -52,6 +52,9 @@ module.exports.listItemMarkerRe = /^([\s>]*)(?:[*+-]|\d+[.)])\s+/;
module.exports.orderedListItemMarkerRe = /^[\s>]*0*(\d+)[.)]/;
// Regular expression for all instances of emphasis markers
const emphasisMarkersRe = /[_*]/g;
// Regular expression for blockquote prefixes
const blockquotePrefixRe = /^[>\s]*/;
module.exports.blockquotePrefixRe = blockquotePrefixRe;
// Regular expression for reference links (full, collapsed, and shortcut)
const referenceLinkRe = /!?\\?\[((?:\[[^\]\0]*]|[^\]\0])*)](?:(?:\[([^\]\0]*)\])|([^(])|$)/g;
// Regular expression for link reference definitions
@ -763,6 +766,7 @@ function getReferenceLinkImageData(lineMetadata) {
forEachLine(lineMetadata, (line, lineIndex, inCode) => {
lineOffsets[lineIndex] = currentOffset;
if (!inCode) {
line = line.replace(blockquotePrefixRe, "");
if (line.trim().length === 0) {
// Allow RegExp to detect the end of a block
line = "\0";
@ -3582,9 +3586,8 @@ module.exports = {
"use strict";
// @ts-check
const { addErrorContext, isBlankLine } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { addErrorContext, blockquotePrefixRe, isBlankLine } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { flattenedLists } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
const quotePrefixRe = /^[>\s]*/;
module.exports = {
"names": ["MD032", "blanks-around-lists"],
"description": "Lists should be surrounded by blank lines",
@ -3596,7 +3599,7 @@ module.exports = {
const firstIndex = list.open.map[0];
if (!isBlankLine(lines[firstIndex - 1])) {
const line = lines[firstIndex];
const quotePrefix = line.match(quotePrefixRe)[0].trimEnd();
const quotePrefix = line.match(blockquotePrefixRe)[0].trimEnd();
addErrorContext(onError, firstIndex + 1, line.trim(), null, null, null, {
"insertText": `${quotePrefix}\n`
});
@ -3604,7 +3607,7 @@ module.exports = {
const lastIndex = list.lastLineIndex - 1;
if (!isBlankLine(lines[lastIndex + 1])) {
const line = lines[lastIndex];
const quotePrefix = line.match(quotePrefixRe)[0].trimEnd();
const quotePrefix = line.match(blockquotePrefixRe)[0].trimEnd();
addErrorContext(onError, lastIndex + 1, line.trim(), null, null, null, {
"lineNumber": lastIndex + 2,
"insertText": `${quotePrefix}\n`

View file

@ -30,6 +30,10 @@ module.exports.orderedListItemMarkerRe = /^[\s>]*0*(\d+)[.)]/;
// Regular expression for all instances of emphasis markers
const emphasisMarkersRe = /[_*]/g;
// Regular expression for blockquote prefixes
const blockquotePrefixRe = /^[>\s]*/;
module.exports.blockquotePrefixRe = blockquotePrefixRe;
// Regular expression for reference links (full, collapsed, and shortcut)
const referenceLinkRe =
/!?\\?\[((?:\[[^\]\0]*]|[^\]\0])*)](?:(?:\[([^\]\0]*)\])|([^(])|$)/g;
@ -794,6 +798,7 @@ function getReferenceLinkImageData(lineMetadata) {
forEachLine(lineMetadata, (line, lineIndex, inCode) => {
lineOffsets[lineIndex] = currentOffset;
if (!inCode) {
line = line.replace(blockquotePrefixRe, "");
if (line.trim().length === 0) {
// Allow RegExp to detect the end of a block
line = "\0";

View file

@ -2,11 +2,10 @@
"use strict";
const { addErrorContext, isBlankLine } = require("../helpers");
const { addErrorContext, blockquotePrefixRe, isBlankLine } =
require("../helpers");
const { flattenedLists } = require("./cache");
const quotePrefixRe = /^[>\s]*/;
module.exports = {
"names": [ "MD032", "blanks-around-lists" ],
"description": "Lists should be surrounded by blank lines",
@ -18,7 +17,7 @@ module.exports = {
const firstIndex = list.open.map[0];
if (!isBlankLine(lines[firstIndex - 1])) {
const line = lines[firstIndex];
const quotePrefix = line.match(quotePrefixRe)[0].trimEnd();
const quotePrefix = line.match(blockquotePrefixRe)[0].trimEnd();
addErrorContext(
onError,
firstIndex + 1,
@ -33,7 +32,7 @@ module.exports = {
const lastIndex = list.lastLineIndex - 1;
if (!isBlankLine(lines[lastIndex + 1])) {
const line = lines[lastIndex];
const quotePrefix = line.match(quotePrefixRe)[0].trimEnd();
const quotePrefix = line.match(blockquotePrefixRe)[0].trimEnd();
addErrorContext(
onError,
lastIndex + 1,

View file

@ -34,6 +34,21 @@ line collapsed label][]
Multi-line shortcut label: [multi line
shortcut label]
> Multi-line full text: [multi
> line][blockquote multi line full text]
>
> Multi-line full label: [text][blockquote multi
> line full label]
>
> Multi-line collapsed label: [blockquote multi
> line collapsed label][]
>
> Multi-line shortcut label: [blockquote multi line
> shortcut label]
>
> > Multi-line shortcut label: [blockquote blockquote
> > multi line shortcut label]
Dedicated line:
[text][label]
@ -134,6 +149,11 @@ Missing[^2]
[multi line full label]: https://example.com/multi-line-full-label
[multi line collapsed label]: https://example.com/multi-line-collapsed-label
[multi line shortcut label]: https://example.com/multi-line-shortcut-label
[blockquote multi line full text]: https://example.com/blockquote-multi-line-full-text
[blockquote multi line full label]: https://example.com/blockquote-multi-line-full-label
[blockquote multi line collapsed label]: https://example.com/blockquote-multi-line-collapsed-label
[blockquote multi line shortcut label]: https://example.com/blockquote-multi-line-shortcut-label
[blockquote blockquote multi line shortcut label]: https://example.com/blockquote-blockquote-multi-line-shortcut-label
[colon]: https://example.com/colon
[multi-line-label]:
https://example.com/multi-line-label

View file

@ -33688,7 +33688,7 @@ Generated by [AVA](https://avajs.dev).
15,
],
fixInfo: null,
lineNumber: 55,
lineNumber: 70,
ruleDescription: 'Reference links and images should use a label that is defined',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md052',
ruleNames: [
@ -33704,7 +33704,7 @@ Generated by [AVA](https://avajs.dev).
15,
],
fixInfo: null,
lineNumber: 57,
lineNumber: 72,
ruleDescription: 'Reference links and images should use a label that is defined',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md052',
ruleNames: [
@ -33720,7 +33720,7 @@ Generated by [AVA](https://avajs.dev).
14,
],
fixInfo: null,
lineNumber: 59,
lineNumber: 74,
ruleDescription: 'Reference links and images should use a label that is defined',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md052',
ruleNames: [
@ -33736,7 +33736,7 @@ Generated by [AVA](https://avajs.dev).
16,
],
fixInfo: null,
lineNumber: 104,
lineNumber: 119,
ruleDescription: 'Reference links and images should use a label that is defined',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md052',
ruleNames: [
@ -33752,7 +33752,7 @@ Generated by [AVA](https://avajs.dev).
16,
],
fixInfo: null,
lineNumber: 106,
lineNumber: 121,
ruleDescription: 'Reference links and images should use a label that is defined',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md052',
ruleNames: [
@ -33768,7 +33768,7 @@ Generated by [AVA](https://avajs.dev).
25,
],
fixInfo: null,
lineNumber: 176,
lineNumber: 196,
ruleDescription: 'Reference links and images should use a label that is defined',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md052',
ruleNames: [
@ -33784,7 +33784,7 @@ Generated by [AVA](https://avajs.dev).
10,
],
fixInfo: null,
lineNumber: 190,
lineNumber: 210,
ruleDescription: 'Reference links and images should use a label that is defined',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md052',
ruleNames: [
@ -33802,7 +33802,7 @@ Generated by [AVA](https://avajs.dev).
fixInfo: {
deleteCount: -1,
},
lineNumber: 152,
lineNumber: 172,
ruleDescription: 'Link and image reference definitions should be needed',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md053',
ruleNames: [
@ -33820,7 +33820,7 @@ Generated by [AVA](https://avajs.dev).
fixInfo: {
deleteCount: -1,
},
lineNumber: 155,
lineNumber: 175,
ruleDescription: 'Link and image reference definitions should be needed',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md053',
ruleNames: [
@ -33838,7 +33838,7 @@ Generated by [AVA](https://avajs.dev).
fixInfo: {
deleteCount: -1,
},
lineNumber: 158,
lineNumber: 178,
ruleDescription: 'Link and image reference definitions should be needed',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md053',
ruleNames: [
@ -33854,7 +33854,7 @@ Generated by [AVA](https://avajs.dev).
44,
],
fixInfo: null,
lineNumber: 160,
lineNumber: 180,
ruleDescription: 'Link and image reference definitions should be needed',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md053',
ruleNames: [
@ -33870,7 +33870,7 @@ Generated by [AVA](https://avajs.dev).
44,
],
fixInfo: null,
lineNumber: 163,
lineNumber: 183,
ruleDescription: 'Link and image reference definitions should be needed',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md053',
ruleNames: [
@ -33915,6 +33915,21 @@ Generated by [AVA](https://avajs.dev).
Multi-line shortcut label: [multi line␊
shortcut label]␊
> Multi-line full text: [multi␊
> line][blockquote multi line full text]␊
>␊
> Multi-line full label: [text][blockquote multi␊
> line full label]␊
>␊
> Multi-line collapsed label: [blockquote multi␊
> line collapsed label][]␊
>␊
> Multi-line shortcut label: [blockquote multi line␊
> shortcut label]␊
>␊
> > Multi-line shortcut label: [blockquote blockquote␊
> > multi line shortcut label]␊
Dedicated line:␊
[text][label]␊
@ -34015,6 +34030,11 @@ Generated by [AVA](https://avajs.dev).
[multi line full label]: https://example.com/multi-line-full-label␊
[multi line collapsed label]: https://example.com/multi-line-collapsed-label␊
[multi line shortcut label]: https://example.com/multi-line-shortcut-label␊
[blockquote multi line full text]: https://example.com/blockquote-multi-line-full-text␊
[blockquote multi line full label]: https://example.com/blockquote-multi-line-full-label␊
[blockquote multi line collapsed label]: https://example.com/blockquote-multi-line-collapsed-label␊
[blockquote multi line shortcut label]: https://example.com/blockquote-multi-line-shortcut-label␊
[blockquote blockquote multi line shortcut label]: https://example.com/blockquote-blockquote-multi-line-shortcut-label␊
[colon]: https://example.com/colon␊
[multi-line-label]:␊
https://example.com/multi-line-label␊