diff --git a/src/Docfx.Build/TableOfContents/BuildTocDocument.cs b/src/Docfx.Build/TableOfContents/BuildTocDocument.cs index 2377d5af4fd..f227e49218c 100644 --- a/src/Docfx.Build/TableOfContents/BuildTocDocument.cs +++ b/src/Docfx.Build/TableOfContents/BuildTocDocument.cs @@ -25,11 +25,7 @@ public class BuildTocDocument : BaseDocumentBuildStep /// public override IEnumerable Prebuild(ImmutableList models, IHostService host) { - var (resolvedTocModels, includedTocs) = TocHelper.ResolveToc(models, host); - - ReportPreBuildDependency(resolvedTocModels, host, 8, includedTocs); - - return resolvedTocModels; + return TocHelper.ResolveToc(models, host); } public override void Build(FileModel model, IHostService host) @@ -106,155 +102,4 @@ private static LinkSourceInfo GetLinkSourceInfo(string path, string anchor, stri Target = path, }; } - - private static void ReportPreBuildDependency(List models, IHostService host, int parallelism, HashSet includedTocs) - { - var nearest = new ConcurrentDictionary(FilePathComparer.OSPlatformSensitiveStringComparer); - models.RunAll(model => - { - var item = (TocItemViewModel)model.Content; - UpdateNearestToc(host, item, model, nearest); - }, - parallelism); - - // handle not-in-toc items - UpdateNearestTocForNotInTocItem(models, host, nearest, parallelism); - } - - private static void UpdateNearestToc(IHostService host, TocItemViewModel item, FileModel toc, ConcurrentDictionary nearest) - { - var tocHref = item.TocHref; - var type = Utility.GetHrefType(tocHref); - if (type == HrefType.MarkdownTocFile || type == HrefType.YamlTocFile) - { - UpdateNearestTocCore(host, UriUtility.GetPath(tocHref), toc, nearest); - } - else if (item.TopicUid == null && Utility.IsSupportedRelativeHref(item.Href)) - { - UpdateNearestTocCore(host, UriUtility.GetPath(item.Href), toc, nearest); - } - - if (item.Items != null && item.Items.Count > 0) - { - foreach (var i in item.Items) - { - UpdateNearestToc(host, i, toc, nearest); - } - } - } - - private static void UpdateNearestTocForNotInTocItem(List models, IHostService host, ConcurrentDictionary nearest, int parallelism) - { - var allSourceFiles = host.SourceFiles; - var tocInfos = models.Select(m => new TocInfo(m)).ToArray(); - Parallel.ForEach( - EnumerateNotInTocArticles(), - new ParallelOptions { MaxDegreeOfParallelism = parallelism }, - item => - { - var near = (from tocInfo in tocInfos - let rel = new RelativeInfo(tocInfo, allSourceFiles[item]) - where rel.TocPathRelativeToArticle.SubdirectoryCount == 0 - orderby rel.TocPathRelativeToArticle.ParentDirectoryCount - select rel).FirstOrDefault(); - if (near != null) - { - nearest[item] = near; - } - }); - - IEnumerable EnumerateNotInTocArticles() - { - return from pair in allSourceFiles - where pair.Value.Type == DocumentType.Article && !nearest.ContainsKey(pair.Key) - select pair.Key; - } - } - - private static void UpdateNearestTocCore(IHostService host, string item, FileModel toc, ConcurrentDictionary nearest) - { - if (!host.SourceFiles.TryGetValue(item, out var itemSource)) - { - return; - } - - var tocInfo = new RelativeInfo(toc, itemSource); - nearest.AddOrUpdate( - item, - k => tocInfo, - (k, v) => Compare(tocInfo, v) < 0 ? tocInfo : v); - } - - private static RelativePath GetOutputPath(FileAndType file) - { - if (file.SourceDir != file.DestinationDir) - { - return (RelativePath)file.DestinationDir + (((RelativePath)file.File) - (RelativePath)file.SourceDir); - } - else - { - return (RelativePath)file.File; - } - } - - private static int Compare(RelativeInfo infoA, RelativeInfo infoB) - { - var relativePathA = infoA.TocPathRelativeToArticle; - var relativePathB = infoB.TocPathRelativeToArticle; - - int subDirCompareResult = relativePathA.SubdirectoryCount - relativePathB.SubdirectoryCount; - if (subDirCompareResult != 0) - { - return subDirCompareResult; - } - - int parentDirCompareResult = relativePathA.ParentDirectoryCount - relativePathB.ParentDirectoryCount; - if (parentDirCompareResult != 0) - { - return parentDirCompareResult; - } - - var tocA = infoA.TocInfo; - var tocB = infoB.TocInfo; - - var outputPathCompareResult = StringComparer.OrdinalIgnoreCase.Compare(tocA.OutputPath, tocB.OutputPath); - if (outputPathCompareResult != 0) - { - return outputPathCompareResult; - } - - return StringComparer.OrdinalIgnoreCase.Compare(tocA.FilePath, tocB.FilePath); - } - - private class TocInfo - { - public FileModel Model { get; set; } - - public string FilePath { get; set; } - - public RelativePath OutputPath { get; set; } - - public TocInfo(FileModel tocModel) - { - Model = tocModel; - FilePath = tocModel.FileAndType.File; - OutputPath = GetOutputPath(tocModel.FileAndType); - } - } - - private class RelativeInfo - { - public TocInfo TocInfo { get; set; } - - public RelativePath TocPathRelativeToArticle { get; set; } - - public RelativeInfo(TocInfo tocInfo, FileAndType article) - { - TocInfo = tocInfo; - TocPathRelativeToArticle = tocInfo.OutputPath.RemoveWorkingFolder().MakeRelativeTo(GetOutputPath(article)); - } - - public RelativeInfo(FileModel tocModel, FileAndType article) - : this(new TocInfo(tocModel), article) { } - } } diff --git a/src/Docfx.Build/TableOfContents/TocHelper.cs b/src/Docfx.Build/TableOfContents/TocHelper.cs index 95cd389c62f..bdf81c9fd30 100644 --- a/src/Docfx.Build/TableOfContents/TocHelper.cs +++ b/src/Docfx.Build/TableOfContents/TocHelper.cs @@ -15,11 +15,10 @@ public static class TocHelper YamlDeserializerWithFallback.Create() .WithFallback(); - public static (List tocModels, HashSet includedTocs) ResolveToc(ImmutableList models, IHostService host) + public static List ResolveToc(ImmutableList models, IHostService host) { var tocCache = new Dictionary(FilePathComparer.OSPlatformSensitiveStringComparer); var nonReferencedTocModels = new List(); - var referencedToc = new HashSet(FilePathComparer.OSPlatformSensitiveStringComparer); foreach (var model in models) { @@ -40,13 +39,9 @@ public static (List tocModels, HashSet includedTocs) ResolveT model.Content = tocItemInfo.Content; nonReferencedTocModels.Add(model); } - else - { - referencedToc.Add(model.Key); - } } - return (nonReferencedTocModels, referencedToc); + return nonReferencedTocModels; } public static TocItemViewModel LoadSingleToc(string file)