private string ResolveHref(string pathToFile, string originalPathToFile, FileModel model, IDocumentBuildContext context, string propertyName) { if (!Utility.IsSupportedRelativeHref(pathToFile)) { return(pathToFile); } var index = pathToFile.IndexOfAny(QueryStringOrAnchor); if (index == 0) { throw new DocumentException($"Invalid toc link for {propertyName}: {originalPathToFile}."); } var path = UriUtility.GetPath(pathToFile); var segments = UriUtility.GetQueryStringAndFragment(pathToFile); var fli = FileLinkInfo.Create(model.LocalPathFromRoot, model.File, path, context); var result = context.HrefGenerator?.GenerateHref(fli); if (fli.ToFileInDest == null && result == null) { // original path to file can be null for files generated by docfx in PreBuild var displayFilePath = string.IsNullOrEmpty(originalPathToFile) ? pathToFile : originalPathToFile; Logger.LogInfo($"Unable to find file \"{displayFilePath}\" for {propertyName} referenced by TOC file \"{model.LocalPathFromRoot}\""); return(originalPathToFile); } return((result ?? fli.Href) + segments); }
private string ResolveHref(string pathToFile, string originalPathToFile, FileModel model, IDocumentBuildContext context, string propertyName) { if (!Utility.IsSupportedRelativeHref(pathToFile)) { return(pathToFile); } var index = pathToFile.IndexOfAny(QueryStringOrAnchor); if (index == 0) { var message = $"Invalid toc link for {propertyName}: {originalPathToFile}."; Logger.LogError(message, code: ErrorCodes.Toc.InvalidTocLink); throw new DocumentException(message); } var path = UriUtility.GetPath(pathToFile); var segments = UriUtility.GetQueryStringAndFragment(pathToFile); var fli = FileLinkInfo.Create(model.LocalPathFromRoot, model.File, path, context); var href = context.HrefGenerator?.GenerateHref(fli); if (fli.ToFileInDest == null && href == null) { // original path to file can be null for files generated by docfx in PreBuild var displayFilePath = string.IsNullOrEmpty(originalPathToFile) ? pathToFile : originalPathToFile; Logger.LogInfo($"Unable to find file \"{displayFilePath}\" for {propertyName} referenced by TOC file \"{model.LocalPathFromRoot}\""); return(originalPathToFile); } // fragment and query in original href takes precedence over the one from hrefGenerator return(href == null ? fli.Href + segments : UriUtility.MergeHref(href, segments)); }
public void TestFileLinkInfo_EncodedWorkspaceCharacter() { string fromFileInSource = "articles/vpn-gateway/vpn-gateway-verify-connection-resource-manager.md"; string fromFileInDest = "vpn-gateway/vpn-gateway-verify-connection-resource-manager.html"; string href = "%7E/includes/media/vpn-gateway-verify-connection-portal-rm-include/connectionsucceeded.png"; var context = new Build.Engine.DocumentBuildContext("_output"); var expected = new FileLinkInfo { FileLinkInDest = null, FileLinkInSource = "~/includes/media/vpn-gateway-verify-connection-portal-rm-include/connectionsucceeded.png", FromFileInDest = "vpn-gateway/vpn-gateway-verify-connection-resource-manager.html", FromFileInSource = "articles/vpn-gateway/vpn-gateway-verify-connection-resource-manager.md", GroupInfo = null, Href = "../../includes/media/vpn-gateway-verify-connection-portal-rm-include/connectionsucceeded.png", ToFileInDest = null, ToFileInSource = "includes/media/vpn-gateway-verify-connection-portal-rm-include/connectionsucceeded.png" }; var result = FileLinkInfo.Create(fromFileInSource, fromFileInDest, href, context); Assert.Equal(result, expected); }