Skip to content

Commit

Permalink
[Feature] Exclude links from broken link check (#56)
Browse files Browse the repository at this point in the history
* Exclude links from broken link check

* Fix styling

---------

Co-authored-by: Baspa <[email protected]>
  • Loading branch information
Baspa and Baspa committed Jan 26, 2024
1 parent 4432444 commit 2f98cbf
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
6 changes: 6 additions & 0 deletions config/seo.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,11 @@
'broken_link_check' => [
// Add status codes that should be considered as broken links. Empty array means all status codes starting with a 4, 5 or 0.
'status_codes' => [],

// If you wish to skip running some checks on some paths, list the paths
// in the array below. You can use wildcards.
'exclude_paths' => [
//
],
],
];
44 changes: 35 additions & 9 deletions src/Checks/Content/BrokenLinkCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,7 @@ public function validateContent(Crawler $crawler): bool
$content = collect($content)->filter(fn ($value) => $value !== null)
->map(fn ($link) => addBaseIfRelativeUrl($link, $this->url))
->filter(function ($link) {
// Filter out all links that are mailto or tel
if (preg_match('/^mailto:/msi', $link) ||
preg_match('/^tel:/msi', $link) ||
filter_var($link, FILTER_VALIDATE_URL) === false
) {
return false;
}

return $link;
return $this->isValidLink($link) && ! $this->isExcludedLink($link);
})
->filter(function ($link) {
return isBrokenLink($link) ? $link : false;
Expand Down Expand Up @@ -89,4 +81,38 @@ public function validateContent(Crawler $crawler): bool

return true;
}

private function isValidLink($link): bool
{
return ! preg_match('/^mailto:/msi', $link) &&
! preg_match('/^tel:/msi', $link) &&
filter_var($link, FILTER_VALIDATE_URL) !== false;
}

private function isExcludedLink($link): bool
{
$excludedPaths = config('seo.broken_link_check.exclude_paths');
if (empty($excludedPaths)) {
return false;
}

foreach ($excludedPaths as $path) {
if ($this->linkMatchesPath($link, $path)) {
return true;
}
}

return false;
}

private function linkMatchesPath($link, $path): bool
{
if (str_contains($path, '*')) {
$path = str_replace('/*', '', $path);

return str_starts_with($link, $path);
}

return str_contains($link, $path);
}
}
15 changes: 15 additions & 0 deletions tests/Checks/Content/BrokenLinkCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,18 @@

$this->assertTrue($check->check(Http::get('vormkracht10.nl/admin/dashboard'), $crawler));
});

it('can exclude certain paths from the broken link check', function () {
$check = new BrokenLinkCheck();
$crawler = new Crawler();

config(['seo.broken_link_check.exclude_paths' => ['https://vormkracht10.nl/excluded']]);

Http::fake([
'vormkracht10.nl' => Http::response('<html><head></head><body><a href="https://vormkracht10.nl/excluded">Excluded Link</a></body></html>', 200),
]);

$crawler->addHtmlContent(Http::get('vormkracht10.nl')->body());

$this->assertTrue($check->check(Http::get('vormkracht10.nl'), $crawler));
});

0 comments on commit 2f98cbf

Please sign in to comment.