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);

            string href = context.GetFilePath(HttpUtility.UrlDecode(path));

            // original path to file can be null for files generated by docfx in PreBuild
            var displayFilePath = string.IsNullOrEmpty(originalPathToFile) ? pathToFile : originalPathToFile;

            if (href == null)
            {
                Logger.LogInfo($"Unable to find file \"{displayFilePath}\" for {propertyName} referenced by TOC file \"{model.LocalPathFromRoot}\"");
                return(originalPathToFile);
            }

            var relativePath = GetRelativePath(href, model.File);
            var resolvedHref = ((RelativePath)relativePath).UrlEncode().ToString() + segments;

            return(resolvedHref);
        }
Beispiel #2
0
        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);
        }
Beispiel #3
0
        public override IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            if (parser.Options.LegacyMode)
            {
                return(TryMatchOld(parser, context));
            }
            var match = context.Match(DfmFencesMatcher);

            if (match?.Length > 0)
            {
                var sourceInfo = context.Consume(match.Length);

                // [!code-lang[name](href "optionalTitle")]
                var name  = StringHelper.UnescapeMarkdown(match["name"].GetValue());
                var href  = StringHelper.UnescapeMarkdown(match["href"].GetValue());
                var lang  = match.GetGroup("lang")?.GetValue() ?? string.Empty;
                var title = StringHelper.UnescapeMarkdown(match.GetGroup("title")?.GetValue() ?? string.Empty);
                var queryStringAndFragment = UriUtility.GetQueryStringAndFragment(href);
                var path            = UriUtility.GetPath(href);
                var pathQueryOption =
                    !string.IsNullOrEmpty(queryStringAndFragment) ?
                    ParsePathQueryString(queryStringAndFragment.Remove(1), queryStringAndFragment.Substring(1)) :
                    null;
                return(new DfmFencesBlockToken(this, parser.Context, name, path, sourceInfo, lang, title, pathQueryOption));
            }
            return(null);
        }
        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));
        }
Beispiel #5
0
        private string UpdateHrefCore(string href, RelativePath fromFile)
        {
            if (href.Length == 0)
            {
                return(string.Empty);
            }
            var path = UriUtility.GetPath(href);

            if (path.Length == 0)
            {
                return(href);
            }
            var qf = UriUtility.GetQueryStringAndFragment(href);
            var rp = RelativePath.TryParse(path);

            if (rp == null || !rp.IsFromWorkingFolder())
            {
                return(href);
            }
            if (!FileMap.TryGetValue(rp.UrlDecode(), out string filePath))
            {
                return(href);
            }
            string modifiedPath;

            if (fromFile == null)
            {
                modifiedPath = ((RelativePath)filePath).UrlEncode();
            }
            else
            {
                modifiedPath = ((RelativePath)filePath - fromFile).UrlEncode();
            }
            return(modifiedPath + qf);
        }
        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));
        }
Beispiel #7
0
            private string GetHrefFromRoot(string originalHref, HandleModelAttributesContext context)
            {
                if (context.FileAndType == null || string.IsNullOrEmpty(originalHref) || !RelativePath.IsRelativePath(originalHref))
                {
                    return(originalHref);
                }

                var result = originalHref;
                var ft     = context.FileAndType;
                var path   = (RelativePath)ft.File + (RelativePath)UriUtility.GetPath(originalHref);
                var file   = path.GetPathFromWorkingFolder().UrlDecode();

                if (context.Host.SourceFiles.ContainsKey(file))
                {
                    result = file.UrlEncode().ToString() + UriUtility.GetQueryStringAndFragment(originalHref);
                }

                List <LinkSourceInfo> sources;

                if (!context.FileLinkSources.TryGetValue(file, out sources))
                {
                    sources = new List <LinkSourceInfo>();
                    context.FileLinkSources[file] = sources;
                }
                sources.Add(new LinkSourceInfo
                {
                    Target     = file,
                    Anchor     = UriUtility.GetFragment(originalHref),
                    SourceFile = ft.File,
                });
                context.LinkToFiles.Add(file);

                return(result);
            }
Beispiel #8
0
 public void TestUriUtility(string input, string path, string queryString, string fragment, string queryStringAndFragment, string nonFragment)
 {
     Assert.Equal(path, UriUtility.GetPath(input));
     Assert.Equal(queryString, UriUtility.GetQueryString(input));
     Assert.Equal(fragment, UriUtility.GetFragment(input));
     Assert.Equal(queryStringAndFragment, UriUtility.GetQueryStringAndFragment(input));
     Assert.Equal(nonFragment, UriUtility.GetNonFragment(input));
 }
Beispiel #9
0
        public object Interpret(BaseSchema schema, object value, IProcessContext context, string path)
        {
            if (value == null || !CanInterpret(schema))
            {
                return(value);
            }

            if (!(value is string val))
            {
                throw new ArgumentException($"{value.GetType()} is not supported type string.");
            }

            if (!Uri.TryCreate(val, UriKind.RelativeOrAbsolute, out Uri uri))
            {
                var message = $"{val} is not a valid href";
                Logger.LogError(message, code: ErrorCodes.Build.InvalidHref);
                throw new DocumentException(message);
            }

            // "/" is also considered as absolute to us
            if (uri.IsAbsoluteUri || val.StartsWith("/", StringComparison.Ordinal))
            {
                return(Helper.RemoveHostName(val, _siteHostName));
            }

            // sample value: a/b/c?hello
            var filePath  = UriUtility.GetPath(val);
            var fragments = UriUtility.GetQueryStringAndFragment(val);
            var relPath   = RelativePath.TryParse(filePath);

            if (relPath != null)
            {
                var originalFile = context.GetOriginalContentFile(path);
                var currentFile  = (RelativePath)originalFile.File;
                relPath = (currentFile + relPath.UrlDecode()).GetPathFromWorkingFolder();
                if (_exportFileLink)
                {
                    (context.FileLinkSources).AddFileLinkSource(new LinkSourceInfo
                    {
                        Target     = relPath,
                        Anchor     = UriUtility.GetFragment(val),
                        SourceFile = originalFile.File
                    });
                }

                if (_updateValue && context.BuildContext != null)
                {
                    var resolved = (RelativePath)context.BuildContext.GetFilePath(relPath);
                    if (resolved != null)
                    {
                        val = resolved.MakeRelativeTo(((RelativePath)context.FileAndType.File).GetPathFromWorkingFolder()).UrlEncode() + fragments;
                    }
                }
            }

            return(val);
        }
Beispiel #10
0
        public object Interpret(BaseSchema schema, object value, IProcessContext context, string path)
        {
            if (value == null || !CanInterpret(schema))
            {
                return(value);
            }

            if (!(value is string val))
            {
                throw new ArgumentException($"{value.GetType()} is not supported type string.");
            }

            if (!Uri.TryCreate(val, UriKind.RelativeOrAbsolute, out Uri uri))
            {
                throw new DocumentException($"{val} is not a valid href");
            }

            // "/" is also considered as absolute to us
            if (uri.IsAbsoluteUri || val.StartsWith("/"))
            {
                return(value);
            }

            // sample value: a/b/c?hello
            var filePath  = UriUtility.GetPath(val);
            var fragments = UriUtility.GetQueryStringAndFragment(val);
            var relPath   = RelativePath.TryParse(filePath);

            if (relPath != null)
            {
                var currentFile = (RelativePath)context.Model.OriginalFileAndType.File;
                relPath = (currentFile + relPath.UrlDecode()).GetPathFromWorkingFolder();
                if (_exportFileLink)
                {
                    ((Dictionary <string, List <LinkSourceInfo> >)context.Properties.FileLinkSources).AddFileLinkSource(new LinkSourceInfo
                    {
                        Target     = relPath,
                        Anchor     = UriUtility.GetFragment(val),
                        SourceFile = context.Model.OriginalFileAndType.File
                    });
                }

                if (_updateValue && context.BuildContext != null)
                {
                    var resolved = (RelativePath)context.BuildContext.GetFilePath(relPath);
                    if (resolved != null)
                    {
                        val = resolved.MakeRelativeTo(((RelativePath)context.Model.File).GetPathFromWorkingFolder()).UrlEncode() + fragments;
                    }
                }
            }

            return(val);
        }
Beispiel #11
0
        public string GetHrefFromRoot(string originalHref, string sourceFileKey)
        {
            if (string.IsNullOrEmpty(sourceFileKey) || string.IsNullOrEmpty(originalHref) || !RelativePath.IsRelativePath(originalHref))
            {
                return(originalHref);
            }
            var path = (RelativePath)sourceFileKey + (RelativePath)UriUtility.GetPath(originalHref);
            var file = path.GetPathFromWorkingFolder().UrlDecode();

            if (!_context.AllSourceFiles.ContainsKey(file))
            {
                Logger.LogWarning($"Invalid file link: ({originalHref})", file: sourceFileKey, code: WarningCodes.Build.InvalidFileLink);
                return(originalHref);
            }
            return(file.UrlEncode().ToString() + UriUtility.GetQueryStringAndFragment(originalHref));
        }