11param (
2- # url list to verify links. Can either be a http address or a local file request. Local file paths support md and html files.
2+ # url list to verify links. Can either be a http address or a local file request. Local file paths support md and html files.
33 [string []] $urls ,
44 # file that contains a set of links to ignore when verifying
55 [string ] $ignoreLinksFile = " $PSScriptRoot /ignore-links.txt" ,
@@ -17,14 +17,14 @@ param (
1717 [string ] $branchReplaceRegex = " ^(https://github.com/.*/(?:blob|tree)/)master(/.*)$" ,
1818 # the substitute branch name or SHA commit
1919 [string ] $branchReplacementName = " " ,
20- # flag to allow checking against azure sdk link guidance.
20+ # flag to allow checking against azure sdk link guidance. Check link guidance here: https://aka.ms/azsdk/guideline/links
2121 [bool ] $checkLinkGuidance = $false
2222)
2323
2424$ProgressPreference = " SilentlyContinue" ; # Disable invoke-webrequest progress dialog
2525# Regex of the locale keywords.
2626$locale = " /en-us/"
27-
27+ $emptyLinkMessage = " There is at least one empty link in the page. Please replace with absolute link. Check here for more infomation: https://aka.ms/azsdk/guideline/links "
2828function NormalizeUrl ([string ]$url ){
2929 if (Test-Path $url ) {
3030 $url = " file://" + (Resolve-Path $url ).ToString();
@@ -85,16 +85,18 @@ function ResolveUri ([System.Uri]$referralUri, [string]$link)
8585 $linkUri = [System.Uri ]$link ;
8686 # Our link guidelines do not allow relative links so only resolve them when we are not
8787 # validating links against our link guidelines (i.e. !$checkLinkGuideance)
88- if (! $checkLinkGuidance ) {
89- if (! $linkUri.IsAbsoluteUri ) {
88+ if ($checkLinkGuidance -and ! $linkUri.IsAbsoluteUri ) {
89+ return $linkUri
90+ }
91+
92+ if (! $linkUri.IsAbsoluteUri ) {
9093 # For rooted paths resolve from the baseUrl
91- if ($link.StartsWith (" /" )) {
92- Write-Verbose " rooturl = $rootUrl "
93- $linkUri = new-object System.Uri([System.Uri ]$rootUrl , " .$link " );
94- }
95- else {
96- $linkUri = new-object System.Uri($referralUri , $link );
97- }
94+ if ($link.StartsWith (" /" )) {
95+ Write-Verbose " rooturl = $rootUrl "
96+ $linkUri = new-object System.Uri([System.Uri ]$rootUrl , " .$link " );
97+ }
98+ else {
99+ $linkUri = new-object System.Uri($referralUri , $link );
98100 }
99101 }
100102
@@ -134,6 +136,10 @@ function ParseLinks([string]$baseUri, [string]$htmlContent)
134136
135137function CheckLink ([System.Uri ]$linkUri )
136138{
139+ if (! $linkUri.ToString ().Trim()) {
140+ LogWarning " Found Empty link. Please use absolute link instead. Check here for more infomation: https://aka.ms/azsdk/guideline/links"
141+ return $false
142+ }
137143 if ($checkedLinks.ContainsKey ($linkUri )) {
138144 if (! $checkedLinks [$linkUri ]) {
139145 LogWarning " broken link $linkUri "
@@ -193,11 +199,19 @@ function CheckLink ([System.Uri]$linkUri)
193199 }
194200 }
195201
196- # Check if link uri includes locale info.
197- if ($checkLinkGuidance -and ($linkUri -match $locale )) {
198- LogWarning " DO NOT include locale $locale information in links: $linkUri ."
199- $linkValid = $false
202+ if ($checkLinkGuidance ) {
203+ # Check if the url is relative links, suppress the archor link validation.
204+ if (! $linkUri.IsAbsoluteUri -and ! $linkUri.ToString ().StartsWith(" #" )) {
205+ LogWarning " DO NOT use relative link $linkUri . Please use absolute link instead. Check here for more infomation: https://aka.ms/azsdk/guideline/links"
206+ $linkValid = $false
207+ }
208+ # Check if link uri includes locale info.
209+ if ($linkUri -match $locale ) {
210+ LogWarning " DO NOT include locale $locale information in links: $linkUri . Check here for more information: https://aka.ms/azsdk/guideline/links"
211+ $linkValid = $false
212+ }
200213 }
214+
201215 $checkedLinks [$linkUri ] = $linkValid
202216 return $linkValid
203217}
@@ -270,7 +284,6 @@ $checkedPages = @{};
270284$checkedLinks = @ {};
271285$badLinks = @ {};
272286$pageUrisToCheck = new-object System.Collections.Queue
273-
274287foreach ($url in $urls ) {
275288 $uri = NormalizeUrl $url
276289 $pageUrisToCheck.Enqueue ($uri );
@@ -286,9 +299,12 @@ while ($pageUrisToCheck.Count -ne 0)
286299 Write-Host " Found $ ( $linkUris.Count ) links on page $pageUri " ;
287300 $badLinksPerPage = @ ();
288301 foreach ($linkUri in $linkUris ) {
289- $linkUri = ReplaceGithubLink $linkUri
290- $isLinkValid = CheckLink $linkUri
302+ $replacedLink = ReplaceGithubLink $linkUri
303+ $isLinkValid = CheckLink $replacedLink
291304 if (! $isLinkValid -and ! $badLinksPerPage.Contains ($linkUri )) {
305+ if (! $linkUri.ToString ().Trim()) {
306+ $linkUri = $emptyLinkMessage
307+ }
292308 $badLinksPerPage += $linkUri
293309 }
294310 if ($recursive -and $isLinkValid ) {
0 commit comments