-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No more broken links #113
Merged
Merged
No more broken links #113
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
35a3787
add link checker script and github action
owenwahlgren 2de8a20
test
owenwahlgren 1844355
test
owenwahlgren 871f0b9
no more broken links
owenwahlgren 63be483
not needed
owenwahlgren f6fdea5
move to dev dependency
owenwahlgren 026e888
update script to check internal links + add caching
owenwahlgren 3260348
fix broken links
owenwahlgren e7aaf5b
update linkChecker
owenwahlgren 2182228
Merge branch 'dev' into broken-links-cicd
owenwahlgren 994bb2e
Update checklinks.yml
owenwahlgren be58cab
remove unused image from 07-avacloud-and-core-bridge.mdx
owenwahlgren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import get from 'axios'; | ||
import { readFileSync } from 'fs'; | ||
import { load } from 'cheerio'; | ||
import { sync as globSync } from 'glob'; | ||
|
||
const baseUrl = 'https://academy.avax.network'; | ||
|
||
const whitelist = [] // some websites return 404 for head requests, so we need to whitelist them, (fix: pass header -H 'Accept: text/html' and parse text/html) | ||
// see https://github.com/rust-lang/crates.io/issues/788 | ||
|
||
interface LinkCheckResult { | ||
file: string; | ||
link: string; | ||
line: number; | ||
isValid: boolean; | ||
} | ||
|
||
function isValidURLOrPath(url: string): boolean { | ||
try { | ||
new URL(url) | ||
return true | ||
} catch { | ||
if (url.startsWith("{") && url.endsWith("}")) { // is a a JSX component, ignore | ||
return false; | ||
} | ||
else if (url.indexOf('.') > -1) { // is a url or misconfigured path | ||
return true; | ||
} | ||
// where all our content lives | ||
return url.startsWith("/"); | ||
} | ||
} | ||
|
||
async function checkLink(url: string): Promise<boolean> { | ||
try { | ||
const response = await get(url, { | ||
timeout: 10000, // timeout to 10 seconds | ||
maxRedirects: 5, // handle up to 5 redirects | ||
validateStatus: function (status) { | ||
return status >= 200 && status < 400; // resolve only if the status code is less than 400 | ||
}, | ||
headers: { | ||
'User-Agent': 'Mozilla/5.0 (compatible; LinkChecker/1.0)', // Custom User-Agent | ||
} | ||
}); | ||
return response.status === 200; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
function extractLinksWithLineNumbers(mdxContent: string): { link: string; line: number }[] { | ||
const lines = mdxContent.split('\n'); | ||
const links: { link: string; line: number }[] = []; | ||
|
||
lines.forEach((line, index) => { | ||
const $ = load(`<div>${line}</div>`); | ||
$('a').each((i, elem) => { | ||
const href = $(elem).attr('href'); | ||
if (href && isValidURLOrPath(href)) { | ||
links.push({ link: href, line: index + 1 }); | ||
} | ||
}); | ||
|
||
const markdownLinkRegex = /\[.*?\]\((.*?)\)/g; | ||
let match; | ||
while ((match = markdownLinkRegex.exec(line)) !== null) { | ||
const link = match[1]; | ||
if (isValidURLOrPath(link)) { | ||
links.push({ link, line: index + 1 }); | ||
} | ||
} | ||
}); | ||
|
||
return links; | ||
} | ||
|
||
async function checkAllMdxFiles(): Promise<void> { | ||
const files = globSync('content/**/*.mdx'); | ||
console.log(`Found ${files.length} MDX files.`); | ||
|
||
const results: LinkCheckResult[] = []; | ||
|
||
for (const file of files) { | ||
console.log(`Processing file: ${file}`); | ||
|
||
const content = readFileSync(file, 'utf-8'); | ||
const links = extractLinksWithLineNumbers(content); | ||
|
||
const cache: { [link: string]: boolean } = {}; | ||
let isValid: boolean; | ||
|
||
for (const { link, line } of links) { | ||
console.log(`Checking link: ${link} in file: ${file} (line ${line})`); | ||
|
||
if (cache[link]) { | ||
isValid = cache[link]; | ||
} else { | ||
isValid = await checkLink(link); // check the link | ||
if (!isValid) { | ||
isValid = await checkLink(baseUrl + link); // if link failed check first time, try adding the base url (for internal relative links) | ||
} | ||
for (const wl of whitelist) { | ||
if (link.includes(wl)) { | ||
isValid = true; | ||
break; | ||
} | ||
} | ||
cache[link] = isValid; | ||
} | ||
results.push({ file, link, line, isValid }); | ||
|
||
if (!isValid) { | ||
console.error(`\x1b[31mBroken link found\x1b[0m in ${file} (line ${line}): \x1b[33m${link}\x1b[0m`); | ||
} | ||
} | ||
} | ||
|
||
|
||
const brokenLinks = results.filter(result => !result.isValid); | ||
if (brokenLinks.length > 0) { | ||
console.error(`\n\x1b[31mSummary of broken links:\x1b[0m`); | ||
brokenLinks.forEach(result => { | ||
console.error(`File: \x1b[36m${result.file}\x1b[0m, Line: \x1b[33m${result.line}\x1b[0m, Link: \x1b[31m${result.link}\x1b[0m`); | ||
}); | ||
process.exit(1); | ||
} else { | ||
console.log(`\x1b[32mAll links are valid.\x1b[0m`); | ||
} | ||
} | ||
|
||
checkAllMdxFiles().catch(error => { | ||
console.error('\x1b[31mError checking links:\x1b[0m', error); | ||
process.exit(1); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: Check MDX Links | ||
|
||
on: | ||
push: | ||
paths: | ||
- '**/*.mdx' | ||
pull_request: | ||
paths: | ||
- '**/*.mdx' | ||
|
||
jobs: | ||
check-links: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: '19' | ||
- name: Install dependencies | ||
run: | | ||
yarn install --frozen-lockfile | ||
yarn global add tsx | ||
- name: Check links | ||
run: yarn check-links |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here