mirror of
https://github.com/janikvonrotz/awesome-powershell.git
synced 2024-10-01 03:15:42 -04:00
311845151a
Make a headline named 'Dead links found' and add each dead link on a new line
36 lines
1.0 KiB
PowerShell
36 lines
1.0 KiB
PowerShell
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 {
|
|
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
|
|
} |