Skip to content

Commit

Permalink
fix: Enable InvalidFileLink check when custom hrefGenerator is used (d…
Browse files Browse the repository at this point in the history
…otnet#8845)

fix: enable InvalidFileLink warnings. if custom href generator returns original href
  • Loading branch information
filzrev authored and p-kostov committed Jun 28, 2024
1 parent 075875f commit f2b846a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/Microsoft.DocAsCode.Build.Engine/LinkPhaseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,12 @@ private void CheckFileLink(FileModel model, HostService hostService, SaveResult
ToFileInSource = ((RelativePath)fileLink).RemoveWorkingFolder().ToString(),
FileLinkInSource = path,
GroupInfo = Context.GroupInfo,
Href = path.UrlEncode()
};
fli.Href = path.UrlEncode();
if (Context.ApplyTemplateSettings.HrefGenerator.GenerateHref(fli) != null)

if (Context.ApplyTemplateSettings.HrefGenerator.GenerateHref(fli) != fli.Href)
{
return;
return; // if HrefGenerator returns new href. Skip InvalidFileLink check.
}
}
if (result.FileLinkSources.TryGetValue(fileLink, out ImmutableList<LinkSourceInfo> list))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,13 @@ private string ResolveHref(string pathToFile, string originalPathToFile, FileMod
var fli = FileLinkInfo.Create(model.LocalPathFromRoot, model.File, path, context);
var href = context.HrefGenerator?.GenerateHref(fli);

if (fli.ToFileInDest == null && href == null)
// Check href is modified by HrefGenerator or not.
if (href != null && href != fli.Href)
{
return UriUtility.MergeHref(href, segments);
}

if (fli.ToFileInDest == null)
{
// original path to file can be null for files generated by docfx in PreBuild
var displayFilePath = string.IsNullOrEmpty(originalPathToFile) ? pathToFile : originalPathToFile;
Expand All @@ -167,7 +173,7 @@ private string ResolveHref(string pathToFile, string originalPathToFile, FileMod
}

// fragment and query in original href takes precedence over the one from hrefGenerator
return href == null ? fli.Href + segments : UriUtility.MergeHref(href, segments);
return fli.Href + segments;
}
#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public void TestBuild()
{
"# [test1](test.md#bookmark)",
"## [test2](test/test.md)",
"## [GitHub](GitHub.md?shouldBeAbbreviated=true#test)",
"# Api",
"## [Console](@System.Console)",
"## [ConsoleColor](xref:System.ConsoleColor)",
Expand Down Expand Up @@ -97,6 +98,7 @@ public void TestBuild()
"Test external xref with absolute URL and anchor: @str",
"Test invalid autolink xref: <xref:?displayProperty=fullName>",
"Test href generator: [GitHub](GitHub.md?shouldBeAbbreviated=true#test)",
"Test href generator: [Git](Git.md?shouldBeAbbreviated=true#test)",
"<p>",
"test",
},
Expand Down Expand Up @@ -205,6 +207,8 @@ public void TestBuild()
Assert.NotNull(model[0].Items);
Assert.Equal("test2", model[0].Items[0].Name);
Assert.Equal("test/test.html", model[0].Items[0].Href);
Assert.Equal("GitHub", model[0].Items[1].Name);
Assert.Equal("GH.md?isAbbreviated=true&shouldBeAbbreviated=true#test", model[0].Items[1].Href);
Assert.Equal("Api", model[1].Name);
Assert.Null(model[1].Href);
Assert.NotNull(model[1].Items);
Expand All @@ -213,7 +217,7 @@ public void TestBuild()
Assert.Equal("ConsoleColor", model[1].Items[1].Name);
Assert.Equal("../System.ConsoleColor.csyml", model[1].Items[1].Href);
}

{
// check conceptual.
var conceptualOutputPath = Path.Combine(_outputFolder, Path.ChangeExtension(conceptualFile, ".html"));
Expand Down Expand Up @@ -251,7 +255,8 @@ public void TestBuild()
"Test invalid xref with attribute: <xref href=\"invalid\" fullname=\"Foo&lt;T&gt;\"></xref>",
$"Test external xref with absolute URL and anchor: <xref href=\"str\" data-throw-if-not-resolved=\"False\" data-raw-source=\"@str\" sourcefile=\"{_inputFolder}/test.md\" sourcestartlinenumber=\"30\"></xref>",
$"Test invalid autolink xref: <xref href=\"?displayProperty=fullName\" data-throw-if-not-resolved=\"True\" data-raw-source=\"&lt;xref:?displayProperty=fullName&gt;\" sourcefile=\"{_inputFolder}/test.md\" sourcestartlinenumber=\"31\"></xref>",
$"Test href generator: <a href=\"GitHub.md?shouldBeAbbreviated=true#test\" sourcefile=\"{_inputFolder}/test.md\" sourcestartlinenumber=\"32\">GitHub</a></p>",
$"Test href generator: <a href=\"GitHub.md?shouldBeAbbreviated=true#test\" sourcefile=\"{_inputFolder}/test.md\" sourcestartlinenumber=\"32\">GitHub</a>",
$"Test href generator: <a href=\"Git.md?shouldBeAbbreviated=true#test\" sourcefile=\"{_inputFolder}/test.md\" sourcestartlinenumber=\"33\">Git</a></p>",
"<p>",
@"test",
"</p>"),
Expand Down Expand Up @@ -284,7 +289,8 @@ public void TestBuild()
"Test invalid xref with attribute: <span class=\"xref\">Foo&lt;T&gt;</span>",
"Test external xref with absolute URL and anchor: <a class=\"xref\" href=\"https://docs.python.org/3.5/library/stdtypes.html#str\">str</a>",
"Test invalid autolink xref: &lt;xref:?displayProperty=fullName&gt;",
"Test href generator: <a href=\"GH.md?isAbbreviated=true&shouldBeAbbreviated=true#test\">GitHub</a></p>",
"Test href generator: <a href=\"GH.md?isAbbreviated=true&shouldBeAbbreviated=true#test\">GitHub</a>",
"Test href generator: <a href=\"Git.md?shouldBeAbbreviated=true#test\">Git</a></p>",
"<p>",
"test",
"</p>"),
Expand Down

0 comments on commit f2b846a

Please sign in to comment.