Skip to content

Commit

Permalink
run markdown link test
Browse files Browse the repository at this point in the history
  • Loading branch information
metablaster committed Jan 18, 2021
1 parent 39f692e commit cecaa4e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 19 deletions.
8 changes: 3 additions & 5 deletions Modules/Ruleset.Compatibility/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,19 @@ Import-WinModule Microsoft.PowerShell.Management
Get-EventLog -Newest 5 -LogName "Application"
```

View more in the [Quick Start Guide][QuickStart].
View more in the [Quick Start Guide](./Help/QuickStart.md).

## Documentation

The module documentation is located in the [Help][ModuleDocs] directory.
The module documentation is located in the [Help](./Help) directory.

## Module maintainers

- Mark Kraus ([markekraus][markekraus])
- Steve Lee ([stevel-msft][stevel-msft])
- Bruce Payette ([brucepay][brucepay])

[ModuleDocs]: ./Help
[QuickStart]: ./Help/QuickStart.md
[PSGallery]: https://www.powershellgallery.com/packages/WindowsCompatibility/
[PSGallery]: https://www.powershellgallery.com/packages/WindowsCompatibility
[feature]: https://github.com/PowerShell/PowerShell-RFC/pull/226
[markekraus]: https://github.com/markekraus
[stevel-msft]: https://github.com/stevel-msft
Expand Down
42 changes: 29 additions & 13 deletions Modules/Ruleset.Test/Public/Test-MarkdownLinks.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ SOFTWARE.
Test links in markdown files
.DESCRIPTION
Test each link in one or multiple markdown files and report if any link is dead.
Test each link in one or multiple markdown files and report if any link is invalid.
You can "brute force" test links or test only unique ones.
Links to be tested can be excluded or included by using wildcard pattern.
Test can be customized for various TLS protocols, query timeouts and retry attempts.
Expand Down Expand Up @@ -107,7 +107,7 @@ For example, Depth 2 includes the Path parameter's directory, first level of sub
second level of subdirectories.
.PARAMETER Log
If specified, dead links are logged.
If specified, invalid links are logged.
Log file can be found in Logs\MarkdownLinkTest_DATE.log
.EXAMPLE
Expand All @@ -128,6 +128,7 @@ None. Test-MarkdownLinks does not generate any output
.NOTES
WebSslProtocol enum does not list Tls13
TODO: Implement pipeline support
TODO: Implement testing links to repository
#>
function Test-MarkdownLinks
{
Expand Down Expand Up @@ -341,21 +342,35 @@ function Test-MarkdownLinks

[uri[]] $FileLinks = @()

# URL regex breakdown:
# (
# https?:\/\/(www\.)?
# [a-zA-Z0-9@:%._\+~#=]{2,256}
# \.[a-z]{2,6}
# \b([-a-zA-Z0-9@:%_\+.~#?&//=]*)
# (\([^(]+\))?
# )

# TODO: This will capture only valid URL syntax
$LinkRegex = "(https?:\/\/(www\.)?[a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)(\([^(]+\))?)"

# Capture inline links
if (($LinkType -eq "Any") -or ($LinkType -eq "Inline"))
{
# Capture inline links
$FileLinks += Select-String -Path $Markdown -Pattern "(?<=\[.+\]\()(http.+?)(?=\))" -Encoding $DefaultEncoding |
# URL regex starts with: (?<=\[.+\]\()
# URL regex ends with: (?=\))
$FileLinks += Select-String -Path $Markdown -Encoding $DefaultEncoding -Pattern "(?<=\[.+\]\()$LinkRegex(?=\))" |
ForEach-Object {
# [Microsoft.PowerShell.Commands.MatchInfo]
$_.Matches.Groups[1].ToString()
} | Where-Object { !(& $SkipLink $_ ([ref] $TotalDiscarded)) }
}

# Capture reference links
if (($LinkType -eq "Any") -or ($LinkType -eq "Reference"))
{
# Capture reference links
$FileLinks += Select-String -Path $Markdown -Encoding $DefaultEncoding `
-Pattern "(?<=\[.+\]:\s)([(http(s)?):\/\/(www\.)?a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)(\(.+\))?)" |
# URL regex starts with: (?<=\[.+\]:\s)
$FileLinks += Select-String -Path $Markdown -Encoding $DefaultEncoding -Pattern "(?<=\[.+\]:\s)$LinkRegex" |
ForEach-Object {
$_.Matches.Groups[1].ToString()
} | Where-Object { !(& $SkipLink $_ ([ref] $TotalDiscarded)) }
Expand Down Expand Up @@ -440,7 +455,7 @@ function Test-MarkdownLinks
}
catch
{
Write-Warning -Message "Found dead link in '$File' -> $URL"
Write-Warning -Message "Found invalid link in '$File' -> $URL"

if ($Log)
{
Expand Down Expand Up @@ -484,10 +499,11 @@ function Test-MarkdownLinks
$HeaderStack.Pop | Out-Null
}

Write-Information -Tags "Test" -MessageData "**** LINK TEST STATUS REPORT ****"

if ($StatusReport.Count)
{
$RootRegex = [regex]::Escape($ProjectRoot)
Write-Information -Tags "Test" -MessageData "*** LINK TEST STATUS REPORT ***"

foreach ($Status in $StatusReport)
{
Expand All @@ -514,14 +530,14 @@ function Test-MarkdownLinks

if ($StatusReport.Count -eq 1)
{
Write-Warning -Message "Only 1 dead link found"
Write-Warning -Message "Only 1 invalid link found"
}
else
{
Write-Warning -Message "In total there are $($StatusReport.Count) dead links"
Write-Warning -Message "In total there are $($StatusReport.Count) invalid links"
}
}

Write-Information -Tags "Test" -MessageData "$TotalLinksTested links were tested in total"
Write-Information -Tags "Test" -MessageData "$TotalDiscarded links were discarded from test"
Write-Information -Tags "Test" -MessageData "INFO: $TotalLinksTested links were tested in total"
Write-Information -Tags "Test" -MessageData "INFO: $TotalDiscarded links were discarded from test"
}
30 changes: 29 additions & 1 deletion Readme/Regex.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,36 @@ Select path up to last directory, up to 3rd directory and last item respectively

### URL validation

Regex breakdown:

```regex
(
https?:\/\/(www\.)?
[a-zA-Z0-9@:%._\+~#=]{2,256}
\.[a-z]{2,6}
\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)
(\([^(]+\))?
)
```

```powershell
"[(http(s)?):\/\/(www\.)?a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)"
"https?:\/\/(www\.)?[a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)"
```

Sample match:

```none
https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-2000-server/bb726984
```

```powershell
"https?:\/\/(www\.)?[a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)(\([^(]+\))?"
```

Sample match:

```none
https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-2000-server/bb726984(v=technet.10)
```

### DACL validation
Expand Down

0 comments on commit cecaa4e

Please sign in to comment.