diff --git a/src/Elastic.Markdown/IO/DocumentationFolder.cs b/src/Elastic.Markdown/IO/DocumentationFolder.cs index 820b8a94d..3991d60d8 100644 --- a/src/Elastic.Markdown/IO/DocumentationFolder.cs +++ b/src/Elastic.Markdown/IO/DocumentationFolder.cs @@ -6,24 +6,53 @@ namespace Elastic.Markdown.IO; public class DocumentationFolder { - public MarkdownFile? Index { get; } + public MarkdownFile? Index { get; set; } - public List FilesInOrder { get; } = new(); - public List GroupsInOrder { get; } = new(); + public List FilesInOrder { get; } + public List GroupsInOrder { get; } + + public required DocumentationFolder? Parent { get; init; } private HashSet OwnFiles { get; } public int Level { get; } - public DocumentationFolder(IReadOnlyCollection toc, + public DocumentationFolder( + IReadOnlyCollection toc, IDictionary lookup, IDictionary folderLookup, int level = 0, - MarkdownFile? index = null) + MarkdownFile? index = null + ) { Level = level; - Index = index; + var foundIndex = ProcessTocItems(toc, lookup, folderLookup, level, out var groupsInOrder, out var filesInOrder); + + GroupsInOrder = groupsInOrder; + FilesInOrder = filesInOrder; + Index = index ?? foundIndex; + + if (Index is not null) + { + FilesInOrder = FilesInOrder.Except([Index]).ToList(); + Index.Parent ??= this; + } + + OwnFiles = [.. FilesInOrder]; + } + private MarkdownFile? ProcessTocItems( + IReadOnlyCollection toc, + IDictionary lookup, + IDictionary folderLookup, + int level, + out List groupsInOrder, + out List filesInOrder + ) + { + groupsInOrder = []; + filesInOrder = []; + MarkdownFile? index = null; foreach (var tocItem in toc) { if (tocItem is TocFile file) @@ -31,16 +60,21 @@ public DocumentationFolder(IReadOnlyCollection toc, if (!lookup.TryGetValue(file.Path, out var d) || d is not MarkdownFile md) continue; + md.Parent = this; + if (file.Children.Count > 0 && d is MarkdownFile virtualIndex) { - var group = new DocumentationFolder(file.Children, lookup, folderLookup, level + 1, virtualIndex); - GroupsInOrder.Add(group); + var group = new DocumentationFolder(file.Children, lookup, folderLookup, level + 1, virtualIndex) + { + Parent = this + }; + groupsInOrder.Add(group); continue; } - FilesInOrder.Add(md); + filesInOrder.Add(md); if (file.Path.EndsWith("index.md") && d is MarkdownFile i) - Index ??= i; + index ??= i; } else if (tocItem is TocFolder folder) { @@ -53,15 +87,15 @@ public DocumentationFolder(IReadOnlyCollection toc, .ToArray(); } - var group = new DocumentationFolder(children, lookup, folderLookup, level + 1); - GroupsInOrder.Add(group); + var group = new DocumentationFolder(children, lookup, folderLookup, level + 1) + { + Parent = this + }; + groupsInOrder.Add(group); } } - Index ??= FilesInOrder.FirstOrDefault(); - if (Index != null) - FilesInOrder = FilesInOrder.Except(new[] { Index }).ToList(); - OwnFiles = [.. FilesInOrder]; + return index ?? filesInOrder.FirstOrDefault(); } public bool HoldsCurrent(MarkdownFile current) => diff --git a/src/Elastic.Markdown/IO/DocumentationSet.cs b/src/Elastic.Markdown/IO/DocumentationSet.cs index 0ddca3b6c..0d109dbcc 100644 --- a/src/Elastic.Markdown/IO/DocumentationSet.cs +++ b/src/Elastic.Markdown/IO/DocumentationSet.cs @@ -56,7 +56,10 @@ public DocumentationSet(BuildContext context) .GroupBy(file => file.RelativeFolder) .ToDictionary(g => g.Key, g => g.ToArray()); - Tree = new DocumentationFolder(Configuration.TableOfContents, FlatMappedFiles, folderFiles); + Tree = new DocumentationFolder(Configuration.TableOfContents, FlatMappedFiles, folderFiles) + { + Parent = null + }; } public MarkdownFile? GetMarkdownFile(IFileInfo sourceFile) diff --git a/src/Elastic.Markdown/IO/MarkdownFile.cs b/src/Elastic.Markdown/IO/MarkdownFile.cs index fe8b53944..7e78968b9 100644 --- a/src/Elastic.Markdown/IO/MarkdownFile.cs +++ b/src/Elastic.Markdown/IO/MarkdownFile.cs @@ -28,7 +28,9 @@ public MarkdownFile(IFileInfo sourceFile, IDirectoryInfo rootPath, MarkdownParse Collector = context.Collector; } - public DiagnosticsCollector Collector { get; } + private DiagnosticsCollector Collector { get; } + + public DocumentationFolder? Parent { get; set; } public string? UrlPathPrefix { get; } private MarkdownParser MarkdownParser { get; } @@ -52,6 +54,19 @@ public string? NavigationTitle private bool _instructionsParsed; + public MarkdownFile[] YieldParents() + { + var parents = new List(); + var parent = Parent; + do + { + if (parent is { Index: not null } && parent.Index != this) + parents.Add(parent.Index); + parent = parent?.Parent; + } while (parent != null); + return parents.ToArray(); + } + public async Task MinimalParse(Cancel ctx) { var document = await MarkdownParser.MinimalParseAsync(SourceFile, ctx); diff --git a/src/Elastic.Markdown/Slices/HtmlWriter.cs b/src/Elastic.Markdown/Slices/HtmlWriter.cs index 812fbe0d8..e8f162441 100644 --- a/src/Elastic.Markdown/Slices/HtmlWriter.cs +++ b/src/Elastic.Markdown/Slices/HtmlWriter.cs @@ -44,6 +44,9 @@ public async Task RenderLayout(MarkdownFile markdown, Cancel ctx = defau var html = markdown.CreateHtml(document); await DocumentationSet.Tree.Resolve(ctx); var navigationHtml = await RenderNavigation(markdown, ctx); + + var previous = DocumentationSet; + var slice = Index.Create(new IndexViewModel { Title = markdown.Title ?? "[TITLE NOT SET]", diff --git a/src/Elastic.Markdown/Slices/_Layout.cshtml b/src/Elastic.Markdown/Slices/_Layout.cshtml index 2b42c676d..0b5163912 100644 --- a/src/Elastic.Markdown/Slices/_Layout.cshtml +++ b/src/Elastic.Markdown/Slices/_Layout.cshtml @@ -17,7 +17,6 @@
@(new HtmlString(Model.NavigationHtml)) - @*@(await RenderPartialAsync(Elastic.Markdown.Slices.Layout._TocTree.Create(Model)))*@ @(await RenderPartialAsync(_TableOfContents.Create(Model)))