Skip to content

Commit

Permalink
Merge pull request #73 from fulldecent/features/github-pages-template…
Browse files Browse the repository at this point in the history
…-allow-to-skip-testing-for-URLs#72

github pages template, allowed to skip testing for URLs.
  • Loading branch information
Raza403 committed Apr 17, 2024
2 parents 90b8c5f + 8899047 commit 10fc066
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions test/fixtures/external-link-broken.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
<body>
<a href="https://freehorses.example.com/free-horses-on-1998-04-01-only.html">Get free horses here!</a>
<a href="tel:12345678999">12345678999</a>
<a href="https://dont-check-this.example.com">https://dont-check-this.example.com</a>
</body>
</html>
26 changes: 24 additions & 2 deletions test/plugin.html-validate.external-links.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const TIMEOUT_SECONDS = 5;
* @param {number} parallelism - The maximum number of tasks that can be processed concurrently. Must be greater than zero.
* @returns {Promise} A promise that resolves when all tasks have completed.
*/
async function runPromiseFunctionsWithParallelism(promiseFunctions, parallelism) {
async function runPromiseFunctionsWithParallelism(promiseFunctions, parallelism) {
const promisesInProgress = new Set();
for (const promiseFunction of promiseFunctions) {
if (promisesInProgress.size >= parallelism) {
Expand All @@ -32,6 +32,23 @@ async function runPromiseFunctionsWithParallelism(promiseFunctions, parallelism)
}

export default class ExternalLinksRule extends Rule {
constructor(options) {
super(options);
this.skipUrlsRegex = this.loadSkipUrls();
}

loadSkipUrls() {
try {
const skipUrlsFile = fs.readFileSync("./test/skip-urls.regex.txt", "utf-8");
const skipUrls = skipUrlsFile.split("\n").filter(url => url.trim() !== "");
// Convert each skip URL pattern to a regex object
return skipUrls.map(pattern => new RegExp(pattern));
} catch (error) {
console.error("Error loading skip URLs:", error);
return [];
}
}

documentation() {
return {
description: "Require all external links to be live.",
Expand All @@ -57,7 +74,12 @@ export default class ExternalLinksRule extends Rule {

check(url, element) {
if (!url || !url.startsWith("http")) {
return
return;
}

// Check if the URL matches any of the skip URL regex patterns
if (this.skipUrlsRegex.some(regex => regex.test(url))) {
return;
}

const row = this.db.prepare("SELECT found, time FROM urls WHERE url = ?").get(url);
Expand Down
3 changes: 3 additions & 0 deletions test/skip-urls.regex.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://dont-check-this.example.com
^https?:\/\/(?:www\.)?twitter\.com\/
^https?:\/\/(?:www\.)?linkedin\.com\/

0 comments on commit 10fc066

Please sign in to comment.