Skip to content

Commit

Permalink
adding link validation config for ignoring specific link destinations (
Browse files Browse the repository at this point in the history
  • Loading branch information
daspek authored and OneDriveAutomation committed Nov 29, 2018
1 parent 7fa46ff commit 61f40bd
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 11 deletions.
1 change: 1 addition & 0 deletions ApiDoctor.Validation/ApiDoctor.Validation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<Compile Include="AuthScopeDefinition.cs" />
<Compile Include="CodeBlockAnnotation.cs" />
<Compile Include="Config\DocumentOutlineFile.cs" />
<Compile Include="Config\LinkValidationConfigFile.cs" />
<Compile Include="DocFile.cs" />
<Compile Include="DocSet.cs" />
<Compile Include="EnumerationDefinition.cs" />
Expand Down
43 changes: 43 additions & 0 deletions ApiDoctor.Validation/Config/LinkValidationConfigFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* API Doctor
* Copyright (c) Microsoft Corporation
* All rights reserved.
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the ""Software""), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

namespace ApiDoctor.Validation.Config
{
using Newtonsoft.Json;

public class LinkValidationConfigFile : ConfigFile
{
[JsonProperty("pathsToIgnore")]
public string[] IgnoredPaths { get; set; }

public override bool IsValid
{
get
{
return this.IgnoredPaths != null;
}
}
}
}
19 changes: 18 additions & 1 deletion ApiDoctor.Validation/DocFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,18 @@ public string[] LinkDestinations
return new string[0];
}

return this.MarkdownLinks.Select(m => m.Definition.url).ToArray();
var destinations = new List<string>(this.MarkdownLinks.Count);
foreach (var link in this.MarkdownLinks)
{
if (link.Definition == null)
{
throw new ArgumentException("Link Definition was null. Link text: " + link.Text);
}

destinations.Add(link.Definition.url);
}

return destinations.ToArray();
}
}

Expand Down Expand Up @@ -1390,6 +1401,12 @@ protected virtual LinkValidationResult VerifyRelativeLink(FileInfo sourceFile, s
workingLinkUrl = workingLinkUrl.Substring(0, indexOfHash);
}

if (this.Parent?.LinkValidationConfig?.IgnoredPaths?.Any(ignoredPath =>
workingLinkUrl.StartsWith(ignoredPath, StringComparison.OrdinalIgnoreCase)) == true)
{
return LinkValidationResult.ExternalSkipped;
}

if (workingLinkUrl.StartsWith("/"))
{
// URL is relative to the base for the documentation
Expand Down
44 changes: 34 additions & 10 deletions ApiDoctor.Validation/DocSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ public IEnumerable<ErrorDefinition> ErrorCodes

public DocumentOutlineFile DocumentStructure { get; internal set;}

public LinkValidationConfigFile LinkValidationConfig { get; private set; }

internal TableSpec.TableSpecConverter TableParser { get; private set; }
#endregion

Expand Down Expand Up @@ -157,6 +159,14 @@ private void LoadRequirements()
this.DocumentStructure = foundOutlines;
}

LinkValidationConfigFile[] linkConfigs = TryLoadConfigurationFiles<LinkValidationConfigFile>(this.SourceFolderPath);
var foundLinkConfig = linkConfigs.FirstOrDefault();
if (foundLinkConfig != null)
{
Console.WriteLine($"Using link validation config file: {foundLinkConfig.SourcePath}");
this.LinkValidationConfig = foundLinkConfig;
}

SchemaConfigFile[] schemaConfigs = TryLoadConfigurationFiles<SchemaConfigFile>(this.SourceFolderPath);
var schemaConfig = schemaConfigs.FirstOrDefault();
if (schemaConfig != null)
Expand Down Expand Up @@ -582,20 +592,34 @@ public bool ValidateLinks(bool includeWarnings, string[] relativePathForFiles, I
}
}

foreach (var file in filesToCheck.Where(f => f.LinkDestinations.Any()))
foreach (var file in filesToCheck) //.Where(f => f.LinkDestinations.Any()))
{
string[] linkedPages;
file.ValidateNoBrokenLinks(includeWarnings, issues.For(file.DisplayName), out linkedPages, requireFilenameCaseMatch);

foreach (string pageName in linkedPages)
var issuesForFile = issues.For(file.DisplayName);
try
{
orphanedPageIndex[pageName] = false;
}
// skip files without links
if (file.LinkDestinations?.Length == 0)
{
continue;
}

if (null != file.Annotation && file.Annotation.TocPath != null)
string[] linkedPages;
file.ValidateNoBrokenLinks(includeWarnings, issuesForFile, out linkedPages, requireFilenameCaseMatch);

foreach (string pageName in linkedPages)
{
orphanedPageIndex[pageName] = false;
}

if (null != file.Annotation && file.Annotation.TocPath != null)
{
var pageName = this.CleanUpDisplayName(file.DisplayName);
orphanedPageIndex[pageName] = false;
}
}
catch (Exception ex)
{
var pageName = this.CleanUpDisplayName(file.DisplayName);
orphanedPageIndex[pageName] = false;
issuesForFile.Error(ValidationErrorCode.Unknown, "Exception processing links.", ex);
}
}

Expand Down

0 comments on commit 61f40bd

Please sign in to comment.