awesome-powershell/DeadLinksAnalyzer.ps1

32 lines
931 B
PowerShell
Raw Normal View History

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
} catch {
2020-04-04 16:19:26 -04:00
Write-Warning -Message "Found dead url $url in $fileName"
$unreachable += $url
}
}
}
2020-04-04 16:46:43 -04:00
# Output urls
return $unreachable
}
2020-04-04 16:46:43 -04:00
$deadlinks = Test-MarkdownLinks -Path ".\readme.md"
if ($deadlinks) {
Write-Error -Message 'Dead links found' -Category ObjectNotFound
}