Skip to content

Commit 18cabd2

Browse files
Implements #1552: [Feature Request] Change how the title in the head is defined.
1 parent 9dddf81 commit 18cabd2

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

src/Elastic.Markdown/DocumentationGenerator.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,36 @@ public async Task ResolveDirectoryTree(Cancel ctx)
9999
_logger.LogInformation("Resolving tree");
100100
await DocumentationSet.Tree.Resolve(ctx);
101101
_logger.LogInformation("Resolved tree");
102+
ReportDuplicateTitles(DocumentationSet.Tree.ResolvedMarkdownFiles);
103+
}
104+
105+
private void ReportDuplicateTitles(List<MarkdownFile> files)
106+
{
107+
// Create a dictionary where keys are the titles
108+
// and values are files with that title
109+
var titleMap = new Dictionary<string, List<MarkdownFile>>(StringComparer.OrdinalIgnoreCase);
110+
foreach (var file in files)
111+
{
112+
if (string.IsNullOrWhiteSpace(file.Title))
113+
continue;
114+
// If there is no entry for this title, create it and
115+
// initialize it to an empty list
116+
if (!titleMap.TryGetValue(file.Title, out var list))
117+
titleMap[file.Title] = [];
118+
titleMap[file.Title].Add(file);
119+
}
120+
// Go through all the titles and if a title has multiple files, report it
121+
foreach (var kv in titleMap)
122+
{
123+
var documentFiles = kv.Value;
124+
if (documentFiles.Count > 1)
125+
{
126+
var fileList = string.Join(", ", documentFiles.Select(f => f.RelativePath));
127+
foreach (var documentFile in documentFiles)
128+
Context.Collector.EmitHint(documentFile.RelativePath,
129+
$"Duplicate titles found. The title '{kv.Key}' is used in files: {{{fileList}}}");
130+
}
131+
}
102132
}
103133

104134
public async Task<GenerationResult> GenerateAll(Cancel ctx)

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public class DocumentationGroup : INodeNavigationItem<MarkdownFile, INavigationI
4646

4747
private readonly IRootNavigationItem<MarkdownFile, INavigationItem>? _root;
4848

49+
public List<MarkdownFile> ResolvedMarkdownFiles { get; set; }
50+
4951
protected virtual IRootNavigationItem<MarkdownFile, INavigationItem> DefaultNavigation =>
5052
_root ?? throw new InvalidOperationException("root navigation's model is not of type MarkdownFile");
5153

@@ -70,6 +72,7 @@ protected DocumentationGroup(string folderName,
7072
// We'll need to address this more structurally
7173
// ReSharper disable VirtualMemberCallInConstructor
7274
_root = toplevelTree;
75+
ResolvedMarkdownFiles = [];
7376
toplevelTree ??= DefaultNavigation;
7477
if (parent?.Depth == 0)
7578
toplevelTree = DefaultNavigation;
@@ -225,10 +228,22 @@ public async Task Resolve(Cancel ctx = default)
225228
if (_resolved)
226229
return;
227230

228-
await Parallel.ForEachAsync(FilesInOrder, ctx, async (file, token) => await file.MinimalParseAsync(token));
229-
await Parallel.ForEachAsync(GroupsInOrder, ctx, async (group, token) => await group.Resolve(token));
231+
// First add the index file
232+
ResolvedMarkdownFiles.Add(Index);
233+
// Then add all the files in this group
234+
ResolvedMarkdownFiles.AddRange(FilesInOrder);
235+
// Then add all files in subgroups, breadth first
236+
var treeGroups = new Queue<DocumentationGroup>(GroupsInOrder);
237+
while (treeGroups.Count > 0)
238+
{
239+
var group = treeGroups.Dequeue();
240+
ResolvedMarkdownFiles.Add(group.Index);
241+
ResolvedMarkdownFiles.AddRange(group.FilesInOrder);
242+
foreach (var subgroup in group.GroupsInOrder)
243+
treeGroups.Enqueue(subgroup);
244+
}
230245

231-
_ = await Index.MinimalParseAsync(ctx);
246+
await Parallel.ForEachAsync(ResolvedMarkdownFiles, ctx, async (file, token) => await file.MinimalParseAsync(token));
232247

233248
_resolved = true;
234249
}

0 commit comments

Comments
 (0)