mirror of
https://github.com/theoludwig/markdownlint-rule-relative-links.git
synced 2024-12-08 00:45:32 +01:00
fix: fragments checking should work in other elements than only anchor (e.g: <div>)
This commit is contained in:
parent
a5deae599a
commit
747203c23b
40
src/utils.js
40
src/utils.js
@ -100,11 +100,11 @@ const anchorNameRe = getHtmlAttributeRe("name")
|
|||||||
const anchorIdRe = getHtmlAttributeRe("id")
|
const anchorIdRe = getHtmlAttributeRe("id")
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the anchor HTML fragments from a Markdown string.
|
* Gets the id or anchor name fragments from a Markdown string.
|
||||||
* @param {string} content
|
* @param {string} content
|
||||||
* @returns {string[]}
|
* @returns {string[]}
|
||||||
*/
|
*/
|
||||||
const getMarkdownAnchorHTMLFragments = (content) => {
|
const getMarkdownIdOrAnchorNameFragments = (content) => {
|
||||||
const markdownIt = new MarkdownIt({ html: true })
|
const markdownIt = new MarkdownIt({ html: true })
|
||||||
const tokens = markdownIt.parse(content, {})
|
const tokens = markdownIt.parse(content, {})
|
||||||
|
|
||||||
@ -112,30 +112,20 @@ const getMarkdownAnchorHTMLFragments = (content) => {
|
|||||||
const result = []
|
const result = []
|
||||||
|
|
||||||
for (const token of tokens) {
|
for (const token of tokens) {
|
||||||
if (token.type === "inline") {
|
const anchorMatch =
|
||||||
if (!token.children) {
|
anchorIdRe.exec(token.content) || anchorNameRe.exec(token.content)
|
||||||
continue
|
if (!anchorMatch || anchorMatch.length === 0) {
|
||||||
}
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
for (const child of token.children) {
|
const anchorIdOrName = anchorMatch[1]
|
||||||
if (child.type === "html_inline") {
|
if (anchorMatch[1] === undefined) {
|
||||||
const anchorMatch =
|
continue
|
||||||
anchorIdRe.exec(token.content) || anchorNameRe.exec(token.content)
|
}
|
||||||
if (!anchorMatch || anchorMatch.length === 0) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
const anchorIdOrName = anchorMatch[1]
|
const anchorHTMLFragment = "#" + anchorIdOrName
|
||||||
if (anchorMatch[1] === undefined) {
|
if (!result.includes(anchorHTMLFragment)) {
|
||||||
continue
|
result.push(anchorHTMLFragment)
|
||||||
}
|
|
||||||
|
|
||||||
const anchorHTMLFragment = "#" + anchorIdOrName
|
|
||||||
if (!result.includes(anchorHTMLFragment)) {
|
|
||||||
result.push(anchorHTMLFragment)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,5 +136,5 @@ module.exports = {
|
|||||||
filterTokens,
|
filterTokens,
|
||||||
convertHeadingToHTMLFragment,
|
convertHeadingToHTMLFragment,
|
||||||
getMarkdownHeadings,
|
getMarkdownHeadings,
|
||||||
getMarkdownAnchorHTMLFragments,
|
getMarkdownAnchorHTMLFragments: getMarkdownIdOrAnchorNameFragments,
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user