-
-
Notifications
You must be signed in to change notification settings - Fork 740
Commit
#495).
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// @ts-check | ||
|
||
"use strict"; | ||
|
||
const { addError, forEachHeading, filterTokens } = require("../helpers"); | ||
|
||
/** | ||
* Converts a Markdown heading into an HTML fragment | ||
* according to the rules used by GitHub. | ||
* | ||
* @param {string} string The string to convert. | ||
* @returns {string} The converted string. | ||
*/ | ||
function convertHeadingToHTMLFragment(string) { | ||
return "#" + string | ||
.toLowerCase() | ||
.replace(/ /g, "-") | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
DavidAnson
Owner
|
||
.replace(/[^-_a-z0-9]/g, ""); | ||
} | ||
|
||
module.exports = { | ||
"names": [ "MD051", "valid-link-fragments" ], | ||
"description": "Link fragments should be valid", | ||
"tags": [ "links" ], | ||
"function": function MD051(params, onError) { | ||
const validLinkFragments = []; | ||
forEachHeading(params, (_heading, content) => { | ||
validLinkFragments.push(convertHeadingToHTMLFragment(content)); | ||
}); | ||
filterTokens(params, "inline", (token) => { | ||
token.children.forEach((child) => { | ||
const { lineNumber, type, attrs } = child; | ||
if (type === "link_open") { | ||
const href = attrs.find((attr) => attr[0] === "href"); | ||
if (href !== undefined && | ||
href[1].startsWith("#") && | ||
!validLinkFragments.includes(href[1]) | ||
) { | ||
const detail = "Link Fragment is invalid"; | ||
addError(onError, lineNumber, detail, href[1]); | ||
} | ||
} | ||
}); | ||
}); | ||
} | ||
}; |
https://github.com/bhollis/maruku uses
_
instead of-
should it be an option ?