private void UpdateHref(HtmlNode link, string attribute, IDocumentBuildContext context, string sourceFilePath, string destFilePath)
        {
            var originalHref   = link.GetAttributeValue(attribute, null);
            var path           = UriUtility.GetPath(originalHref);
            var anchorFromNode = link.GetAttributeValue("anchor", null);
            var segments       = anchorFromNode ?? UriUtility.GetQueryStringAndFragment(originalHref);

            link.Attributes.Remove("anchor");
            if (RelativePath.TryParse(path) == null)
            {
                if (!string.IsNullOrEmpty(anchorFromNode))
                {
                    link.SetAttributeValue(attribute, originalHref + anchorFromNode);
                }
                return;
            }
            var fli = FileLinkInfo.Create(sourceFilePath, destFilePath, path, context);

            // fragment and query in original href takes precedence over the one from hrefGenerator
            var href = _settings.HrefGenerator?.GenerateHref(fli);

            link.SetAttributeValue(
                attribute,
                href == null ? fli.Href + segments : UriUtility.MergeHref(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)
            {
                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 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));
        }
Beispiel #3
0
        public void ApplyXrefSpec(XRefSpec spec)
        {
            if (spec == null)
            {
                return;
            }

            // TODO: What if href is not html?
            if (!string.IsNullOrEmpty(spec.Href))
            {
                var href = UriUtility.MergeHref(spec.Href, Query + Anchor);
                (Href, Query, Anchor) = UriUtility.Split(href);
            }
            Spec = spec;
        }
Beispiel #4
0
 public void TestMergeHref(string target, string source, string expected)
 {
     Assert.Equal(expected, UriUtility.MergeHref(target, source));
 }