public static IsPathUnderSpecificFolder ( string path, string folder ) : bool | ||
path | string | |
folder | string | |
return | bool |
private string FixNonMdRelativeFileHref(string nonMdHref, IMarkdownContext context, string rawMarkdown) { // If the context doesn't have necessary info or nonMdHref is not a relative path, return the original href if (!context.Variables.ContainsKey("path") || !TypeForwardedToPathUtility.IsRelativePath(nonMdHref)) { return(nonMdHref); } var currentFilePath = (string)context.Variables["path"]; var currentFolderPath = Path.GetDirectoryName(currentFilePath); try { // if the relative path (not from azure resource file info mapping) is under docset. Just return it. var nonMdHrefFullPath = Path.GetFullPath(Path.Combine(currentFolderPath, nonMdHref)); if (TypeForwardedToPathUtility.IsPathUnderSpecificFolder(nonMdHrefFullPath, currentFolderPath)) { return(nonMdHref); } else { Logger.LogVerbose($"Relative path:{nonMdHref} is not under {currentFolderPath} of file {currentFilePath}. Use ex_resource to replace the link."); } // if azure resource file info doesn't exist, log warning and return if (!context.Variables.ContainsKey("azureResourceFileInfoMapping")) { Logger.LogWarning($"Can't find azure resource file info mapping. Couldn't fix href: {nonMdHref} in file {currentFilePath}. raw: {rawMarkdown}"); return(nonMdHref); } var nonMdHrefFileName = Path.GetFileName(nonMdHref); var azureResourceFileInfoMapping = (Dictionary <string, AzureFileInfo>)context.Variables["azureResourceFileInfoMapping"]; AzureFileInfo azureResourceFileInfo; if (!azureResourceFileInfoMapping.TryGetValue(nonMdHrefFileName, out azureResourceFileInfo)) { Logger.LogWarning($"Can't find info for file name {nonMdHrefFileName} in azure resource file info mapping. Couldn't fix href: {nonMdHref} in file {currentFilePath}. raw: {rawMarkdown}"); return(nonMdHref); } // If the nonMdHref is under same docset with current file. No need to fix that. if (TypeForwardedToPathUtility.IsPathUnderSpecificFolder(azureResourceFileInfo.FilePath, currentFolderPath)) { return(nonMdHref); } // If the nonMdHref is under different docset with current file but not exists. Then log warning and won't fix. if (!File.Exists(azureResourceFileInfo.FilePath)) { Logger.LogWarning($"{nonMdHref} refer by {currentFilePath} doesn't exists. Won't do link fix. raw: {rawMarkdown}"); return(nonMdHref); } // If the nonMdHref is under different docset with current file and also exists, then fix the link. // 1. copy the external file to ex_resource folder. 2. Return new href path to the file under external folder var exResourceDir = Directory.CreateDirectory(Path.Combine(currentFolderPath, ExternalResourceFolderName)); var resDestPath = Path.Combine(exResourceDir.FullName, Path.GetFileName(azureResourceFileInfo.FilePath)); File.Copy(azureResourceFileInfo.FilePath, resDestPath, true); return(TypeForwardedToPathUtility.MakeRelativePath(currentFolderPath, resDestPath)); } catch (NotSupportedException nse) { Logger.LogWarning($"Warning: FixNonMdRelativeFileHref can't be apply on reference: {nonMdHref}. Exception: {nse.Message}"); return(nonMdHref); } }
private string GenerateAzureLinkHref(IMarkdownContext context, string href, string rawMarkdown) { StringBuffer content = StringBuffer.Empty; // If the context doesn't have necessary info, return the original href if (!context.Variables.ContainsKey("path") || !context.Variables.ContainsKey("azureMarkdownFileInfoMapping")) { return(href); } // if the href is not relative path, return it if (!TypeForwardedToPathUtility.IsRelativePath(href)) { return(href); } // deal with bookmark. Get file name and anchor string hrefFileName = string.Empty; string anchor = string.Empty; var index = href.IndexOf('#'); if (index == -1) { hrefFileName = Path.GetFileName(href); } else if (index == 0) { return(href); } else { hrefFileName = Path.GetFileName(href.Remove(index)); anchor = href.Substring(index); } // deal with different kinds of relative paths var currentFilePath = (string)context.Variables["path"]; var azureMarkdownFileInfoMapping = (IReadOnlyDictionary <string, AzureFileInfo>)context.Variables["azureMarkdownFileInfoMapping"]; if (azureMarkdownFileInfoMapping == null || !azureMarkdownFileInfoMapping.ContainsKey(hrefFileName)) { Logger.LogWarning($"Can't fild reference file: {href} in azure file system for file {currentFilePath}. Raw: {rawMarkdown}"); return(href); } string azureHref = null; var hrefFileInfo = azureMarkdownFileInfoMapping[hrefFileName]; // Not in docsets and transform to azure external link if (hrefFileInfo.NeedTransformToAzureExternalLink) { azureHref = $"{hrefFileInfo.UriPrefix}/{Path.GetFileNameWithoutExtension(hrefFileName)}{anchor}"; } else { var hrefPath = hrefFileInfo.FilePath; // It is correct for Azure strucuture. Azure articles are all under same folder var isHrefInsameDocset = TypeForwardedToPathUtility.IsPathUnderSpecificFolder(hrefPath, Path.GetDirectoryName(currentFilePath)); // In same docset with current file, use relative path. Otherwise, use docset link prefix if (isHrefInsameDocset) { azureHref = string.Format("{0}{1}", TypeForwardedToPathUtility.MakeRelativePath(Path.GetDirectoryName(currentFilePath), hrefFileInfo.FilePath), anchor); } else { // If the file is in different docset, then append the absolute path prefix. .html should be remove as docs also don't need it now. azureHref = $"{hrefFileInfo.UriPrefix}/{Path.GetFileNameWithoutExtension(hrefFileName)}{anchor}"; } } return(azureHref); }
private static bool GenerateAzureFileInfo( string repositoryRoot, RewriterToolArguments rewriterToolArguments, string azureDocumentUriPrefix, AzureFileInformationCollection azureFileInformationCollection) { var azureMarkdownFileInfoMapping = new ConcurrentDictionary<string, AzureFileInfo>(); var azureResourceFileInfoMapping = new ConcurrentDictionary<string, AzureFileInfo>(); var files = Directory.GetFiles(repositoryRoot, "*", SearchOption.AllDirectories); Parallel.ForEach( files, new ParallelOptions { MaxDegreeOfParallelism = 8 }, file => { var relativePath = TypeForwardedToPathUtility.MakeRelativePath(repositoryRoot, file); if (IsIgnoreFile(relativePath, rewriterToolArguments.IsMigration)) { return; } var isSucceed = true; var azureTransformArguments = rewriterToolArguments.AzureTransformArgumentsList.FirstOrDefault(a => TypeForwardedToPathUtility.IsPathUnderSpecificFolder(file, a.SourceDir)); // By default, all the link should be transformed to external link with azure uri prefix // However, if we find that the file is under one of the folder that need to be transformed. Then the prefix uri should be docs but not auzre var needTransformToAzureExternalLink = true; var uriPrefix = azureDocumentUriPrefix; if (azureTransformArguments != null) { needTransformToAzureExternalLink = false; uriPrefix = azureTransformArguments.DocsHostUriPrefix; } var fileName = Path.GetFileName(file); var azureFileInfo = new AzureFileInfo { FileName = fileName, FilePath = TypeForwardedToPathUtility.NormalizePath(file), NeedTransformToAzureExternalLink = needTransformToAzureExternalLink, UriPrefix = uriPrefix }; AzureFileInfo conflictFile; var isMarkdownFile = Path.GetExtension(relativePath).Equals(MarkdownExtension, StringComparison.OrdinalIgnoreCase); if (isMarkdownFile) { isSucceed = azureMarkdownFileInfoMapping.TryAdd(fileName, azureFileInfo); azureMarkdownFileInfoMapping.TryGetValue(fileName, out conflictFile); } else { isSucceed = azureResourceFileInfoMapping.TryAdd(fileName, azureFileInfo); azureResourceFileInfoMapping.TryGetValue(fileName, out conflictFile); } if (!isSucceed) { Console.WriteLine($"GenerateAzureFileInfo warning: can't insert file: {file}, confilicts with: {conflictFile?.FilePath}"); } }); azureFileInformationCollection.AzureMarkdownFileInfoMapping = azureMarkdownFileInfoMapping.ToDictionary(m => m.Key, m => m.Value); azureFileInformationCollection.AzureResourceFileInfoMapping = azureResourceFileInfoMapping.ToDictionary(m => m.Key, m => m.Value); return true; }