From f70ee988c37b09db05c10e61a3c0a397f591bc24 Mon Sep 17 00:00:00 2001 From: Laurent Ellerbach Date: Thu, 7 Dec 2023 17:19:19 +0100 Subject: [PATCH 1/2] fixing index not generated on root --- .../DocFxTocGenerator.csproj | 2 +- src/DocFxTocGenerator/TocGenerator.cs | 31 ++++++++++--------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/DocFxTocGenerator/DocFxTocGenerator.csproj b/src/DocFxTocGenerator/DocFxTocGenerator.csproj index d3c6526..ea40d0a 100644 --- a/src/DocFxTocGenerator/DocFxTocGenerator.csproj +++ b/src/DocFxTocGenerator/DocFxTocGenerator.csproj @@ -18,7 +18,7 @@ - + diff --git a/src/DocFxTocGenerator/TocGenerator.cs b/src/DocFxTocGenerator/TocGenerator.cs index fc56fe6..018eec8 100644 --- a/src/DocFxTocGenerator/TocGenerator.cs +++ b/src/DocFxTocGenerator/TocGenerator.cs @@ -200,18 +200,18 @@ private static string WalkDirectoryTree(DirectoryInfo folder, TocItem yamlNode) _message.Verbose($"Items ordered in {folder.FullName}"); } - if (!string.IsNullOrWhiteSpace(yamlNode.Filename)) + // if indicated, add a folder index - but not for the root folder. + if (_options.AutoIndex) { - // if indicated, add a folder index - but not for the root folder. - if (_options.AutoIndex) + string indexFile = AddIndex(folder, yamlNode, GetOverrides(folder.Parent)); + if (!string.IsNullOrEmpty(indexFile)) { - string indexFile = AddIndex(folder, yamlNode, GetOverrides(folder.Parent)); - if (!string.IsNullOrEmpty(indexFile)) - { - yamlNode.Href = GetRelativePath(indexFile, _options.DocFolder); - } + yamlNode.Href = GetRelativePath(indexFile, _options.DocFolder); } - else + } + else + { + if (!string.IsNullOrWhiteSpace(yamlNode.Filename)) { if (yamlNode.Items != null && yamlNode.Items.Any()) { @@ -538,7 +538,9 @@ private static bool WriteIndex(string outputFile, TocItem yamlNode) // read lines if existing file. List lines = new List(); - lines.Add($"# {yamlNode.Title}"); + // We are taking the node name as title, if it's the root one, then the name of the parent folder + var title = string.IsNullOrWhiteSpace(yamlNode.Title) ? ToTitleCase(Directory.GetParent(outputFile).Name) : yamlNode.Title; + lines.Add($"# {title}"); lines.Add(string.Empty); foreach (TocItem item in yamlNode.Items) { @@ -575,11 +577,12 @@ private static void Serialize(IndentedTextWriter writer, TocItem tocItem, bool i if (!string.IsNullOrEmpty(tocItem.Title)) { writer.WriteLine($"- name: {tocItem.Title}"); - } - if (!string.IsNullOrEmpty(tocItem.Href)) - { - writer.WriteLine($" href: {tocItem.Href}"); + // href can't be singleton, you alwyas have to have a name before + if (!string.IsNullOrEmpty(tocItem.Href)) + { + writer.WriteLine($" href: {tocItem.Href}"); + } } if (tocItem.Items != null) From 1008680ad991e1b3505c79c70c421dbba6032d8a Mon Sep 17 00:00:00 2001 From: Martin Tirion <59616517+mtirionMSFT@users.noreply.github.com> Date: Thu, 7 Dec 2023 18:16:14 +0100 Subject: [PATCH 2/2] Update src/DocFxTocGenerator/TocGenerator.cs --- src/DocFxTocGenerator/TocGenerator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DocFxTocGenerator/TocGenerator.cs b/src/DocFxTocGenerator/TocGenerator.cs index 018eec8..8151697 100644 --- a/src/DocFxTocGenerator/TocGenerator.cs +++ b/src/DocFxTocGenerator/TocGenerator.cs @@ -578,7 +578,7 @@ private static void Serialize(IndentedTextWriter writer, TocItem tocItem, bool i { writer.WriteLine($"- name: {tocItem.Title}"); - // href can't be singleton, you alwyas have to have a name before + // href can't be singleton, you always have to have a name before if (!string.IsNullOrEmpty(tocItem.Href)) { writer.WriteLine($" href: {tocItem.Href}");