-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify_links.ps1
More file actions
75 lines (63 loc) · 2.37 KB
/
verify_links.ps1
File metadata and controls
75 lines (63 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
$root = "c:\Users\ablma\Documents\Cerezo\Web\condadodecastilla"
$htmlFiles = Get-ChildItem -Path $root -Include "*.html" -Recurse
$brokenLinks = @()
foreach ($file in $htmlFiles) {
# Skip layout templates which are dynamically loaded
if ($file.Name -like "_*.html") {
continue
}
$content = Get-Content $file.FullName -Raw
# Regex to find href and src
$links = [regex]::Matches($content, '(href|src)="([^"]+)"')
foreach ($match in $links) {
$type = $match.Groups[1].Value
$link = $match.Groups[2].Value
# Strip query strings and anchors
$link = $link -replace '[?#].*$', ''
# Skip external links, mailto, data, and empty links
if ($link -match "^(http|//|mailto:|data:)" -or [string]::IsNullOrWhiteSpace($link)) {
continue
}
# Resolve path
$currentDir = $file.DirectoryName
# Handle root-relative links
if ($link.StartsWith("/")) {
$targetPath = Join-Path $root $link.TrimStart("/")
}
else {
$targetPath = Join-Path $currentDir $link
}
# Normalize path
try {
$targetPath = [System.IO.Path]::GetFullPath($targetPath)
}
catch {
$brokenLinks += [PSCustomObject]@{
SourceFile = $file.FullName
Link = $link
Reason = "Invalid Path Syntax"
}
continue
}
# Check if file exists
if (-not (Test-Path $targetPath)) {
# Check if it's a directory (some links might point to folders, expecting index.html)
if (-not (Test-Path "$targetPath\index.html")) {
$brokenLinks += [PSCustomObject]@{
SourceFile = $file.FullName
Link = $link
ResolvedPath = $targetPath
Reason = "File Not Found"
}
}
}
}
}
if ($brokenLinks.Count -gt 0) {
$report = $brokenLinks | Format-List | Out-String
Set-Content -Path "broken_links_report.txt" -Value $report -Encoding UTF8
Write-Host "Found $($brokenLinks.Count) broken links. Report saved to broken_links_report.txt" -ForegroundColor Red
}
else {
Write-Host "No broken internal links found!" -ForegroundColor Green
}