Skip to content

Commit

Permalink
Update MD051/link-fragments to also look for a fragment matching the …
Browse files Browse the repository at this point in the history
…URI-encoded link definition (implicit behavior in previous version via markdown-it) (fixes #954, fixes #955).
  • Loading branch information
DavidAnson committed Sep 7, 2023
1 parent b7ef571 commit 1ddee6b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 37 deletions.
15 changes: 9 additions & 6 deletions demo/markdownlint-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6328,7 +6328,10 @@ module.exports = {
try {
var _loop = function _loop() {
var definition = _step4.value;
if (definition.text.length > 1 && definition.text.startsWith("#") && !fragments.has(definition.text)) {
var endColumn = definition.endColumn,
startColumn = definition.startColumn,
text = definition.text;
if (text.length > 1 && text.startsWith("#") && !fragments.has(text) && !fragments.has("#".concat(encodeURIComponent(text.slice(1))))) {
// eslint-disable-next-line no-undef-init
var context = undefined;
// eslint-disable-next-line no-undef-init
Expand All @@ -6339,18 +6342,18 @@ module.exports = {
context = link.text;
range = [link.startColumn, link.endColumn - link.startColumn];
fixInfo = {
"editColumn": definition.startColumn,
"deleteCount": definition.endColumn - definition.startColumn
"editColumn": startColumn,
"deleteCount": endColumn - startColumn
};
}
var definitionTextLower = definition.text.toLowerCase();
var textLower = text.toLowerCase();
var mixedCaseKey = _toConsumableArray(fragments.keys()).find(function (key) {
return definitionTextLower === key.toLowerCase();
return textLower === key.toLowerCase();
});
if (mixedCaseKey) {
// @ts-ignore
(fixInfo || {}).insertText = mixedCaseKey;
addErrorDetailIf(onError, link.startLine, mixedCaseKey, definition.text, undefined, context, range, fixInfo);
addErrorDetailIf(onError, link.startLine, mixedCaseKey, text, undefined, context, range, fixInfo);
} else {
addError(onError, link.startLine, undefined, context, range);
}
Expand Down
18 changes: 10 additions & 8 deletions lib/md051.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@ module.exports = {
for (const link of links) {
const definitions = filterByTypes(link.children, [ definitionType ]);
for (const definition of definitions) {
const { endColumn, startColumn, text } = definition;
if (
(definition.text.length > 1) &&
definition.text.startsWith("#") &&
!fragments.has(definition.text)
(text.length > 1) &&
text.startsWith("#") &&
!fragments.has(text) &&
!fragments.has(`#${encodeURIComponent(text.slice(1))}`)
) {
// eslint-disable-next-line no-undef-init
let context = undefined;
Expand All @@ -114,21 +116,21 @@ module.exports = {
context = link.text;
range = [ link.startColumn, link.endColumn - link.startColumn ];
fixInfo = {
"editColumn": definition.startColumn,
"deleteCount": definition.endColumn - definition.startColumn
"editColumn": startColumn,
"deleteCount": endColumn - startColumn
};
}
const definitionTextLower = definition.text.toLowerCase();
const textLower = text.toLowerCase();
const mixedCaseKey = [ ...fragments.keys() ]
.find((key) => definitionTextLower === key.toLowerCase());
.find((key) => textLower === key.toLowerCase());
if (mixedCaseKey) {
// @ts-ignore
(fixInfo || {}).insertText = mixedCaseKey;
addErrorDetailIf(
onError,
link.startLine,
mixedCaseKey,
definition.text,
text,
undefined,
context,
range,
Expand Down
10 changes: 10 additions & 0 deletions test/link-fragments.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,16 @@

[Valid](#valid-heading-about-lh%C3%B4pitals-rule)

[Valid](#valid-heading-about-lhôpitals-rule)

[Valid](#en-t%C3%AAte-valide-dans-fran%C3%A7ais-pour-v%C3%A9rification)

[Valid](#en-tête-valide-dans-français-pour-vérification)

[Valid](#%E6%A0%87%E9%A2%98)

[Valid](#标题)

[Valid](#valid-heading-is-a-link)

[Valid](#valid-heading-has-a-link)
Expand Down Expand Up @@ -137,6 +145,8 @@ Text

### En-tête Valide Dans Français Pour Vérification

### 标题

### [Valid Heading Is a Link](https://example.com)

### Valid Heading [Has a Link](https://example.com)
Expand Down
56 changes: 33 additions & 23 deletions test/snapshots/markdownlint-test-scenarios.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -23969,7 +23969,7 @@ Generated by [AVA](https://avajs.dev).
37,
],
fixInfo: null,
lineNumber: 172,
lineNumber: 182,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md051.md',
ruleNames: [
Expand All @@ -23985,7 +23985,7 @@ Generated by [AVA](https://avajs.dev).
31,
],
fixInfo: null,
lineNumber: 174,
lineNumber: 184,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md051.md',
ruleNames: [
Expand All @@ -24001,7 +24001,7 @@ Generated by [AVA](https://avajs.dev).
36,
],
fixInfo: null,
lineNumber: 176,
lineNumber: 186,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md051.md',
ruleNames: [
Expand All @@ -24017,7 +24017,7 @@ Generated by [AVA](https://avajs.dev).
28,
],
fixInfo: null,
lineNumber: 178,
lineNumber: 188,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md051.md',
ruleNames: [
Expand All @@ -24033,7 +24033,7 @@ Generated by [AVA](https://avajs.dev).
18,
],
fixInfo: null,
lineNumber: 180,
lineNumber: 190,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md051.md',
ruleNames: [
Expand All @@ -24053,7 +24053,7 @@ Generated by [AVA](https://avajs.dev).
editColumn: 11,
insertText: '#HREFandID',
},
lineNumber: 182,
lineNumber: 192,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md051.md',
ruleNames: [
Expand All @@ -24069,7 +24069,7 @@ Generated by [AVA](https://avajs.dev).
34,
],
fixInfo: null,
lineNumber: 184,
lineNumber: 194,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md051.md',
ruleNames: [
Expand All @@ -24085,7 +24085,7 @@ Generated by [AVA](https://avajs.dev).
34,
],
fixInfo: null,
lineNumber: 186,
lineNumber: 196,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md051.md',
ruleNames: [
Expand All @@ -24101,7 +24101,7 @@ Generated by [AVA](https://avajs.dev).
39,
],
fixInfo: null,
lineNumber: 188,
lineNumber: 198,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md051.md',
ruleNames: [
Expand All @@ -24114,7 +24114,7 @@ Generated by [AVA](https://avajs.dev).
errorDetail: null,
errorRange: null,
fixInfo: null,
lineNumber: 190,
lineNumber: 200,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md051.md',
ruleNames: [
Expand All @@ -24130,7 +24130,7 @@ Generated by [AVA](https://avajs.dev).
28,
],
fixInfo: null,
lineNumber: 195,
lineNumber: 205,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md051.md',
ruleNames: [
Expand All @@ -24150,7 +24150,7 @@ Generated by [AVA](https://avajs.dev).
editColumn: 9,
insertText: '#valid-fragments',
},
lineNumber: 199,
lineNumber: 209,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md051.md',
ruleNames: [
Expand All @@ -24170,7 +24170,7 @@ Generated by [AVA](https://avajs.dev).
editColumn: 12,
insertText: '#namedlink',
},
lineNumber: 201,
lineNumber: 211,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md051.md',
ruleNames: [
Expand All @@ -24183,7 +24183,7 @@ Generated by [AVA](https://avajs.dev).
errorDetail: 'Expected: #namedlink; Actual: #NAMEDLINK',
errorRange: null,
fixInfo: null,
lineNumber: 203,
lineNumber: 213,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md051.md',
ruleNames: [
Expand All @@ -24203,7 +24203,7 @@ Generated by [AVA](https://avajs.dev).
editColumn: 13,
insertText: '#idlink',
},
lineNumber: 208,
lineNumber: 218,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md051.md',
ruleNames: [
Expand All @@ -24219,7 +24219,7 @@ Generated by [AVA](https://avajs.dev).
26,
],
fixInfo: null,
lineNumber: 243,
lineNumber: 253,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md051.md',
ruleNames: [
Expand All @@ -24235,7 +24235,7 @@ Generated by [AVA](https://avajs.dev).
26,
],
fixInfo: null,
lineNumber: 245,
lineNumber: 255,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md051.md',
ruleNames: [
Expand All @@ -24251,7 +24251,7 @@ Generated by [AVA](https://avajs.dev).
20,
],
fixInfo: null,
lineNumber: 247,
lineNumber: 257,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md051.md',
ruleNames: [
Expand All @@ -24267,7 +24267,7 @@ Generated by [AVA](https://avajs.dev).
23,
],
fixInfo: null,
lineNumber: 249,
lineNumber: 259,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md051.md',
ruleNames: [
Expand All @@ -24283,7 +24283,7 @@ Generated by [AVA](https://avajs.dev).
22,
],
fixInfo: null,
lineNumber: 251,
lineNumber: 261,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md051.md',
ruleNames: [
Expand All @@ -24299,7 +24299,7 @@ Generated by [AVA](https://avajs.dev).
42,
],
fixInfo: null,
lineNumber: 253,
lineNumber: 263,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md051.md',
ruleNames: [
Expand All @@ -24315,7 +24315,7 @@ Generated by [AVA](https://avajs.dev).
21,
],
fixInfo: null,
lineNumber: 255,
lineNumber: 265,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md051.md',
ruleNames: [
Expand All @@ -24331,7 +24331,7 @@ Generated by [AVA](https://avajs.dev).
21,
],
fixInfo: null,
lineNumber: 257,
lineNumber: 267,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md051.md',
ruleNames: [
Expand Down Expand Up @@ -24386,8 +24386,16 @@ Generated by [AVA](https://avajs.dev).
[Valid](#valid-heading-about-lh%C3%B4pitals-rule)␊
[Valid](#valid-heading-about-lhôpitals-rule)␊
[Valid](#en-t%C3%AAte-valide-dans-fran%C3%A7ais-pour-v%C3%A9rification)␊
[Valid](#en-tête-valide-dans-français-pour-vérification)␊
[Valid](#%E6%A0%87%E9%A2%98)␊
[Valid](#标题)␊
[Valid](#valid-heading-is-a-link)␊
[Valid](#valid-heading-has-a-link)␊
Expand Down Expand Up @@ -24479,6 +24487,8 @@ Generated by [AVA](https://avajs.dev).
### En-tête Valide Dans Français Pour Vérification␊
### 标题␊
### [Valid Heading Is a Link](https://example.com)␊
### Valid Heading [Has a Link](https://example.com)␊
Expand Down
Binary file modified test/snapshots/markdownlint-test-scenarios.js.snap
Binary file not shown.

0 comments on commit 1ddee6b

Please sign in to comment.