Skip to content
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

github pages template, allowed to skip testing for URLs. #73

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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\/
Loading