Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 50 additions & 16 deletions src/Elastic.Markdown/IO/DocumentationFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,75 @@ namespace Elastic.Markdown.IO;

public class DocumentationFolder
{
public MarkdownFile? Index { get; }
public MarkdownFile? Index { get; set; }

public List<MarkdownFile> FilesInOrder { get; } = new();
public List<DocumentationFolder> GroupsInOrder { get; } = new();
public List<MarkdownFile> FilesInOrder { get; }
public List<DocumentationFolder> GroupsInOrder { get; }

public required DocumentationFolder? Parent { get; init; }

private HashSet<MarkdownFile> OwnFiles { get; }

public int Level { get; }

public DocumentationFolder(IReadOnlyCollection<ITocItem> toc,
public DocumentationFolder(
IReadOnlyCollection<ITocItem> toc,
IDictionary<string, DocumentationFile> lookup,
IDictionary<string, DocumentationFile[]> 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<ITocItem> toc,
IDictionary<string, DocumentationFile> lookup,
IDictionary<string, DocumentationFile[]> folderLookup,
int level,
out List<DocumentationFolder> groupsInOrder,
out List<MarkdownFile> filesInOrder
)
{
groupsInOrder = [];
filesInOrder = [];
MarkdownFile? index = null;
foreach (var tocItem in toc)
{
if (tocItem is TocFile file)
{
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)
{
Expand All @@ -53,15 +87,15 @@ public DocumentationFolder(IReadOnlyCollection<ITocItem> 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) =>
Expand Down
5 changes: 4 additions & 1 deletion src/Elastic.Markdown/IO/DocumentationSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion src/Elastic.Markdown/IO/MarkdownFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
3 changes: 3 additions & 0 deletions src/Elastic.Markdown/Slices/HtmlWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public async Task<string> 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]",
Expand Down
15 changes: 12 additions & 3 deletions src/Elastic.Markdown/Slices/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
</div>
<div class="sy-page sy-container flex mx-auto">
@(new HtmlString(Model.NavigationHtml))
@*@(await RenderPartialAsync(Elastic.Markdown.Slices.Layout._TocTree.Create(Model)))*@
@(await RenderPartialAsync(_TableOfContents.Create(Model)))
<main class="sy-main w-full max-sm:max-w-full print:pt-6">
<div class="sy-breadcrumbs" role="navigation">
Expand All @@ -35,9 +34,19 @@
<span>/</span>
<meta itemprop="position" content="1">
</li>
@foreach (var item in Model.Parents.Reverse())
{
<li itemprop="itemListElement" itemscope="" itemtype="https://schema.org/ListItem">
<a itemprop="item" href="@item.Url">
<span itemprop="name">@item.Title</span>
</a>
<span>/</span>
<meta itemprop="position" content="2">
</li>
}
<li itemprop="itemListElement" itemscope="" itemtype="https://schema.org/ListItem">
<strong itemprop="name">Elastic Docs v3</strong>
<meta itemprop="position" content="2">
<strong itemprop="name">@Model.CurrentDocument.Title</strong>
<meta itemprop="position" content="3">
</li>
</ol>
<div class="xl:hidden ml-1">
Expand Down
21 changes: 21 additions & 0 deletions src/Elastic.Markdown/Slices/_ViewModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ public class LayoutViewModel
public required string NavigationHtml { get; set; }
public required string? UrlPathPrefix { get; set; }

private MarkdownFile[]? _parents;
public MarkdownFile[] Parents
{
get
{
if (_parents is not null)
return _parents;

var parents = new List<MarkdownFile>();
var current = CurrentDocument.Parent;
do
{
if (current is { Index: not null } && current.Index != CurrentDocument)
parents.Add(current.Index);
current = current?.Parent;
} while (current != null);

_parents = [.. parents];
return _parents;
}
}

public string Static(string path)
{
Expand Down
Loading