diff --git a/demo/markdownlint-browser.js b/demo/markdownlint-browser.js index 12de7389..074a137d 100644 --- a/demo/markdownlint-browser.js +++ b/demo/markdownlint-browser.js @@ -4601,7 +4601,8 @@ module.exports = [ const { addError, escapeForRegExp, filterTokens, forEachInlineChild, forEachHeading, htmlElementRe } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"); // Regular expression for identifying HTML anchor names -const identifierRe = /(?:id|name)\s*=\s*['"]?([^'"\s>]+)/iu; +const idRe = /\sid\s*=\s*['"]?([^'"\s>]+)/iu; +const nameRe = /\sname\s*=\s*['"]?([^'"\s>]+)/iu; /** * Converts a Markdown heading into an HTML fragment according to the rules * used by GitHub. @@ -4643,11 +4644,10 @@ module.exports = { let match = null; while ((match = htmlElementRe.exec(token.content)) !== null) { const [tag, , element] = match; - if (element.toLowerCase() === "a") { - const idMatch = identifierRe.exec(tag); - if (idMatch) { - fragments.set(`#${idMatch[1]}`, 0); - } + const anchorMatch = idRe.exec(tag) || + (element.toLowerCase() === "a" && nameRe.exec(tag)); + if (anchorMatch) { + fragments.set(`#${anchorMatch[1]}`, 0); } } }; diff --git a/lib/md051.js b/lib/md051.js index 478f3613..b8b67f5a 100644 --- a/lib/md051.js +++ b/lib/md051.js @@ -6,7 +6,8 @@ const { addError, escapeForRegExp, filterTokens, forEachInlineChild, forEachHeading, htmlElementRe } = require("../helpers"); // Regular expression for identifying HTML anchor names -const identifierRe = /(?:id|name)\s*=\s*['"]?([^'"\s>]+)/iu; +const idRe = /\sid\s*=\s*['"]?([^'"\s>]+)/iu; +const nameRe = /\sname\s*=\s*['"]?([^'"\s>]+)/iu; /** * Converts a Markdown heading into an HTML fragment according to the rules @@ -55,11 +56,10 @@ module.exports = { let match = null; while ((match = htmlElementRe.exec(token.content)) !== null) { const [ tag, , element ] = match; - if (element.toLowerCase() === "a") { - const idMatch = identifierRe.exec(tag); - if (idMatch) { - fragments.set(`#${idMatch[1]}`, 0); - } + const anchorMatch = idRe.exec(tag) || + (element.toLowerCase() === "a" && nameRe.exec(tag)); + if (anchorMatch) { + fragments.set(`#${anchorMatch[1]}`, 0); } } }; diff --git a/test/link-fragments.md b/test/link-fragments.md index de9caec4..07221c3e 100644 --- a/test/link-fragments.md +++ b/test/link-fragments.md @@ -52,6 +52,10 @@ [Valid](#HREFandID) +[Valid](#id-for-other-element) + +[Valid](#id-after-name) + [Valid][goodref] ### Valid H3 Heading @@ -125,6 +129,14 @@ Text Text +
+ + + + + + + [goodref]: #namedlink ## Invalid Fragments @@ -139,6 +151,12 @@ Text [Invalid](#hrefandid) {MD051} +[Invalid](#name-for-other-element) {MD051} + +[Invalid](#name-should-be-ignored) {MD051} + +[Invalid](#not-an-id-should-be-ignored) {MD051} + [Invalid][badref] {MD051} [badref]: #missing diff --git a/test/snapshots/markdownlint-test-scenarios.js.md b/test/snapshots/markdownlint-test-scenarios.js.md index cba78172..aed2c4e4 100644 --- a/test/snapshots/markdownlint-test-scenarios.js.md +++ b/test/snapshots/markdownlint-test-scenarios.js.md @@ -20950,7 +20950,7 @@ Generated by [AVA](https://avajs.dev). 31, ], fixInfo: null, - lineNumber: 132, + lineNumber: 144, ruleDescription: 'Link fragments should be valid', ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md051', ruleNames: [ @@ -20966,7 +20966,7 @@ Generated by [AVA](https://avajs.dev). 36, ], fixInfo: null, - lineNumber: 134, + lineNumber: 146, ruleDescription: 'Link fragments should be valid', ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md051', ruleNames: [ @@ -20982,7 +20982,7 @@ Generated by [AVA](https://avajs.dev). 28, ], fixInfo: null, - lineNumber: 136, + lineNumber: 148, ruleDescription: 'Link fragments should be valid', ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md051', ruleNames: [ @@ -20998,7 +20998,7 @@ Generated by [AVA](https://avajs.dev). 18, ], fixInfo: null, - lineNumber: 138, + lineNumber: 150, ruleDescription: 'Link fragments should be valid', ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md051', ruleNames: [ @@ -21014,7 +21014,55 @@ Generated by [AVA](https://avajs.dev). 21, ], fixInfo: null, - lineNumber: 140, + lineNumber: 152, + ruleDescription: 'Link fragments should be valid', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md051', + ruleNames: [ + 'MD051', + 'link-fragments', + ], + }, + { + errorContext: '[Invalid](#name-for-other-element)', + errorDetail: null, + errorRange: [ + 1, + 34, + ], + fixInfo: null, + lineNumber: 154, + ruleDescription: 'Link fragments should be valid', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md051', + ruleNames: [ + 'MD051', + 'link-fragments', + ], + }, + { + errorContext: '[Invalid](#name-should-be-ignored)', + errorDetail: null, + errorRange: [ + 1, + 34, + ], + fixInfo: null, + lineNumber: 156, + ruleDescription: 'Link fragments should be valid', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md051', + ruleNames: [ + 'MD051', + 'link-fragments', + ], + }, + { + errorContext: '[Invalid](#not-an-id-should-be-ignored)', + errorDetail: null, + errorRange: [ + 1, + 39, + ], + fixInfo: null, + lineNumber: 158, ruleDescription: 'Link fragments should be valid', ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md051', ruleNames: [ @@ -21027,7 +21075,7 @@ Generated by [AVA](https://avajs.dev). errorDetail: null, errorRange: null, fixInfo: null, - lineNumber: 142, + lineNumber: 160, ruleDescription: 'Link fragments should be valid', ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md051', ruleNames: [ @@ -21090,6 +21138,10 @@ Generated by [AVA](https://avajs.dev). ␊ [Valid](#HREFandID)␊ ␊ + [Valid](#id-for-other-element)␊ + ␊ + [Valid](#id-after-name)␊ + ␊ [Valid][goodref]␊ ␊ ### Valid H3 Heading␊ @@ -21163,6 +21215,14 @@ Generated by [AVA](https://avajs.dev). ␊ Text␊ ␊ + ␊ + ␊ + ␊ + ␊ + ␊ + ␊ + ␊ + ␊ [goodref]: #namedlink␊ ␊ ## Invalid Fragments␊ @@ -21177,6 +21237,12 @@ Generated by [AVA](https://avajs.dev). ␊ [Invalid](#hrefandid) {MD051}␊ ␊ + [Invalid](#name-for-other-element) {MD051}␊ + ␊ + [Invalid](#name-should-be-ignored) {MD051}␊ + ␊ + [Invalid](#not-an-id-should-be-ignored) {MD051}␊ + ␊ [Invalid][badref] {MD051}␊ ␊ [badref]: #missing␊ diff --git a/test/snapshots/markdownlint-test-scenarios.js.snap b/test/snapshots/markdownlint-test-scenarios.js.snap index fda4e967..1b203b7e 100644 Binary files a/test/snapshots/markdownlint-test-scenarios.js.snap and b/test/snapshots/markdownlint-test-scenarios.js.snap differ