Skip to content

Commit

Permalink
feat: catch links that hava a missing "/" at the beginning
Browse files Browse the repository at this point in the history
  • Loading branch information
Joaquin Montes committed Sep 7, 2023
1 parent 46514e6 commit d20dbed
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/actions/validate-docs-links/lib/index.js

Large diffs are not rendered by default.

16 changes: 11 additions & 5 deletions .github/actions/validate-docs-links/src/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type FailureFunction = (message: string) => void

const RELATIVE_PATH = '/'
const EXCLUDED_HASHES: string[] = []
const EXCLUDED_PATHS: string[] = ['/movies.json']

const slugger = new GithubSlugger()

Expand Down Expand Up @@ -161,6 +162,8 @@ function validateInternalLink(errors: Errors, href: string): void {
// /docs/api/example#heading -> ["api/example", "heading""]
const [link, hash] = href.split('#')

if (EXCLUDED_PATHS.includes(link)) return

// check if doc page exists
const foundPage = documentMap.get(link.replace(/^\/+/, ''))

Expand Down Expand Up @@ -205,26 +208,29 @@ function traverseTreeAndValidateLinks(tree: any, doc: Document, setFailed: Failu
source: [],
related: [],
}

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

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)) {
if (counter < 7) {
console.log('\n\n\nNODE: ',JSON.stringify(node))
counter++;
}
const customComponentTree = markdownProcessor.parse(node.value)
traverseRecursively(customComponentTree)
}

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

if (href.startsWith(RELATIVE_PATH)) {
validateInternalLink(errors, href)
} else if (href.startsWith('#')) {
validateHashLink(errors, href, doc)
} else if (!nonInternalLinkRegex.test(href)) {
errors.related.push(href)
}
}
}
Expand Down

0 comments on commit d20dbed

Please sign in to comment.