Skip to content

Commit

Permalink
Update MD051/link-fragments to ignore HTML comments embedded in headi…
Browse files Browse the repository at this point in the history
…ng text.
  • Loading branch information
DavidAnson authored Jul 19, 2022
1 parent 6700acc commit 1bd748f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
7 changes: 5 additions & 2 deletions demo/markdownlint-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4610,7 +4610,10 @@ const identifierRe = /(?:id|name)\s*=\s*['"]?([^'"\s>]+)/iu;
* @returns {string} Fragment string for heading.
*/
function convertHeadingToHTMLFragment(inline) {
const inlineText = inline.children.map((token) => token.content).join("");
const inlineText = inline.children
.filter((token) => token.type !== "html_inline")
.map((token) => token.content)
.join("");
return "#" + encodeURIComponent(inlineText
.toLowerCase()
// RegExp source with Ruby's \p{Word} expanded into its General Categories
Expand Down Expand Up @@ -4663,7 +4666,7 @@ module.exports = {
context = match[0];
range = [match.index + 1, match[0].length];
}
addError(onError, lineNumber, null, context, range);
addError(onError, lineNumber, undefined, context, range);
}
});
}
Expand Down
7 changes: 5 additions & 2 deletions lib/md051.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ const identifierRe = /(?:id|name)\s*=\s*['"]?([^'"\s>]+)/iu;
* @returns {string} Fragment string for heading.
*/
function convertHeadingToHTMLFragment(inline) {
const inlineText = inline.children.map((token) => token.content).join("");
const inlineText = inline.children
.filter((token) => token.type !== "html_inline")
.map((token) => token.content)
.join("");
return "#" + encodeURIComponent(
inlineText
.toLowerCase()
Expand Down Expand Up @@ -77,7 +80,7 @@ module.exports = {
context = match[0];
range = [ match.index + 1, match[0].length ];
}
addError(onError, lineNumber, null, context, range);
addError(onError, lineNumber, undefined, context, range);
}
});
}
Expand Down
12 changes: 12 additions & 0 deletions test/link-fragments.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@

[Valid](#valid-repeated-heading-2)

[Valid](#valid-heading-with-trailing-space-)

[Valid](#valid-heading-with-two-trailing-spaces--)

[Valid](#valid-heading-with-embedded--comment)

[Valid](#namedlink)

[Valid](#idlink)
Expand Down Expand Up @@ -93,6 +99,12 @@ Text

### Valid Repeated Heading

### Valid Heading With Trailing Space <!-- comment -->

### Valid Heading With Two Trailing Spaces <!-- comment -->

### Valid Heading With Embedded <!-- comment --> Comment

<a name="namedlink"></a>

<a id = idlink></a>
Expand Down
24 changes: 18 additions & 6 deletions test/snapshots/markdownlint-test-scenarios.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -20950,7 +20950,7 @@ Generated by [AVA](https://avajs.dev).
31,
],
fixInfo: null,
lineNumber: 108,
lineNumber: 120,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md051',
ruleNames: [
Expand All @@ -20966,7 +20966,7 @@ Generated by [AVA](https://avajs.dev).
36,
],
fixInfo: null,
lineNumber: 110,
lineNumber: 122,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md051',
ruleNames: [
Expand All @@ -20982,7 +20982,7 @@ Generated by [AVA](https://avajs.dev).
28,
],
fixInfo: null,
lineNumber: 112,
lineNumber: 124,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md051',
ruleNames: [
Expand All @@ -20998,7 +20998,7 @@ Generated by [AVA](https://avajs.dev).
18,
],
fixInfo: null,
lineNumber: 114,
lineNumber: 126,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md051',
ruleNames: [
Expand All @@ -21014,7 +21014,7 @@ Generated by [AVA](https://avajs.dev).
21,
],
fixInfo: null,
lineNumber: 116,
lineNumber: 128,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md051',
ruleNames: [
Expand All @@ -21027,7 +21027,7 @@ Generated by [AVA](https://avajs.dev).
errorDetail: null,
errorRange: null,
fixInfo: null,
lineNumber: 118,
lineNumber: 130,
ruleDescription: 'Link fragments should be valid',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md051',
ruleNames: [
Expand Down Expand Up @@ -21070,6 +21070,12 @@ Generated by [AVA](https://avajs.dev).
[Valid](#valid-repeated-heading-2)␊
[Valid](#valid-heading-with-trailing-space-)␊
[Valid](#valid-heading-with-two-trailing-spaces--)␊
[Valid](#valid-heading-with-embedded--comment)␊
[Valid](#namedlink)␊
[Valid](#idlink)␊
Expand Down Expand Up @@ -21131,6 +21137,12 @@ Generated by [AVA](https://avajs.dev).
### Valid Repeated Heading␊
### Valid Heading With Trailing Space <!-- comment -->␊
### Valid Heading With Two Trailing Spaces <!-- comment -->␊
### Valid Heading With Embedded <!-- comment --> Comment␊
<a name="namedlink"></a>␊
<a id = idlink></a>␊
Expand Down
Binary file modified test/snapshots/markdownlint-test-scenarios.js.snap
Binary file not shown.

0 comments on commit 1bd748f

Please sign in to comment.