diff --git a/src/Microsoft.DocAsCode.Build.Engine/LinkPhaseHandler.cs b/src/Microsoft.DocAsCode.Build.Engine/LinkPhaseHandler.cs index 03c48ef9cf9..e76abee18af 100644 --- a/src/Microsoft.DocAsCode.Build.Engine/LinkPhaseHandler.cs +++ b/src/Microsoft.DocAsCode.Build.Engine/LinkPhaseHandler.cs @@ -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 list)) diff --git a/src/Microsoft.DocAsCode.Build.TableOfContents/TocDocumentProcessorBase.cs b/src/Microsoft.DocAsCode.Build.TableOfContents/TocDocumentProcessorBase.cs index d0519359176..84f1ec5ccf5 100644 --- a/src/Microsoft.DocAsCode.Build.TableOfContents/TocDocumentProcessorBase.cs +++ b/src/Microsoft.DocAsCode.Build.TableOfContents/TocDocumentProcessorBase.cs @@ -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; @@ -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 } diff --git a/test/Microsoft.DocAsCode.Build.Engine.Tests/DocumentBuilderTest.cs b/test/Microsoft.DocAsCode.Build.Engine.Tests/DocumentBuilderTest.cs index dc45bba0c09..bf31bf0ae37 100644 --- a/test/Microsoft.DocAsCode.Build.Engine.Tests/DocumentBuilderTest.cs +++ b/test/Microsoft.DocAsCode.Build.Engine.Tests/DocumentBuilderTest.cs @@ -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)", @@ -97,6 +98,7 @@ public void TestBuild() "Test external xref with absolute URL and anchor: @str", "Test invalid autolink xref: ", "Test href generator: [GitHub](GitHub.md?shouldBeAbbreviated=true#test)", + "Test href generator: [Git](Git.md?shouldBeAbbreviated=true#test)", "

", "test", }, @@ -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); @@ -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")); @@ -251,7 +255,8 @@ public void TestBuild() "Test invalid xref with attribute: ", $"Test external xref with absolute URL and anchor: ", $"Test invalid autolink xref: ", - $"Test href generator: GitHub

", + $"Test href generator: GitHub", + $"Test href generator: Git

", "

", @"test", "

"), @@ -284,7 +289,8 @@ public void TestBuild() "Test invalid xref with attribute: Foo<T>", "Test external xref with absolute URL and anchor: str", "Test invalid autolink xref: <xref:?displayProperty=fullName>", - "Test href generator: GitHub

", + "Test href generator: GitHub", + "Test href generator: Git

", "

", "test", "

"),