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 void UpdateHref(HtmlNode link, string attribute, IDocumentBuildContext context, string sourceFilePath, string destFilePath) { var originalHref = link.GetAttributeValue(attribute, null); var anchor = link.GetAttributeValue("anchor", null); link.Attributes.Remove("anchor"); var originalPath = UriUtility.GetPath(originalHref); var path = RelativePath.TryParse(originalPath); if (path == null) { if (!string.IsNullOrEmpty(anchor)) { link.SetAttributeValue(attribute, originalHref + anchor); } return; } var fli = new FileLinkInfo { FromFileInSource = sourceFilePath, FromFileInDest = destFilePath, }; if (path.IsFromWorkingFolder()) { var targetInSource = path.UrlDecode(); fli.ToFileInSource = targetInSource.RemoveWorkingFolder(); fli.ToFileInDest = RelativePath.GetPathWithoutWorkingFolderChar(context.GetFilePath(targetInSource)); fli.FileLinkInSource = targetInSource - (RelativePath)sourceFilePath; if (fli.ToFileInDest != null) { var resolved = (RelativePath)fli.ToFileInDest - (RelativePath)destFilePath; fli.FileLinkInDest = resolved; fli.Href = resolved.UrlEncode(); } else { fli.Href = (targetInSource.RemoveWorkingFolder() - ((RelativePath)sourceFilePath).RemoveWorkingFolder()).UrlEncode(); } } else { fli.FileLinkInSource = path.UrlDecode(); fli.ToFileInSource = ((RelativePath)sourceFilePath + path).RemoveWorkingFolder(); fli.FileLinkInDest = fli.FileLinkInSource; fli.Href = originalPath; } var href = _settings.HrefGenerator?.GenerateHref(fli) ?? fli.Href; link.SetAttributeValue(attribute, href + UriUtility.GetQueryString(originalHref) + (anchor ?? UriUtility.GetFragment(originalHref))); }
private void CheckFileLink(FileModel model, HostService hostService, SaveResult result) { result.LinkToFiles.RunAll(fileLink => { if (!hostService.SourceFiles.ContainsKey(fileLink)) { if (Context.ApplyTemplateSettings.HrefGenerator != null) { var path = ((RelativePath)fileLink).RemoveWorkingFolder() - ((RelativePath)model.OriginalFileAndType.File); var fli = new FileLinkInfo { FromFileInSource = model.OriginalFileAndType.File, FromFileInDest = model.File, ToFileInSource = ((RelativePath)fileLink).RemoveWorkingFolder().ToString(), FileLinkInSource = path, GroupInfo = Context.GroupInfo, }; fli.Href = path.UrlEncode(); if (Context.ApplyTemplateSettings.HrefGenerator.GenerateHref(fli) != null) { return; } } if (result.FileLinkSources.TryGetValue(fileLink, out ImmutableList <LinkSourceInfo> list)) { foreach (var fileLinkSourceFile in list) { Logger.LogWarning( $"Invalid file link:({fileLinkSourceFile.Target}{fileLinkSourceFile.Anchor}).", null, fileLinkSourceFile.SourceFile, fileLinkSourceFile.LineNumber.ToString(), WarningCodes.Build.InvalidFileLink); } } else { Logger.LogWarning($"Invalid file link:({fileLink}).", code: WarningCodes.Build.InvalidFileLink); } } }); }
private void UpdateHref(HtmlNode link, string attribute, IDocumentBuildContext context, string sourceFilePath, string destFilePath) { var originalHref = link.GetAttributeValue(attribute, null); var anchor = link.GetAttributeValue("anchor", null); link.Attributes.Remove("anchor"); var originalPath = UriUtility.GetPath(originalHref); var path = RelativePath.TryParse(originalPath); if (path == null) { if (!string.IsNullOrEmpty(anchor)) { link.SetAttributeValue(attribute, originalHref + anchor); } return; } var fli = FileLinkInfo.Create(sourceFilePath, destFilePath, originalPath, context); var href = _settings.HrefGenerator?.GenerateHref(fli) ?? fli.Href; link.SetAttributeValue(attribute, href + UriUtility.GetQueryString(originalHref) + (anchor ?? UriUtility.GetFragment(originalHref))); }