Skip to content

Commit 063310e

Browse files
committed
Update MD054/link-image-style to split reference parameter into full/collapsed/shortcut parameters (fixes #918).
1 parent 4390715 commit 063310e

21 files changed

+2804
-301
lines changed

demo/markdownlint-browser.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6633,8 +6633,10 @@ module.exports = {
66336633
config = params.config;
66346634
var autolink = config.autolink === undefined || !!config.autolink;
66356635
var inline = config.inline === undefined || !!config.inline;
6636-
var reference = config.reference === undefined || !!config.reference;
6637-
if (autolink && inline && reference) {
6636+
var full = config.full === undefined || !!config.full;
6637+
var collapsed = config.collapsed === undefined || !!config.collapsed;
6638+
var shortcut = config.shortcut === undefined || !!config.shortcut;
6639+
if (autolink && inline && full && collapsed && shortcut) {
66386640
// Everything allowed, nothing to check
66396641
return;
66406642
}
@@ -6671,11 +6673,15 @@ module.exports = {
66716673
// link kind is an inline link
66726674
isError = !inline;
66736675
} else {
6674-
// link kind is a reference link
6675-
var referenceLabel = getTokenTextByType(descendents, "referenceString") || label;
6676-
var definition = definitions.get(referenceLabel);
6676+
// link kind is a full/collapsed/shortcut reference link
6677+
var isShortcut = !children.some(function (t) {
6678+
return t.type === "reference";
6679+
});
6680+
var referenceString = getTokenTextByType(descendents, "referenceString");
6681+
var isCollapsed = referenceString === null;
6682+
var definition = definitions.get(referenceString || label);
66776683
destination = definition && definition[1];
6678-
isError = !reference && destination;
6684+
isError = destination && (isShortcut ? !shortcut : isCollapsed ? !collapsed : !full);
66796685
}
66806686
}
66816687
if (isError) {

doc-build/md054.md

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
Links and images in Markdown can provide the link destination or image source at
22
the time of use or can use a label to reference a definition elsewhere in the
3-
document. The reference format is convenient for keeping paragraph text
4-
clutter-free and makes it easy to reuse the same URL in multiple places.
3+
document. The three reference formats are convenient for keeping paragraph text
4+
clutter-free and make it easy to reuse the same URL in multiple places.
55

6-
By default, this rule allows all link/image styles. It is possible to disable
7-
one or more of those styles.
6+
By default, this rule allows all link/image styles.
87

98
Setting the `autolink` parameter to `false` disables autolinks:
109

@@ -20,20 +19,34 @@ Setting the `inline` parameter to `false` disables inline links and images:
2019
![image](https://example.com)
2120
```
2221

23-
Setting the `reference` parameter to `false` disables full, collapsed, and
24-
shortcut reference links and images:
22+
Setting the `full` parameter to `false` disables full reference links and
23+
images:
2524

2625
```markdown
2726
[link][url]
2827

29-
[url][]
28+
![image][url]
3029

31-
[url]
30+
[url]: https://example.com
31+
```
3232

33-
![image][url]
33+
Setting the `collapsed` parameter to `false` disables collapsed reference links
34+
and images:
35+
36+
```markdown
37+
[url][]
3438

3539
![url][]
3640

41+
[url]: https://example.com
42+
```
43+
44+
Setting the `shortcut` parameter to `false` disables shortcut reference links
45+
and images:
46+
47+
```markdown
48+
[url]
49+
3750
![url]
3851

3952
[url]: https://example.com
@@ -43,12 +56,12 @@ To fix violations of this rule, change the link or image to use an allowed
4356
style. This rule can automatically fix violations when a link or image can be
4457
converted to the `inline` style (preferred) or a link can be converted to the
4558
`autolink` style (which does not support images and must be an absolute URL).
46-
This rule does not fix scenarios that require converting a link or image to the
47-
`reference` style because that involves naming the reference and knowing where
48-
in the document to insert it.
59+
This rule does *not* fix scenarios that require converting a link or image to
60+
the `full`, `collapsed`, or `shortcut` reference styles because that involves
61+
naming the reference and determining where to insert it in the document.
4962

5063
Rationale: Consistent formatting makes it easier to understand a document.
5164
Autolinks are concise, but appear as URLs which can be long and confusing.
5265
Inline links and images can include descriptive text, but take up more space in
5366
Markdown form. Reference links and images can be easier to read and manipulate
54-
in Markdown form, but require editing two locations.
67+
in Markdown form, but require a separate link reference definition.

doc/Rules.md

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,18 +2234,21 @@ Aliases: `link-image-style`
22342234
Parameters:
22352235

22362236
- `autolink`: Allow autolinks (`boolean`, default `true`)
2237+
- `collapsed`: Allow collapsed reference links and images (`boolean`, default
2238+
`true`)
2239+
- `full`: Allow full reference links and images (`boolean`, default `true`)
22372240
- `inline`: Allow inline links and images (`boolean`, default `true`)
2238-
- `reference`: Allow reference links and images (`boolean`, default `true`)
2241+
- `shortcut`: Allow shortcut reference links and images (`boolean`, default
2242+
`true`)
22392243

22402244
Fixable: Some violations can be fixed by tooling
22412245

22422246
Links and images in Markdown can provide the link destination or image source at
22432247
the time of use or can use a label to reference a definition elsewhere in the
2244-
document. The reference format is convenient for keeping paragraph text
2245-
clutter-free and makes it easy to reuse the same URL in multiple places.
2248+
document. The three reference formats are convenient for keeping paragraph text
2249+
clutter-free and make it easy to reuse the same URL in multiple places.
22462250

2247-
By default, this rule allows all link/image styles. It is possible to disable
2248-
one or more of those styles.
2251+
By default, this rule allows all link/image styles.
22492252

22502253
Setting the `autolink` parameter to `false` disables autolinks:
22512254

@@ -2261,20 +2264,34 @@ Setting the `inline` parameter to `false` disables inline links and images:
22612264
![image](https://example.com)
22622265
```
22632266

2264-
Setting the `reference` parameter to `false` disables full, collapsed, and
2265-
shortcut reference links and images:
2267+
Setting the `full` parameter to `false` disables full reference links and
2268+
images:
22662269

22672270
```markdown
22682271
[link][url]
22692272

2270-
[url][]
2273+
![image][url]
22712274

2272-
[url]
2275+
[url]: https://example.com
2276+
```
22732277

2274-
![image][url]
2278+
Setting the `collapsed` parameter to `false` disables collapsed reference links
2279+
and images:
2280+
2281+
```markdown
2282+
[url][]
22752283

22762284
![url][]
22772285

2286+
[url]: https://example.com
2287+
```
2288+
2289+
Setting the `shortcut` parameter to `false` disables shortcut reference links
2290+
and images:
2291+
2292+
```markdown
2293+
[url]
2294+
22782295
![url]
22792296

22802297
[url]: https://example.com
@@ -2284,15 +2301,15 @@ To fix violations of this rule, change the link or image to use an allowed
22842301
style. This rule can automatically fix violations when a link or image can be
22852302
converted to the `inline` style (preferred) or a link can be converted to the
22862303
`autolink` style (which does not support images and must be an absolute URL).
2287-
This rule does not fix scenarios that require converting a link or image to the
2288-
`reference` style because that involves naming the reference and knowing where
2289-
in the document to insert it.
2304+
This rule does *not* fix scenarios that require converting a link or image to
2305+
the `full`, `collapsed`, or `shortcut` reference styles because that involves
2306+
naming the reference and determining where to insert it in the document.
22902307

22912308
Rationale: Consistent formatting makes it easier to understand a document.
22922309
Autolinks are concise, but appear as URLs which can be long and confusing.
22932310
Inline links and images can include descriptive text, but take up more space in
22942311
Markdown form. Reference links and images can be easier to read and manipulate
2295-
in Markdown form, but require editing two locations.
2312+
in Markdown form, but require a separate link reference definition.
22962313

22972314
<!-- markdownlint-configure-file {
22982315
"no-inline-html": {

doc/md054.md

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,21 @@ Aliases: `link-image-style`
77
Parameters:
88

99
- `autolink`: Allow autolinks (`boolean`, default `true`)
10+
- `collapsed`: Allow collapsed reference links and images (`boolean`, default
11+
`true`)
12+
- `full`: Allow full reference links and images (`boolean`, default `true`)
1013
- `inline`: Allow inline links and images (`boolean`, default `true`)
11-
- `reference`: Allow reference links and images (`boolean`, default `true`)
14+
- `shortcut`: Allow shortcut reference links and images (`boolean`, default
15+
`true`)
1216

1317
Fixable: Some violations can be fixed by tooling
1418

1519
Links and images in Markdown can provide the link destination or image source at
1620
the time of use or can use a label to reference a definition elsewhere in the
17-
document. The reference format is convenient for keeping paragraph text
18-
clutter-free and makes it easy to reuse the same URL in multiple places.
21+
document. The three reference formats are convenient for keeping paragraph text
22+
clutter-free and make it easy to reuse the same URL in multiple places.
1923

20-
By default, this rule allows all link/image styles. It is possible to disable
21-
one or more of those styles.
24+
By default, this rule allows all link/image styles.
2225

2326
Setting the `autolink` parameter to `false` disables autolinks:
2427

@@ -34,20 +37,34 @@ Setting the `inline` parameter to `false` disables inline links and images:
3437
![image](https://example.com)
3538
```
3639

37-
Setting the `reference` parameter to `false` disables full, collapsed, and
38-
shortcut reference links and images:
40+
Setting the `full` parameter to `false` disables full reference links and
41+
images:
3942

4043
```markdown
4144
[link][url]
4245

43-
[url][]
46+
![image][url]
4447

45-
[url]
48+
[url]: https://example.com
49+
```
4650

47-
![image][url]
51+
Setting the `collapsed` parameter to `false` disables collapsed reference links
52+
and images:
53+
54+
```markdown
55+
[url][]
4856

4957
![url][]
5058

59+
[url]: https://example.com
60+
```
61+
62+
Setting the `shortcut` parameter to `false` disables shortcut reference links
63+
and images:
64+
65+
```markdown
66+
[url]
67+
5168
![url]
5269

5370
[url]: https://example.com
@@ -57,12 +74,12 @@ To fix violations of this rule, change the link or image to use an allowed
5774
style. This rule can automatically fix violations when a link or image can be
5875
converted to the `inline` style (preferred) or a link can be converted to the
5976
`autolink` style (which does not support images and must be an absolute URL).
60-
This rule does not fix scenarios that require converting a link or image to the
61-
`reference` style because that involves naming the reference and knowing where
62-
in the document to insert it.
77+
This rule does *not* fix scenarios that require converting a link or image to
78+
the `full`, `collapsed`, or `shortcut` reference styles because that involves
79+
naming the reference and determining where to insert it in the document.
6380

6481
Rationale: Consistent formatting makes it easier to understand a document.
6582
Autolinks are concise, but appear as URLs which can be long and confusing.
6683
Inline links and images can include descriptive text, but take up more space in
6784
Markdown form. Reference links and images can be easier to read and manipulate
68-
in Markdown form, but require editing two locations.
85+
in Markdown form, but require a separate link reference definition.

lib/configuration.d.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,9 +1009,17 @@ export interface Configuration {
10091009
*/
10101010
inline?: boolean;
10111011
/**
1012-
* Allow reference links and images
1012+
* Allow full reference links and images
10131013
*/
1014-
reference?: boolean;
1014+
full?: boolean;
1015+
/**
1016+
* Allow collapsed reference links and images
1017+
*/
1018+
collapsed?: boolean;
1019+
/**
1020+
* Allow shortcut reference links and images
1021+
*/
1022+
shortcut?: boolean;
10151023
};
10161024
/**
10171025
* MD054/link-image-style : Link and image style : https://github.com/DavidAnson/markdownlint/blob/v0.31.1/doc/md054.md
@@ -1028,9 +1036,17 @@ export interface Configuration {
10281036
*/
10291037
inline?: boolean;
10301038
/**
1031-
* Allow reference links and images
1039+
* Allow full reference links and images
1040+
*/
1041+
full?: boolean;
1042+
/**
1043+
* Allow collapsed reference links and images
1044+
*/
1045+
collapsed?: boolean;
1046+
/**
1047+
* Allow shortcut reference links and images
10321048
*/
1033-
reference?: boolean;
1049+
shortcut?: boolean;
10341050
};
10351051
/**
10361052
* headings : MD001, MD003, MD018, MD019, MD020, MD021, MD022, MD023, MD024, MD025, MD026, MD036, MD041, MD043

lib/md054.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ module.exports = {
2929
const { parsers, config } = params;
3030
const autolink = (config.autolink === undefined) || !!config.autolink;
3131
const inline = (config.inline === undefined) || !!config.inline;
32-
const reference = (config.reference === undefined) || !!config.reference;
33-
if (autolink && inline && reference) {
32+
const full = (config.full === undefined) || !!config.full;
33+
const collapsed = (config.collapsed === undefined) || !!config.collapsed;
34+
const shortcut = (config.shortcut === undefined) || !!config.shortcut;
35+
if (autolink && inline && full && collapsed && shortcut) {
3436
// Everything allowed, nothing to check
3537
return;
3638
}
@@ -62,12 +64,14 @@ module.exports = {
6264
// link kind is an inline link
6365
isError = !inline;
6466
} else {
65-
// link kind is a reference link
66-
const referenceLabel =
67-
getTokenTextByType(descendents, "referenceString") || label;
68-
const definition = definitions.get(referenceLabel);
67+
// link kind is a full/collapsed/shortcut reference link
68+
const isShortcut = !children.some((t) => t.type === "reference");
69+
const referenceString = getTokenTextByType(descendents, "referenceString");
70+
const isCollapsed = (referenceString === null);
71+
const definition = definitions.get(referenceString || label);
6972
destination = definition && definition[1];
70-
isError = !reference && destination;
73+
isError = destination &&
74+
(isShortcut ? !shortcut : (isCollapsed ? !collapsed : !full));
7175
}
7276
}
7377
if (isError) {

schema/.markdownlint.jsonc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,11 @@
285285
"autolink": true,
286286
// Allow inline links and images
287287
"inline": true,
288-
// Allow reference links and images
289-
"reference": true
288+
// Allow full reference links and images
289+
"full": true,
290+
// Allow collapsed reference links and images
291+
"collapsed": true,
292+
// Allow shortcut reference links and images
293+
"shortcut": true
290294
}
291295
}

schema/.markdownlint.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,5 +256,9 @@ MD054:
256256
autolink: true
257257
# Allow inline links and images
258258
inline: true
259-
# Allow reference links and images
260-
reference: true
259+
# Allow full reference links and images
260+
full: true
261+
# Allow collapsed reference links and images
262+
collapsed: true
263+
# Allow shortcut reference links and images
264+
shortcut: true

schema/build-config-schema.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,8 +509,18 @@ for (const rule of rules) {
509509
"type": "boolean",
510510
"default": true
511511
},
512-
"reference": {
513-
"description": "Allow reference links and images",
512+
"full": {
513+
"description": "Allow full reference links and images",
514+
"type": "boolean",
515+
"default": true
516+
},
517+
"collapsed": {
518+
"description": "Allow collapsed reference links and images",
519+
"type": "boolean",
520+
"default": true
521+
},
522+
"shortcut": {
523+
"description": "Allow shortcut reference links and images",
514524
"type": "boolean",
515525
"default": true
516526
}

0 commit comments

Comments
 (0)