Skip to content

Commit 2c3e8c9

Browse files
committed
Update MD053/link-image-reference-definitions to handle multi-line references inside blockquotes (fixes #544).
1 parent 5544ea5 commit 2c3e8c9

6 files changed

+68
-21
lines changed

demo/markdownlint-browser.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ module.exports.listItemMarkerRe = /^([\s>]*)(?:[*+-]|\d+[.)])\s+/;
5252
module.exports.orderedListItemMarkerRe = /^[\s>]*0*(\d+)[.)]/;
5353
// Regular expression for all instances of emphasis markers
5454
const emphasisMarkersRe = /[_*]/g;
55+
// Regular expression for blockquote prefixes
56+
const blockquotePrefixRe = /^[>\s]*/;
57+
module.exports.blockquotePrefixRe = blockquotePrefixRe;
5558
// Regular expression for reference links (full, collapsed, and shortcut)
5659
const referenceLinkRe = /!?\\?\[((?:\[[^\]\0]*]|[^\]\0])*)](?:(?:\[([^\]\0]*)\])|([^(])|$)/g;
5760
// Regular expression for link reference definitions
@@ -763,6 +766,7 @@ function getReferenceLinkImageData(lineMetadata) {
763766
forEachLine(lineMetadata, (line, lineIndex, inCode) => {
764767
lineOffsets[lineIndex] = currentOffset;
765768
if (!inCode) {
769+
line = line.replace(blockquotePrefixRe, "");
766770
if (line.trim().length === 0) {
767771
// Allow RegExp to detect the end of a block
768772
line = "\0";
@@ -3582,9 +3586,8 @@ module.exports = {
35823586
"use strict";
35833587
// @ts-check
35843588

3585-
const { addErrorContext, isBlankLine } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
3589+
const { addErrorContext, blockquotePrefixRe, isBlankLine } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
35863590
const { flattenedLists } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
3587-
const quotePrefixRe = /^[>\s]*/;
35883591
module.exports = {
35893592
"names": ["MD032", "blanks-around-lists"],
35903593
"description": "Lists should be surrounded by blank lines",
@@ -3596,15 +3599,15 @@ module.exports = {
35963599
const firstIndex = list.open.map[0];
35973600
if (!isBlankLine(lines[firstIndex - 1])) {
35983601
const line = lines[firstIndex];
3599-
const quotePrefix = line.match(quotePrefixRe)[0].trimEnd();
3602+
const quotePrefix = line.match(blockquotePrefixRe)[0].trimEnd();
36003603
addErrorContext(onError, firstIndex + 1, line.trim(), null, null, null, {
36013604
"insertText": `${quotePrefix}\n`
36023605
});
36033606
}
36043607
const lastIndex = list.lastLineIndex - 1;
36053608
if (!isBlankLine(lines[lastIndex + 1])) {
36063609
const line = lines[lastIndex];
3607-
const quotePrefix = line.match(quotePrefixRe)[0].trimEnd();
3610+
const quotePrefix = line.match(blockquotePrefixRe)[0].trimEnd();
36083611
addErrorContext(onError, lastIndex + 1, line.trim(), null, null, null, {
36093612
"lineNumber": lastIndex + 2,
36103613
"insertText": `${quotePrefix}\n`

helpers/helpers.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ module.exports.orderedListItemMarkerRe = /^[\s>]*0*(\d+)[.)]/;
3030
// Regular expression for all instances of emphasis markers
3131
const emphasisMarkersRe = /[_*]/g;
3232

33+
// Regular expression for blockquote prefixes
34+
const blockquotePrefixRe = /^[>\s]*/;
35+
module.exports.blockquotePrefixRe = blockquotePrefixRe;
36+
3337
// Regular expression for reference links (full, collapsed, and shortcut)
3438
const referenceLinkRe =
3539
/!?\\?\[((?:\[[^\]\0]*]|[^\]\0])*)](?:(?:\[([^\]\0]*)\])|([^(])|$)/g;
@@ -794,6 +798,7 @@ function getReferenceLinkImageData(lineMetadata) {
794798
forEachLine(lineMetadata, (line, lineIndex, inCode) => {
795799
lineOffsets[lineIndex] = currentOffset;
796800
if (!inCode) {
801+
line = line.replace(blockquotePrefixRe, "");
797802
if (line.trim().length === 0) {
798803
// Allow RegExp to detect the end of a block
799804
line = "\0";

lib/md032.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
"use strict";
44

5-
const { addErrorContext, isBlankLine } = require("../helpers");
5+
const { addErrorContext, blockquotePrefixRe, isBlankLine } =
6+
require("../helpers");
67
const { flattenedLists } = require("./cache");
78

8-
const quotePrefixRe = /^[>\s]*/;
9-
109
module.exports = {
1110
"names": [ "MD032", "blanks-around-lists" ],
1211
"description": "Lists should be surrounded by blank lines",
@@ -18,7 +17,7 @@ module.exports = {
1817
const firstIndex = list.open.map[0];
1918
if (!isBlankLine(lines[firstIndex - 1])) {
2019
const line = lines[firstIndex];
21-
const quotePrefix = line.match(quotePrefixRe)[0].trimEnd();
20+
const quotePrefix = line.match(blockquotePrefixRe)[0].trimEnd();
2221
addErrorContext(
2322
onError,
2423
firstIndex + 1,
@@ -33,7 +32,7 @@ module.exports = {
3332
const lastIndex = list.lastLineIndex - 1;
3433
if (!isBlankLine(lines[lastIndex + 1])) {
3534
const line = lines[lastIndex];
36-
const quotePrefix = line.match(quotePrefixRe)[0].trimEnd();
35+
const quotePrefix = line.match(blockquotePrefixRe)[0].trimEnd();
3736
addErrorContext(
3837
onError,
3938
lastIndex + 1,

test/reference-links-and-images.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ line collapsed label][]
3434
Multi-line shortcut label: [multi line
3535
shortcut label]
3636

37+
> Multi-line full text: [multi
38+
> line][blockquote multi line full text]
39+
>
40+
> Multi-line full label: [text][blockquote multi
41+
> line full label]
42+
>
43+
> Multi-line collapsed label: [blockquote multi
44+
> line collapsed label][]
45+
>
46+
> Multi-line shortcut label: [blockquote multi line
47+
> shortcut label]
48+
>
49+
> > Multi-line shortcut label: [blockquote blockquote
50+
> > multi line shortcut label]
51+
3752
Dedicated line:
3853
[text][label]
3954

@@ -134,6 +149,11 @@ Missing[^2]
134149
[multi line full label]: https://example.com/multi-line-full-label
135150
[multi line collapsed label]: https://example.com/multi-line-collapsed-label
136151
[multi line shortcut label]: https://example.com/multi-line-shortcut-label
152+
[blockquote multi line full text]: https://example.com/blockquote-multi-line-full-text
153+
[blockquote multi line full label]: https://example.com/blockquote-multi-line-full-label
154+
[blockquote multi line collapsed label]: https://example.com/blockquote-multi-line-collapsed-label
155+
[blockquote multi line shortcut label]: https://example.com/blockquote-multi-line-shortcut-label
156+
[blockquote blockquote multi line shortcut label]: https://example.com/blockquote-blockquote-multi-line-shortcut-label
137157
[colon]: https://example.com/colon
138158
[multi-line-label]:
139159
https://example.com/multi-line-label

test/snapshots/markdownlint-test-scenarios.js.md

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33688,7 +33688,7 @@ Generated by [AVA](https://avajs.dev).
3368833688
15,
3368933689
],
3369033690
fixInfo: null,
33691-
lineNumber: 55,
33691+
lineNumber: 70,
3369233692
ruleDescription: 'Reference links and images should use a label that is defined',
3369333693
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md052',
3369433694
ruleNames: [
@@ -33704,7 +33704,7 @@ Generated by [AVA](https://avajs.dev).
3370433704
15,
3370533705
],
3370633706
fixInfo: null,
33707-
lineNumber: 57,
33707+
lineNumber: 72,
3370833708
ruleDescription: 'Reference links and images should use a label that is defined',
3370933709
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md052',
3371033710
ruleNames: [
@@ -33720,7 +33720,7 @@ Generated by [AVA](https://avajs.dev).
3372033720
14,
3372133721
],
3372233722
fixInfo: null,
33723-
lineNumber: 59,
33723+
lineNumber: 74,
3372433724
ruleDescription: 'Reference links and images should use a label that is defined',
3372533725
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md052',
3372633726
ruleNames: [
@@ -33736,7 +33736,7 @@ Generated by [AVA](https://avajs.dev).
3373633736
16,
3373733737
],
3373833738
fixInfo: null,
33739-
lineNumber: 104,
33739+
lineNumber: 119,
3374033740
ruleDescription: 'Reference links and images should use a label that is defined',
3374133741
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md052',
3374233742
ruleNames: [
@@ -33752,7 +33752,7 @@ Generated by [AVA](https://avajs.dev).
3375233752
16,
3375333753
],
3375433754
fixInfo: null,
33755-
lineNumber: 106,
33755+
lineNumber: 121,
3375633756
ruleDescription: 'Reference links and images should use a label that is defined',
3375733757
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md052',
3375833758
ruleNames: [
@@ -33768,7 +33768,7 @@ Generated by [AVA](https://avajs.dev).
3376833768
25,
3376933769
],
3377033770
fixInfo: null,
33771-
lineNumber: 176,
33771+
lineNumber: 196,
3377233772
ruleDescription: 'Reference links and images should use a label that is defined',
3377333773
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md052',
3377433774
ruleNames: [
@@ -33784,7 +33784,7 @@ Generated by [AVA](https://avajs.dev).
3378433784
10,
3378533785
],
3378633786
fixInfo: null,
33787-
lineNumber: 190,
33787+
lineNumber: 210,
3378833788
ruleDescription: 'Reference links and images should use a label that is defined',
3378933789
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md052',
3379033790
ruleNames: [
@@ -33802,7 +33802,7 @@ Generated by [AVA](https://avajs.dev).
3380233802
fixInfo: {
3380333803
deleteCount: -1,
3380433804
},
33805-
lineNumber: 152,
33805+
lineNumber: 172,
3380633806
ruleDescription: 'Link and image reference definitions should be needed',
3380733807
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md053',
3380833808
ruleNames: [
@@ -33820,7 +33820,7 @@ Generated by [AVA](https://avajs.dev).
3382033820
fixInfo: {
3382133821
deleteCount: -1,
3382233822
},
33823-
lineNumber: 155,
33823+
lineNumber: 175,
3382433824
ruleDescription: 'Link and image reference definitions should be needed',
3382533825
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md053',
3382633826
ruleNames: [
@@ -33838,7 +33838,7 @@ Generated by [AVA](https://avajs.dev).
3383833838
fixInfo: {
3383933839
deleteCount: -1,
3384033840
},
33841-
lineNumber: 158,
33841+
lineNumber: 178,
3384233842
ruleDescription: 'Link and image reference definitions should be needed',
3384333843
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md053',
3384433844
ruleNames: [
@@ -33854,7 +33854,7 @@ Generated by [AVA](https://avajs.dev).
3385433854
44,
3385533855
],
3385633856
fixInfo: null,
33857-
lineNumber: 160,
33857+
lineNumber: 180,
3385833858
ruleDescription: 'Link and image reference definitions should be needed',
3385933859
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md053',
3386033860
ruleNames: [
@@ -33870,7 +33870,7 @@ Generated by [AVA](https://avajs.dev).
3387033870
44,
3387133871
],
3387233872
fixInfo: null,
33873-
lineNumber: 163,
33873+
lineNumber: 183,
3387433874
ruleDescription: 'Link and image reference definitions should be needed',
3387533875
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md053',
3387633876
ruleNames: [
@@ -33915,6 +33915,21 @@ Generated by [AVA](https://avajs.dev).
3391533915
Multi-line shortcut label: [multi line␊
3391633916
shortcut label]␊
3391733917
33918+
> Multi-line full text: [multi␊
33919+
> line][blockquote multi line full text]␊
33920+
>␊
33921+
> Multi-line full label: [text][blockquote multi␊
33922+
> line full label]␊
33923+
>␊
33924+
> Multi-line collapsed label: [blockquote multi␊
33925+
> line collapsed label][]␊
33926+
>␊
33927+
> Multi-line shortcut label: [blockquote multi line␊
33928+
> shortcut label]␊
33929+
>␊
33930+
> > Multi-line shortcut label: [blockquote blockquote␊
33931+
> > multi line shortcut label]␊
33932+
3391833933
Dedicated line:␊
3391933934
[text][label]␊
3392033935
@@ -34015,6 +34030,11 @@ Generated by [AVA](https://avajs.dev).
3401534030
[multi line full label]: https://example.com/multi-line-full-label␊
3401634031
[multi line collapsed label]: https://example.com/multi-line-collapsed-label␊
3401734032
[multi line shortcut label]: https://example.com/multi-line-shortcut-label␊
34033+
[blockquote multi line full text]: https://example.com/blockquote-multi-line-full-text␊
34034+
[blockquote multi line full label]: https://example.com/blockquote-multi-line-full-label␊
34035+
[blockquote multi line collapsed label]: https://example.com/blockquote-multi-line-collapsed-label␊
34036+
[blockquote multi line shortcut label]: https://example.com/blockquote-multi-line-shortcut-label␊
34037+
[blockquote blockquote multi line shortcut label]: https://example.com/blockquote-blockquote-multi-line-shortcut-label␊
3401834038
[colon]: https://example.com/colon␊
3401934039
[multi-line-label]:␊
3402034040
https://example.com/multi-line-label␊
Binary file not shown.

0 commit comments

Comments
 (0)