Skip to content

Commit 44e4627

Browse files
authored
Refactor TOC processing to handle excluded and unreachable files (#278)
* Refactor TOC processing to handle excluded and unreachable files Updated the TOC processing logic to emit errors for excluded or unreachable files and ensure valid navigation indexes. These changes improve diagnostics during documentation builds and prevent invalid entries in the output structure. * revert changes to assembly conf, unrelated to this PR
1 parent 4ae4f03 commit 44e4627

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

src/Elastic.Markdown/IO/DocumentationSet.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,18 @@ public DocumentationSet(BuildContext context)
6464
.ToDictionary(g => g.Key, g => g.ToArray());
6565

6666
var fileIndex = 0;
67-
Tree = new DocumentationGroup(Configuration.TableOfContents, FlatMappedFiles, folderFiles, ref fileIndex)
67+
Tree = new DocumentationGroup(Context, Configuration.TableOfContents, FlatMappedFiles, folderFiles, ref fileIndex)
6868
{
6969
Parent = null
7070
};
7171

72-
MarkdownFiles = Files.OfType<MarkdownFile>().ToDictionary(i => i.NavigationIndex, i => i).ToFrozenDictionary();
72+
var markdownFiles = Files.OfType<MarkdownFile>().ToArray();
73+
74+
var excludedChildren = markdownFiles.Where(f => f.NavigationIndex == -1).ToArray();
75+
foreach (var excludedChild in excludedChildren)
76+
Context.EmitError(Context.ConfigurationPath, $"{excludedChild.RelativePath} is unreachable in the TOC because one of its parents matches exclusion glob");
77+
78+
MarkdownFiles = markdownFiles.Where(f => f.NavigationIndex > -1).ToDictionary(i => i.NavigationIndex, i => i).ToFrozenDictionary();
7379

7480
}
7581

src/Elastic.Markdown/IO/MarkdownFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public string? NavigationTitle
6161
public string FileName { get; }
6262
public string Url => $"{UrlPathPrefix}/{RelativePath.Replace(".md", ".html")}";
6363

64-
public int NavigationIndex { get; set; }
64+
public int NavigationIndex { get; internal set; } = -1;
6565

6666
private bool _instructionsParsed;
6767
private DocumentationGroup? _parent;

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

5+
using Elastic.Markdown.Diagnostics;
56
using Elastic.Markdown.IO.Configuration;
67

78
namespace Elastic.Markdown.IO.Navigation;
@@ -33,16 +34,16 @@ public class DocumentationGroup
3334
public int Depth { get; }
3435

3536
public DocumentationGroup(
37+
BuildContext context,
3638
IReadOnlyCollection<ITocItem> toc,
3739
IDictionary<string, DocumentationFile> lookup,
3840
IDictionary<string, DocumentationFile[]> folderLookup,
3941
ref int fileIndex,
4042
int depth = 0,
41-
MarkdownFile? index = null
42-
)
43+
MarkdownFile? index = null)
4344
{
4445
Depth = depth;
45-
Index = ProcessTocItems(index, toc, lookup, folderLookup, depth, ref fileIndex, out var groups, out var files, out var navigationItems);
46+
Index = ProcessTocItems(context, index, toc, lookup, folderLookup, depth, ref fileIndex, out var groups, out var files, out var navigationItems);
4647

4748
GroupsInOrder = groups;
4849
FilesInOrder = files;
@@ -55,6 +56,7 @@ public DocumentationGroup(
5556
}
5657

5758
private MarkdownFile? ProcessTocItems(
59+
BuildContext context,
5860
MarkdownFile? configuredIndex,
5961
IReadOnlyCollection<ITocItem> toc,
6062
IDictionary<string, DocumentationFile> lookup,
@@ -73,15 +75,26 @@ public DocumentationGroup(
7375
{
7476
if (tocItem is FileReference file)
7577
{
76-
if (!lookup.TryGetValue(file.Path, out var d) || d is not MarkdownFile md)
78+
if (!lookup.TryGetValue(file.Path, out var d))
79+
{
80+
context.EmitError(context.ConfigurationPath, $"The following file could not be located: {file.Path} it may be excluded from the build in docset.yml");
81+
continue;
82+
}
83+
if (d is ExcludedFile excluded && excluded.RelativePath.EndsWith(".md"))
84+
{
85+
context.EmitError(context.ConfigurationPath, $"{excluded.RelativePath} matches exclusion glob from docset.yml yet appears in TOC");
86+
continue;
87+
}
88+
if (d is not MarkdownFile md)
7789
continue;
7890

7991
md.Parent = this;
80-
md.NavigationIndex = ++fileIndex;
92+
var navigationIndex = Interlocked.Increment(ref fileIndex);
93+
md.NavigationIndex = navigationIndex;
8194

8295
if (file.Children.Count > 0 && d is MarkdownFile virtualIndex)
8396
{
84-
var group = new DocumentationGroup(file.Children, lookup, folderLookup, ref fileIndex, depth + 1, virtualIndex)
97+
var group = new DocumentationGroup(context, file.Children, lookup, folderLookup, ref fileIndex, depth + 1, virtualIndex)
8598
{
8699
Parent = this
87100
};
@@ -111,7 +124,7 @@ public DocumentationGroup(
111124
.ToArray();
112125
}
113126

114-
var group = new DocumentationGroup(children, lookup, folderLookup, ref fileIndex, depth + 1)
127+
var group = new DocumentationGroup(context, children, lookup, folderLookup, ref fileIndex, depth + 1)
115128
{
116129
Parent = this
117130
};

0 commit comments

Comments
 (0)