-
Notifications
You must be signed in to change notification settings - Fork 421
/
DeadLinksAnalyzer.ps1
36 lines (30 loc) · 1.06 KB
/
DeadLinksAnalyzer.ps1
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
function Test-MarkdownLinks([String]$Path) {
$unreachable = @()
# Get markdown files recursively
$files = Get-ChildItem -Path $Path -Recurse -Include "*.md"
$files | ForEach-Object {
$fileName = $_.Name
Write-Host "Analyzing $fileName"
$urls = Select-String -Path $_ -Pattern "\[.+\]\((http.*?)\)" | ForEach-Object { $_.matches.Groups[1] } | Select-Object
$urls | ForEach-Object {
$url = $_.Value
Write-Host "Requesting url $url"
try {
$request = Invoke-WebRequest -Uri $url -DisableKeepAlive -UseBasicParsing
} catch {
Write-Warning -Message "Found dead url $url in $fileName"
$unreachable += $url
}
}
}
# Output urls
return $unreachable
}
$DeadLinks = Test-MarkdownLinks -Path ".\readme.md"
if ($DeadLinks) {
Write-Host -Object '--- DEAD LINKS FOUND ---' -ForegroundColor Red
foreach ($DeadLink in $DeadLinks) {
Write-Host -Object $DeadLink -ForegroundColor Red
}
exit 1
}