Update MD052/reference-links-images to add ignored_labels parameter and default to ignoring GFM task list items (fixes #1524).

This commit is contained in:
David Anson 2025-04-27 22:36:07 -07:00
parent c413ac9a88
commit 328506e6c8
19 changed files with 357 additions and 12 deletions

View file

@ -28,3 +28,14 @@ so "shortcut" syntax is ignored by default. To include "shortcut" syntax, set
the `include_shortcut` parameter to `true`. Note that doing so produces warnings the `include_shortcut` parameter to `true`. Note that doing so produces warnings
for *all* text in the document that *could* be a shortcut. If bracketed text is for *all* text in the document that *could* be a shortcut. If bracketed text is
intentional, brackets can be escaped with the `\` character: `\[example\]`. intentional, brackets can be escaped with the `\` character: `\[example\]`.
If there are link labels that are deliberately unreferenced, they can be ignored
by setting the `ignored_labels` parameter to the list of strings to ignore. The
default value of this parameter ignores the checkbox syntax used by
[GitHub Flavored Markdown task list items][gfm-tasklist]:
```markdown
- [x] Checked task list item
```
[gfm-tasklist]: https://github.github.com/gfm/#task-list-items-extension-

View file

@ -17,9 +17,9 @@ reference has the corresponding label. The "full", "collapsed", and "shortcut"
formats are all supported. formats are all supported.
If there are reference definitions that are deliberately unreferenced, they can If there are reference definitions that are deliberately unreferenced, they can
be ignored by setting the `ignored_definitions` parameter. The default value of be ignored by setting the `ignored_definitions` parameter to the list of strings
this parameter ignores the following convention for adding non-HTML comments to to ignore. The default value of this parameter ignores the following convention
Markdown: for adding non-HTML comments to Markdown:
```markdown ```markdown
[//]: # (This behaves like a comment) [//]: # (This behaves like a comment)

View file

@ -2278,6 +2278,7 @@ Aliases: `reference-links-images`
Parameters: Parameters:
- `ignored_labels`: Ignored link labels (`string[]`, default `["x"]`)
- `shortcut_syntax`: Include shortcut syntax (`boolean`, default `false`) - `shortcut_syntax`: Include shortcut syntax (`boolean`, default `false`)
Links and images in Markdown can provide the link destination or image source Links and images in Markdown can provide the link destination or image source
@ -2311,6 +2312,17 @@ the `include_shortcut` parameter to `true`. Note that doing so produces warnings
for *all* text in the document that *could* be a shortcut. If bracketed text is for *all* text in the document that *could* be a shortcut. If bracketed text is
intentional, brackets can be escaped with the `\` character: `\[example\]`. intentional, brackets can be escaped with the `\` character: `\[example\]`.
If there are link labels that are deliberately unreferenced, they can be ignored
by setting the `ignored_labels` parameter to the list of strings to ignore. The
default value of this parameter ignores the checkbox syntax used by
[GitHub Flavored Markdown task list items][gfm-tasklist]:
```markdown
- [x] Checked task list item
```
[gfm-tasklist]: https://github.github.com/gfm/#task-list-items-extension-
<a name="md053"></a> <a name="md053"></a>
## `MD053` - Link and image reference definitions should be needed ## `MD053` - Link and image reference definitions should be needed
@ -2344,9 +2356,9 @@ reference has the corresponding label. The "full", "collapsed", and "shortcut"
formats are all supported. formats are all supported.
If there are reference definitions that are deliberately unreferenced, they can If there are reference definitions that are deliberately unreferenced, they can
be ignored by setting the `ignored_definitions` parameter. The default value of be ignored by setting the `ignored_definitions` parameter to the list of strings
this parameter ignores the following convention for adding non-HTML comments to to ignore. The default value of this parameter ignores the following convention
Markdown: for adding non-HTML comments to Markdown:
```markdown ```markdown
[//]: # (This behaves like a comment) [//]: # (This behaves like a comment)

View file

@ -6,6 +6,7 @@ Aliases: `reference-links-images`
Parameters: Parameters:
- `ignored_labels`: Ignored link labels (`string[]`, default `["x"]`)
- `shortcut_syntax`: Include shortcut syntax (`boolean`, default `false`) - `shortcut_syntax`: Include shortcut syntax (`boolean`, default `false`)
Links and images in Markdown can provide the link destination or image source Links and images in Markdown can provide the link destination or image source
@ -38,3 +39,14 @@ so "shortcut" syntax is ignored by default. To include "shortcut" syntax, set
the `include_shortcut` parameter to `true`. Note that doing so produces warnings the `include_shortcut` parameter to `true`. Note that doing so produces warnings
for *all* text in the document that *could* be a shortcut. If bracketed text is for *all* text in the document that *could* be a shortcut. If bracketed text is
intentional, brackets can be escaped with the `\` character: `\[example\]`. intentional, brackets can be escaped with the `\` character: `\[example\]`.
If there are link labels that are deliberately unreferenced, they can be ignored
by setting the `ignored_labels` parameter to the list of strings to ignore. The
default value of this parameter ignores the checkbox syntax used by
[GitHub Flavored Markdown task list items][gfm-tasklist]:
```markdown
- [x] Checked task list item
```
[gfm-tasklist]: https://github.github.com/gfm/#task-list-items-extension-

View file

@ -29,9 +29,9 @@ reference has the corresponding label. The "full", "collapsed", and "shortcut"
formats are all supported. formats are all supported.
If there are reference definitions that are deliberately unreferenced, they can If there are reference definitions that are deliberately unreferenced, they can
be ignored by setting the `ignored_definitions` parameter. The default value of be ignored by setting the `ignored_definitions` parameter to the list of strings
this parameter ignores the following convention for adding non-HTML comments to to ignore. The default value of this parameter ignores the following convention
Markdown: for adding non-HTML comments to Markdown:
```markdown ```markdown
[//]: # (This behaves like a comment) [//]: # (This behaves like a comment)

View file

@ -996,6 +996,10 @@ export interface ConfigurationStrict {
MD052?: MD052?:
| boolean | boolean
| { | {
/**
* Ignored link labels
*/
ignored_labels?: string[];
/** /**
* Include shortcut syntax * Include shortcut syntax
*/ */
@ -1007,6 +1011,10 @@ export interface ConfigurationStrict {
"reference-links-images"?: "reference-links-images"?:
| boolean | boolean
| { | {
/**
* Ignored link labels
*/
ignored_labels?: string[];
/** /**
* Include shortcut syntax * Include shortcut syntax
*/ */

View file

@ -13,6 +13,7 @@ export default {
"function": function MD052(params, onError) { "function": function MD052(params, onError) {
const { config, lines } = params; const { config, lines } = params;
const shortcutSyntax = config.shortcut_syntax || false; const shortcutSyntax = config.shortcut_syntax || false;
const ignoredLabels = new Set(config.ignored_labels || [ "x" ]);
const { definitions, references, shortcuts } = getReferenceLinkImageData(); const { definitions, references, shortcuts } = getReferenceLinkImageData();
const entries = shortcutSyntax ? const entries = shortcutSyntax ?
[ ...references.entries(), ...shortcuts.entries() ] : [ ...references.entries(), ...shortcuts.entries() ] :
@ -20,7 +21,7 @@ export default {
// Look for links/images that use an undefined link reference // Look for links/images that use an undefined link reference
for (const reference of entries) { for (const reference of entries) {
const [ label, datas ] = reference; const [ label, datas ] = reference;
if (!definitions.has(label)) { if (!definitions.has(label) && !ignoredLabels.has(label)) {
for (const data of datas) { for (const data of datas) {
const [ lineIndex, index, length ] = data; const [ lineIndex, index, length ] = data;
// Context will be incomplete if reporting for a multi-line link // Context will be incomplete if reporting for a multi-line link

View file

@ -275,6 +275,10 @@
// MD052/reference-links-images : Reference links and images should use a label that is defined : https://github.com/DavidAnson/markdownlint/blob/v0.37.4/doc/md052.md // MD052/reference-links-images : Reference links and images should use a label that is defined : https://github.com/DavidAnson/markdownlint/blob/v0.37.4/doc/md052.md
"MD052": { "MD052": {
// Ignored link labels
"ignored_labels": [
"x"
],
// Include shortcut syntax // Include shortcut syntax
"shortcut_syntax": false "shortcut_syntax": false
}, },

View file

@ -247,6 +247,9 @@ MD051:
# MD052/reference-links-images : Reference links and images should use a label that is defined : https://github.com/DavidAnson/markdownlint/blob/v0.37.4/doc/md052.md # MD052/reference-links-images : Reference links and images should use a label that is defined : https://github.com/DavidAnson/markdownlint/blob/v0.37.4/doc/md052.md
MD052: MD052:
# Ignored link labels
ignored_labels:
- "x"
# Include shortcut syntax # Include shortcut syntax
shortcut_syntax: false shortcut_syntax: false

View file

@ -493,6 +493,14 @@ for (const rule of rules) {
break; break;
case "MD052": case "MD052":
scheme.properties = { scheme.properties = {
"ignored_labels": {
"description": "Ignored link labels",
"type": "array",
"items": {
"type": "string"
},
"default": [ "x" ]
},
"shortcut_syntax": { "shortcut_syntax": {
"description": "Include shortcut syntax", "description": "Include shortcut syntax",
"type": "boolean", "type": "boolean",

View file

@ -1549,6 +1549,16 @@
], ],
"default": true, "default": true,
"properties": { "properties": {
"ignored_labels": {
"description": "Ignored link labels",
"type": "array",
"items": {
"type": "string"
},
"default": [
"x"
]
},
"shortcut_syntax": { "shortcut_syntax": {
"description": "Include shortcut syntax", "description": "Include shortcut syntax",
"type": "boolean", "type": "boolean",
@ -1565,6 +1575,16 @@
], ],
"default": true, "default": true,
"properties": { "properties": {
"ignored_labels": {
"description": "Ignored link labels",
"type": "array",
"items": {
"type": "string"
},
"default": [
"x"
]
},
"shortcut_syntax": { "shortcut_syntax": {
"description": "Include shortcut syntax", "description": "Include shortcut syntax",
"type": "boolean", "type": "boolean",

View file

@ -1549,6 +1549,16 @@
], ],
"default": true, "default": true,
"properties": { "properties": {
"ignored_labels": {
"description": "Ignored link labels",
"type": "array",
"items": {
"type": "string"
},
"default": [
"x"
]
},
"shortcut_syntax": { "shortcut_syntax": {
"description": "Include shortcut syntax", "description": "Include shortcut syntax",
"type": "boolean", "type": "boolean",
@ -1565,6 +1575,16 @@
], ],
"default": true, "default": true,
"properties": { "properties": {
"ignored_labels": {
"description": "Ignored link labels",
"type": "array",
"items": {
"type": "string"
},
"default": [
"x"
]
},
"shortcut_syntax": { "shortcut_syntax": {
"description": "Include shortcut syntax", "description": "Include shortcut syntax",
"type": "boolean", "type": "boolean",

View file

@ -908,7 +908,7 @@ test("readme", async(t) => {
}); });
test("validateJsonUsingConfigSchemaStrict", async(t) => { test("validateJsonUsingConfigSchemaStrict", async(t) => {
t.plan(199); t.plan(201);
// @ts-ignore // @ts-ignore
const ajv = new Ajv(ajvOptions); const ajv = new Ajv(ajvOptions);
const validateSchemaStrict = ajv.compile(configSchemaStrict); const validateSchemaStrict = ajv.compile(configSchemaStrict);

View file

@ -0,0 +1,19 @@
# Reference Links and Images (Ignored Labels Empty)
[full][full] {MD052}
[collapsed][] {MD052}
[shortcut] {MD052}
[invalid][invalid] {MD052}
- [ ] Unchecked task list item
- [x] Checked task list item {MD052}
<!-- markdownlint-configure-file {
"reference-links-images": {
"ignored_labels": [],
"shortcut_syntax": true
}
} -->

View file

@ -0,0 +1,23 @@
# Reference Links and Images (Ignored Labels)
[full][full]
[collapsed][]
[shortcut]
[invalid][invalid] {MD052}
- [ ] Unchecked task list item
- [x] Checked task list item {MD052}
<!-- markdownlint-configure-file {
"reference-links-images": {
"ignored_labels": [
"full",
"collapsed",
"shortcut"
],
"shortcut_syntax": true
}
} -->

View file

@ -84,6 +84,16 @@ Footnote[^1]
Missing[^2] {MD052} Missing[^2] {MD052}
## GitHub Flavored Markdown Task List Items
- [ ] Unchecked task list item
- [x] Checked task list item
- [x] alpha
- [ ] beta
- [x] charlie
- [ ] delta
## Valid Labels ## Valid Labels
[label]: https://example.com/label [label]: https://example.com/label

View file

@ -242,3 +242,8 @@ Text with a [^footnote] in it
[^footnote]: Footnote with an [embedded-reference][] in it [^footnote]: Footnote with an [embedded-reference][] in it
[embedded-reference]: https://example.com/embedded-reference [embedded-reference]: https://example.com/embedded-reference
## GitHub Flavored Markdown Task List Items
- [ ] Unchecked task list item
- [x] Checked task list item

View file

@ -47639,6 +47639,180 @@ Generated by [AVA](https://avajs.dev).
`, `,
} }
## reference-links-and-images-ignored-labels-empty.md
> Snapshot 1
{
errors: [
{
errorContext: '[full][full]',
errorDetail: 'Missing link or image reference definition: "full"',
errorRange: [
1,
12,
],
fixInfo: null,
lineNumber: 3,
ruleDescription: 'Reference links and images should use a label that is defined',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md052.md',
ruleNames: [
'MD052',
'reference-links-images',
],
},
{
errorContext: '[collapsed][]',
errorDetail: 'Missing link or image reference definition: "collapsed"',
errorRange: [
1,
13,
],
fixInfo: null,
lineNumber: 5,
ruleDescription: 'Reference links and images should use a label that is defined',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md052.md',
ruleNames: [
'MD052',
'reference-links-images',
],
},
{
errorContext: '[shortcut]',
errorDetail: 'Missing link or image reference definition: "shortcut"',
errorRange: [
1,
10,
],
fixInfo: null,
lineNumber: 7,
ruleDescription: 'Reference links and images should use a label that is defined',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md052.md',
ruleNames: [
'MD052',
'reference-links-images',
],
},
{
errorContext: '[invalid][invalid]',
errorDetail: 'Missing link or image reference definition: "invalid"',
errorRange: [
1,
18,
],
fixInfo: null,
lineNumber: 9,
ruleDescription: 'Reference links and images should use a label that is defined',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md052.md',
ruleNames: [
'MD052',
'reference-links-images',
],
},
{
errorContext: '[x]',
errorDetail: 'Missing link or image reference definition: "x"',
errorRange: [
3,
3,
],
fixInfo: null,
lineNumber: 12,
ruleDescription: 'Reference links and images should use a label that is defined',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md052.md',
ruleNames: [
'MD052',
'reference-links-images',
],
},
],
fixed: `# Reference Links and Images (Ignored Labels Empty)␊
[full][full] {MD052}␊
[collapsed][] {MD052}␊
[shortcut] {MD052}␊
[invalid][invalid] {MD052}␊
- [ ] Unchecked task list item␊
- [x] Checked task list item {MD052}␊
<!-- markdownlint-configure-file {␊
"reference-links-images": {␊
"ignored_labels": [],␊
"shortcut_syntax": true␊
}␊
} -->␊
`,
}
## reference-links-and-images-ignored-labels.md
> Snapshot 1
{
errors: [
{
errorContext: '[invalid][invalid]',
errorDetail: 'Missing link or image reference definition: "invalid"',
errorRange: [
1,
18,
],
fixInfo: null,
lineNumber: 9,
ruleDescription: 'Reference links and images should use a label that is defined',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md052.md',
ruleNames: [
'MD052',
'reference-links-images',
],
},
{
errorContext: '[x]',
errorDetail: 'Missing link or image reference definition: "x"',
errorRange: [
3,
3,
],
fixInfo: null,
lineNumber: 12,
ruleDescription: 'Reference links and images should use a label that is defined',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md052.md',
ruleNames: [
'MD052',
'reference-links-images',
],
},
],
fixed: `# Reference Links and Images (Ignored Labels)␊
[full][full]␊
[collapsed][]␊
[shortcut]␊
[invalid][invalid] {MD052}␊
- [ ] Unchecked task list item␊
- [x] Checked task list item {MD052}␊
<!-- markdownlint-configure-file {␊
"reference-links-images": {␊
"ignored_labels": [␊
"full",␊
"collapsed",␊
"shortcut"␊
],␊
"shortcut_syntax": true␊
}␊
} -->␊
`,
}
## reference-links-and-images-shortcuts.md ## reference-links-and-images-shortcuts.md
> Snapshot 1 > Snapshot 1
@ -47657,7 +47831,7 @@ Generated by [AVA](https://avajs.dev).
editColumn: 7, editColumn: 7,
insertText: '<https://example.com/footnote>', insertText: '<https://example.com/footnote>',
}, },
lineNumber: 106, lineNumber: 116,
ruleDescription: 'Bare URL used', ruleDescription: 'Bare URL used',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md034.md', ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md034.md',
ruleNames: [ ruleNames: [
@ -47848,6 +48022,16 @@ Generated by [AVA](https://avajs.dev).
Missing[^2] {MD052}␊ Missing[^2] {MD052}␊
## GitHub Flavored Markdown Task List Items␊
- [ ] Unchecked task list item␊
- [x] Checked task list item␊
- [x] alpha␊
- [ ] beta␊
- [x] charlie␊
- [ ] delta␊
## Valid Labels␊ ## Valid Labels␊
[label]: https://example.com/label␊ [label]: https://example.com/label␊
@ -48359,6 +48543,11 @@ Generated by [AVA](https://avajs.dev).
[^footnote]: Footnote with an [embedded-reference][] in it␊ [^footnote]: Footnote with an [embedded-reference][] in it␊
[embedded-reference]: https://example.com/embedded-reference␊ [embedded-reference]: https://example.com/embedded-reference␊
## GitHub Flavored Markdown Task List Items␊
- [ ] Unchecked task list item␊
- [x] Checked task list item␊
`, `,
} }