diff --git a/package.json b/package.json index 5c285ed6e..681871035 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,8 @@ "test:watch": "jest --watch", "cypress": "cypress open", "cypress:headless": "cypress run --headless true --browser chrome", - "check:translations": "node ./scripts/checkTranlation.js", + "check:translations": "node ./scripts/checkTranslation.js", + "check:links": "node ./scripts/checkLinks.mjs", "postinstall": "husky", "prepare": "husky" }, diff --git a/scripts/checkLinks.mjs b/scripts/checkLinks.mjs new file mode 100644 index 000000000..a07b6fa8a --- /dev/null +++ b/scripts/checkLinks.mjs @@ -0,0 +1,51 @@ +import jsdom from 'jsdom'; + +const getLinks = async (baseUrl) => { + console.log(`Checking links for ${baseUrl}`); + const urlsToCheck = [baseUrl]; + const checkedInternalUrls = new Set(); + const deadExternalUrls = new Set(); + const liveExternalUrls = new Set(); + + const isNewURL = (url) => + !deadExternalUrls.has(url.href) && + !liveExternalUrls.has(url.href) && + !checkedInternalUrls.has(url.href); + + while (urlsToCheck.length !== 0) { + var internalUrl = urlsToCheck.pop(); + checkedInternalUrls.add(internalUrl); + + var dom = await jsdom.JSDOM.fromURL(internalUrl); + + var document = dom.window.document; + + var linksFromSite = document.getElementsByTagName('a'); + for (var urlIndex in linksFromSite) { + var url = linksFromSite[urlIndex]; + if (url.href && url.href.indexOf('://') !== -1 && isNewURL(url.href)) { + var isExternalLink = url.host !== dom.window.location.host; + + if (isExternalLink) { + var response = await fetch(url.href); + response.ok ? liveExternalUrls.add(url.href) : deadExternalUrls.add(url.href); + } else if (!checkedInternalUrls.has(url.href)) { + urlsToCheck.push(url.href); + checkedInternalUrls.add(url.href); + } + } + } + } + + return { + InternalUrls: checkedInternalUrls, + DeadExternalUrls: deadExternalUrls, + LiveExternalUrls: liveExternalUrls, + }; +}; + +var checkedUrls = await getLinks('https://bloom.chayn.co/'); + +console.log('Internal URLs examined', checkedUrls.InternalUrls); +console.log('Dead external URLs', checkedUrls.DeadExternalUrls); +console.log('Live external URLs', checkedUrls.LiveExternalUrls); diff --git a/scripts/checkTranlation.js b/scripts/checkTranslation.js similarity index 100% rename from scripts/checkTranlation.js rename to scripts/checkTranslation.js