From ae6c6e40755c24150a8079d6c166e26afe4be678 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Tue, 7 Jan 2025 19:59:23 +0100 Subject: [PATCH 1/5] Implement breadcrumbs fixes #138 --- .../IO/DocumentationFolder.cs | 68 ++++++++++++++----- src/Elastic.Markdown/IO/DocumentationSet.cs | 5 +- src/Elastic.Markdown/IO/MarkdownFile.cs | 4 +- src/Elastic.Markdown/Slices/HtmlWriter.cs | 3 + src/Elastic.Markdown/Slices/_Layout.cshtml | 15 +++- src/Elastic.Markdown/Slices/_ViewModels.cs | 21 ++++++ 6 files changed, 94 insertions(+), 22 deletions(-) diff --git a/src/Elastic.Markdown/IO/DocumentationFolder.cs b/src/Elastic.Markdown/IO/DocumentationFolder.cs index 820b8a94d..f551d5926 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,37 +60,42 @@ 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) { var children = folder.Children; if (children.Count == 0 - && folderLookup.TryGetValue(folder.Path, out var documentationFiles)) + && folderLookup.TryGetValue(folder.Path, out var documentationFiles)) { children = documentationFiles .Select(d => new TocFile(d.RelativePath, true, [])) .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..20fb10ed9 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; } 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..9d1831f21 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)))