From 0a71800f9185278c936ceb5ea5e445eb5804932a Mon Sep 17 00:00:00 2001 From: Puru Vijay Date: Fri, 20 Sep 2024 22:16:23 +0530 Subject: [PATCH] External link checks --- scripts/checks.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/scripts/checks.ts b/scripts/checks.ts index d2aebe3..ffc0c3f 100644 --- a/scripts/checks.ts +++ b/scripts/checks.ts @@ -181,6 +181,8 @@ async function checkLinks() { const slugErrors = errors.get(slug)!; const lines = raw.split('\n'); + const linksToCheck = new Set(); + await transform(raw, { link({ href }) { if (href.startsWith('#')) { @@ -217,11 +219,27 @@ async function checkLinks() { } } } + } else if (/^https?:\/\//.test(href)) { + // If the link is an external URL, then add it to the link queue + linksToCheck.add(href); } return ''; }, }); + + // Check links to external URLs + for (const link of linksToCheck) { + console.log(kleur.dim().bold(`Checking ${slug}:${link}`)); + const response = await fetch(link); + if (!response.ok) { + slugErrors.add({ + message: `External: Link to ${link} is broken`, + file: path, + line: lines.findIndex(line => line.includes(link)) + 1, + }); + } + } } }