Skip to content

Commit

Permalink
Refactor link-checker to work with json (#2787)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeguillen authored Apr 16, 2024
1 parent 10cc134 commit 3a52314
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions .github/actions/validate-docs-links/src/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,26 +205,35 @@ function traverseTreeAndValidateLinks(tree: any, doc: Document, setFailed: Failu
hash: [],
source: [],
relative: [],

}

// Matches markdown links like [text](link)
const linkRegex = /\[[^\[\]]+\]\([^\(\)]+\)/gm
// Matches markdown links like [text](link) (excluding those that end with a file extension)
const linkRegex = /^(?!.*\.[a-zA-Z]+\)$)\[[^\[\]]+\]\([^\(\)]+\)$/gm

// Matches all links that use some kind of protocol (e.g. http://, https://, mailto:, etc.)
const nonInternalLinkRegex = /^(?:[a-z+]+:)?\/\/|^[a-z]+:/i;
const nonInternalLinkRegex = /^(?:[a-z+]+:)?\/\/|^[a-z]+:/i

function validateNodes (node: any, parse: boolean = false) {
function validateNodes(node: any, parse: boolean = false) {
// Handle links in custom components that were not correctly parsed
if (node.type === 'text' && linkRegex.test(node.value)) {
const customComponentTree = markdownProcessor.parse(node.value)
traverseRecursively(customComponentTree)
}

if (node.type === 'element' && node.tagName === 'a' || node.type === 'link' || node.type === 'buttonlink') {
if (
(node.type === 'element' && node.tagName === 'a') ||
node.type === 'link' ||
node.type === 'buttonlink'
) {
const href = node.properties?.href ?? node.url
if (!href) return


// Check if the link is an internal link and not ending with a file extension
if (href.startsWith(RELATIVE_PATH)) {
validateInternalLink(errors, href)
if(!/^.*\.[^\\]+$/.test(href)){
validateInternalLink(errors, href)
}
} else if (href.startsWith('#')) {
validateHashLink(errors, href, doc)
} else if (!nonInternalLinkRegex.test(href)) {
Expand Down

0 comments on commit 3a52314

Please sign in to comment.