Skip to content

Commit 5719f0a

Browse files
authored
Remove Uri sources from navigation rendering. (#1546)
* Remove Uri sources from navigation rendering. This PR removes the need to keep track of NavigationSource on markdown files to render the navigation. Previous to this we needed the keep this around to track the 'real' parent of a section once it may have been reshuffled by `navigation.yml`. We now more diligently build the global navigation more truthfully and can relay on walking the current navigation items `.Parent` to find the proper root to render. This includes scrubbing/skipping phantoms from the navigation. Another step to refactoring our navigation acyclic graph as the single source of truth. * remove temporary pragma's while refactoring * use logger not Console.WriteLine
1 parent 68bcc75 commit 5719f0a

File tree

16 files changed

+144
-137
lines changed

16 files changed

+144
-137
lines changed

src/Elastic.ApiExplorer/OpenApiGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ private async Task<IFileInfo> Render<T>(INavigationItem current, T page, ApiRend
288288
if (!outputFile.Directory!.Exists)
289289
outputFile.Directory.Create();
290290

291-
var navigationRenderResult = await navigationRenderer.RenderNavigation(current.NavigationRoot, new Uri("http://ignored.example"), INavigationHtmlWriter.AllLevels, ctx);
291+
var navigationRenderResult = await navigationRenderer.RenderNavigation(current.NavigationRoot, INavigationHtmlWriter.AllLevels, ctx);
292292
renderContext = renderContext with
293293
{
294294
CurrentNavigation = current,

src/Elastic.Documentation.Site/Navigation/INavigationHtmlWriter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ public interface INavigationHtmlWriter
1010
{
1111
const int AllLevels = -1;
1212

13-
Task<NavigationRenderResult> RenderNavigation(IRootNavigationItem<INavigationModel, INavigationItem> currentRootNavigation, Uri navigationSource,
14-
int maxLevel, Cancel ctx = default);
13+
Task<NavigationRenderResult> RenderNavigation(IRootNavigationItem<INavigationModel, INavigationItem> currentRootNavigation, int maxLevel, Cancel ctx = default);
1514

1615
async Task<string> Render(NavigationViewModel model, Cancel ctx)
1716
{

src/Elastic.Documentation.Site/Navigation/IsolatedBuildNavigationHtmlWriter.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ public class IsolatedBuildNavigationHtmlWriter(BuildContext context, IRootNaviga
1313
{
1414
private readonly ConcurrentDictionary<(string, int), string> _renderedNavigationCache = [];
1515

16-
public async Task<NavigationRenderResult> RenderNavigation(IRootNavigationItem<INavigationModel, INavigationItem> currentRootNavigation,
17-
Uri navigationSource, int maxLevel, Cancel ctx = default)
16+
public async Task<NavigationRenderResult> RenderNavigation(
17+
IRootNavigationItem<INavigationModel, INavigationItem> currentRootNavigation, int maxLevel, Cancel ctx = default
18+
)
1819
{
1920
var navigation = context.Configuration.Features.PrimaryNavEnabled || currentRootNavigation.IsUsingNavigationDropdown
2021
? currentRootNavigation

src/Elastic.Markdown/HtmlWriter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ private async Task<RenderResult> RenderLayout(MarkdownFile markdown, MarkdownDoc
5656
var html = MarkdownFile.CreateHtml(document);
5757
await DocumentationSet.Tree.Resolve(ctx);
5858

59-
var fullNavigationRenderResult = await NavigationHtmlWriter.RenderNavigation(markdown.NavigationRoot, markdown.NavigationSource, INavigationHtmlWriter.AllLevels, ctx);
60-
var miniNavigationRenderResult = await NavigationHtmlWriter.RenderNavigation(markdown.NavigationRoot, markdown.NavigationSource, 1, ctx);
59+
var fullNavigationRenderResult = await NavigationHtmlWriter.RenderNavigation(markdown.NavigationRoot, INavigationHtmlWriter.AllLevels, ctx);
60+
var miniNavigationRenderResult = await NavigationHtmlWriter.RenderNavigation(markdown.NavigationRoot, 1, ctx);
6161

6262
var navigationHtmlRenderResult = DocumentationSet.Context.Configuration.Features.LazyLoadNavigation
6363
? miniNavigationRenderResult

src/Elastic.Markdown/IO/MarkdownFile.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ DocumentationSet set
5757
ScopeDirectory = build.Configuration.ScopeDirectory;
5858

5959
NavigationRoot = set.Tree;
60-
NavigationSource = set.Source;
6160
}
6261

6362
public bool PartOfNavigation { get; set; }
@@ -66,8 +65,6 @@ DocumentationSet set
6665

6766
public IRootNavigationItem<INavigationModel, INavigationItem> NavigationRoot { get; set; }
6867

69-
public Uri NavigationSource { get; set; }
70-
7168
private IDiagnosticsCollector Collector { get; }
7269

7370
public string? UrlPathPrefix { get; }

src/Elastic.Markdown/IO/Navigation/DocumentationGroup.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Elastic.Markdown.IO.Navigation;
1313

14-
[DebuggerDisplay("Group >{Depth} {FolderName} ({NavigationItems.Count} items)")]
14+
[DebuggerDisplay("Toc: {Depth} {NavigationSource} > ({NavigationItems.Count} items)")]
1515
public class DocumentationGroup : INodeNavigationItem<MarkdownFile, INavigationItem>
1616
{
1717
private readonly TableOfContentsTreeCollector _treeCollector;
@@ -138,7 +138,6 @@ void AddToNavigationItems(INavigationItem item, ref int fileIndex)
138138
// TODO these have to be refactor to be pure navigational properties
139139
md.ScopeDirectory = file.TableOfContentsScope.ScopeDirectory;
140140
md.NavigationRoot = rootNavigationItem;
141-
md.NavigationSource = NavigationSource;
142141

143142
foreach (var extension in lookups.EnabledExtensions)
144143
extension.Visit(d, tocItem);

src/Elastic.Markdown/IO/Navigation/TableOfContentsTree.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace Elastic.Markdown.IO.Navigation;
1010

11-
[DebuggerDisplay("Toc >{Depth} {FolderName} ({NavigationItems.Count} items)")]
11+
[DebuggerDisplay("Toc: {Depth} {NavigationSource} > ({NavigationItems.Count} items)")]
1212
public class TableOfContentsTree : DocumentationGroup, IRootNavigationItem<MarkdownFile, INavigationItem>
1313
{
1414
public Uri Source { get; }

src/Elastic.Markdown/IO/Navigation/TableOfContentsTreeCollector.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ public class TableOfContentsTreeCollector
1111
{
1212
private Dictionary<Uri, TableOfContentsTree> NestedTableOfContentsTrees { get; } = [];
1313

14-
public void Collect(Uri source, TableOfContentsTree tree) =>
15-
NestedTableOfContentsTrees[source] = tree;
16-
17-
public void Collect(TocReference tocReference, TableOfContentsTree tree) =>
18-
NestedTableOfContentsTrees[tocReference.Source] = tree;
14+
public void Collect(Uri source, TableOfContentsTree tree) => NestedTableOfContentsTrees[source] = tree;
1915

2016
public bool TryGetTableOfContentsTree(Uri source, [NotNullWhen(true)] out TableOfContentsTree? tree) =>
2117
NestedTableOfContentsTrees.TryGetValue(source, out tree);

src/tooling/docs-assembler/AssembleSources.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
using YamlDotNet.RepresentationModel;
2121

2222
namespace Documentation.Assembler;
23-
public record TocTopLevelMapping
23+
24+
public record NavigationTocMapping
2425
{
2526
public required Uri Source { get; init; }
2627
public required string SourcePathPrefix { get; init; }
@@ -30,7 +31,7 @@ public record TocTopLevelMapping
3031

3132
public record TocConfigurationMapping
3233
{
33-
public required TocTopLevelMapping TopLevel { get; init; }
34+
public required NavigationTocMapping TopLevel { get; init; }
3435
public required ConfigurationFile RepositoryConfigurationFile { get; init; }
3536
public required TableOfContentsConfiguration TableOfContentsConfiguration { get; init; }
3637
}
@@ -40,7 +41,7 @@ public class AssembleSources
4041
public AssembleContext AssembleContext { get; }
4142
public FrozenDictionary<string, AssemblerDocumentationSet> AssembleSets { get; }
4243

43-
public FrozenDictionary<Uri, TocTopLevelMapping> TocTopLevelMappings { get; }
44+
public FrozenDictionary<Uri, NavigationTocMapping> NavigationTocMappings { get; }
4445

4546
public FrozenDictionary<string, IReadOnlyCollection<string>> HistoryMappings { get; }
4647

@@ -61,19 +62,19 @@ public static async Task<AssembleSources> AssembleAsync(ILoggerFactory logger, A
6162
private AssembleSources(ILoggerFactory logger, AssembleContext assembleContext, Checkout[] checkouts, VersionsConfiguration versionsConfiguration)
6263
{
6364
AssembleContext = assembleContext;
64-
TocTopLevelMappings = GetConfiguredSources(assembleContext);
65+
NavigationTocMappings = GetTocMappings(assembleContext);
6566
HistoryMappings = GetHistoryMapping(assembleContext);
6667
var linkIndexProvider = Aws3LinkIndexReader.CreateAnonymous();
6768
var crossLinkFetcher = new AssemblerCrossLinkFetcher(logger, assembleContext.Configuration, assembleContext.Environment, linkIndexProvider);
68-
UriResolver = new PublishEnvironmentUriResolver(TocTopLevelMappings, assembleContext.Environment);
69+
UriResolver = new PublishEnvironmentUriResolver(NavigationTocMappings, assembleContext.Environment);
6970
var crossLinkResolver = new CrossLinkResolver(crossLinkFetcher, UriResolver);
7071
AssembleSets = checkouts
7172
.Where(c => c.Repository is { Skip: false })
7273
.Select(c => new AssemblerDocumentationSet(logger, assembleContext, c, crossLinkResolver, TreeCollector, versionsConfiguration))
7374
.ToDictionary(s => s.Checkout.Repository.Name, s => s)
7475
.ToFrozenDictionary();
7576

76-
TocConfigurationMapping = TocTopLevelMappings
77+
TocConfigurationMapping = NavigationTocMappings
7778
.Select(kv =>
7879
{
7980
var repo = kv.Value.Source.Scheme;
@@ -148,11 +149,11 @@ static void ReadHistoryMappings(IDictionary<string, IReadOnlyCollection<string>>
148149
}
149150

150151

151-
public static FrozenDictionary<Uri, TocTopLevelMapping> GetConfiguredSources(AssembleContext context)
152+
public static FrozenDictionary<Uri, NavigationTocMapping> GetTocMappings(AssembleContext context)
152153
{
153-
var dictionary = new Dictionary<Uri, TocTopLevelMapping>();
154+
var dictionary = new Dictionary<Uri, NavigationTocMapping>();
154155
var reader = new YamlStreamReader(context.NavigationPath, context.Collector);
155-
var entries = new List<KeyValuePair<Uri, TocTopLevelMapping>>();
156+
var entries = new List<KeyValuePair<Uri, NavigationTocMapping>>();
156157
foreach (var entry in reader.Read())
157158
{
158159
switch (entry.Key)
@@ -167,7 +168,7 @@ public static FrozenDictionary<Uri, TocTopLevelMapping> GetConfiguredSources(Ass
167168
return dictionary.ToFrozenDictionary();
168169

169170
static void ReadTocBlocks(
170-
List<KeyValuePair<Uri, TocTopLevelMapping>> entries,
171+
List<KeyValuePair<Uri, NavigationTocMapping>> entries,
171172
YamlStreamReader reader,
172173
KeyValuePair<YamlNode, YamlNode> entry,
173174
string? parent,
@@ -196,7 +197,7 @@ static void ReadTocBlocks(
196197
}
197198
}
198199
static void ReadBlock(
199-
List<KeyValuePair<Uri, TocTopLevelMapping>> entries,
200+
List<KeyValuePair<Uri, NavigationTocMapping>> entries,
200201
YamlStreamReader reader,
201202
YamlMappingNode tocEntry,
202203
string? parent,
@@ -259,14 +260,14 @@ static void ReadBlock(
259260
topLevelSource ??= sourceUri;
260261
parentSource ??= sourceUri;
261262

262-
var tocTopLevelMapping = new TocTopLevelMapping
263+
var tocTopLevelMapping = new NavigationTocMapping
263264
{
264265
Source = sourceUri,
265266
SourcePathPrefix = pathPrefix,
266267
TopLevelSource = topLevelSource,
267268
ParentSource = parentSource
268269
};
269-
entries.Add(new KeyValuePair<Uri, TocTopLevelMapping>(sourceUri, tocTopLevelMapping));
270+
entries.Add(new KeyValuePair<Uri, NavigationTocMapping>(sourceUri, tocTopLevelMapping));
270271

271272
foreach (var entry in tocEntry.Children)
272273
{

src/tooling/docs-assembler/Building/PublishEnvironmentUriResolver.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ namespace Documentation.Assembler.Building;
1111

1212
public class PublishEnvironmentUriResolver : IUriEnvironmentResolver
1313
{
14-
private readonly FrozenDictionary<Uri, TocTopLevelMapping> _topLevelMappings;
14+
private readonly FrozenDictionary<Uri, NavigationTocMapping> _topLevelMappings;
1515
private Uri BaseUri { get; }
1616

1717
private PublishEnvironment PublishEnvironment { get; }
1818

1919
private IReadOnlyList<string> TableOfContentsPrefixes { get; }
2020

21-
public PublishEnvironmentUriResolver(FrozenDictionary<Uri, TocTopLevelMapping> topLevelMappings, PublishEnvironment environment)
21+
public PublishEnvironmentUriResolver(FrozenDictionary<Uri, NavigationTocMapping> topLevelMappings, PublishEnvironment environment)
2222
{
2323
_topLevelMappings = topLevelMappings;
2424
PublishEnvironment = environment;

0 commit comments

Comments
 (0)