From 7214ac014752483b81d7cc1d6c8a4aae87d378b2 Mon Sep 17 00:00:00 2001 From: Martin Tirion Date: Wed, 20 Nov 2024 23:44:33 +0100 Subject: [PATCH] Fixed override for folders to be used over index or readme --- .../GenerateTocActionTests.cs | 50 +++++++++++++++++++ .../Helpers/MockFileService.cs | 2 + .../TableOfContentsServiceTests.cs | 6 +-- .../Actions/ContentInventoryAction.cs | 1 + .../FileService/FileInfoService.cs | 1 + .../FileService/FolderFileBase.cs | 6 +++ .../TableOfContents/TableOfContentsService.cs | 8 +++ 7 files changed, 71 insertions(+), 3 deletions(-) diff --git a/src/DocFxTocGenerator/DocFxTocGenerator.Test/GenerateTocActionTests.cs b/src/DocFxTocGenerator/DocFxTocGenerator.Test/GenerateTocActionTests.cs index 0bc4734..ce3792e 100644 --- a/src/DocFxTocGenerator/DocFxTocGenerator.Test/GenerateTocActionTests.cs +++ b/src/DocFxTocGenerator/DocFxTocGenerator.Test/GenerateTocActionTests.cs @@ -334,4 +334,54 @@ public async Task Run_Issue_27_Run2() string toc = _fileService.ReadAllText(_fileService.GetFullPath("toc.yml")); toc.Should().Be(expected); } + + [Fact] + public async Task Run_Issue_77() + { + // arrange + _fileService.Files.Clear(); + _fileService.AddFile(string.Empty, "README.md", string.Empty.AddHeading("Issue 77 override problem", 1).AddParagraphs(1)); + _fileService.AddFile(string.Empty, ".override", +@"override-folder;The Folder Override"); + var folder = _fileService.AddFolder("override-folder"); + _fileService.AddFile(folder, "README.md", string.Empty.AddHeading("Title of the README", 1).AddParagraphs(1)); + _fileService.AddFile(folder, "content.md", string.Empty.AddHeading("Some content", 1).AddParagraphs(1)); + + ContentInventoryAction content = new(_fileService.Root, useOrder: false, useIgnore: false, useOverride: true, camelCasing: false, _fileService, _logger); + await content.RunAsync(); + + EnsureIndexAction index = new(content.RootFolder!, Index.IndexGenerationStrategy.Never, camelCasing: false, _fileService, _logger); + await index.RunAsync(); + + GenerateTocAction action = new( + _fileService.Root, + content.RootFolder!, + folderReferenceStrategy: TocFolderReferenceStrategy.IndexReadme, + orderStrategy: TocOrderStrategy.All, + maxDepth: 0, + _fileService, + _logger); + + int originalCount = _fileService.Files.Count(); + + string expected = +@"# This is an automatically generated file +- name: Issue 77 override problem + href: README.md +- name: The Folder Override + href: override-folder/README.md + items: + - name: Some content + href: override-folder/content.md +".NormalizeContent(); + + // act + ReturnCode ret = await action.RunAsync(); + + // assert + ret.Should().Be(ReturnCode.Normal); + _fileService.Files.Should().HaveCount(originalCount + 1); + string toc = _fileService.ReadAllText(_fileService.GetFullPath("toc.yml")); + toc.Should().Be(expected); + } } diff --git a/src/DocFxTocGenerator/DocFxTocGenerator.Test/Helpers/MockFileService.cs b/src/DocFxTocGenerator/DocFxTocGenerator.Test/Helpers/MockFileService.cs index d9f46d4..3d2413d 100644 --- a/src/DocFxTocGenerator/DocFxTocGenerator.Test/Helpers/MockFileService.cs +++ b/src/DocFxTocGenerator/DocFxTocGenerator.Test/Helpers/MockFileService.cs @@ -131,6 +131,8 @@ public void FillDemoSet() folder = AddFolder("deep-tree"); folder = AddFolder("deep-tree/level1"); + AddFile(folder, ".override", +@"level2;The Second Level"); folder = AddFolder("deep-tree/level1/level2"); AddFile(folder, "index.md", string.Empty .AddHeading("Index of LEVEL 2", 1) diff --git a/src/DocFxTocGenerator/DocFxTocGenerator.Test/TableOfContentsServiceTests.cs b/src/DocFxTocGenerator/DocFxTocGenerator.Test/TableOfContentsServiceTests.cs index 492a999..dfb9bef 100644 --- a/src/DocFxTocGenerator/DocFxTocGenerator.Test/TableOfContentsServiceTests.cs +++ b/src/DocFxTocGenerator/DocFxTocGenerator.Test/TableOfContentsServiceTests.cs @@ -136,7 +136,7 @@ public async Task GetTocItems_GetTocFolderReferenceIndex() toc.Href.Should().BeNull(); toc.Items.Count.Should().Be(1); - toc.Items[0].Name.Should().Be("Index of LEVEL 2"); + toc.Items[0].Name.Should().Be("The Second Level"); toc.Items[0].Href.Should().Be("deep-tree/level1/level2/index.md"); } @@ -429,7 +429,7 @@ public async Task SerializeTocItem_Hierarchy() items: - name: Level1 items: - - name: Level2 + - name: The Second Level items: - name: Index of LEVEL 2 href: deep-tree/level1/level2/index.md @@ -548,7 +548,7 @@ public async Task SerializeTocItem_Hierarchy_CamelCase() items: - name: level1 items: - - name: level2 + - name: The Second Level items: - name: index of LEVEL 2 href: deep-tree/level1/level2/index.md diff --git a/src/DocFxTocGenerator/DocFxTocGenerator/Actions/ContentInventoryAction.cs b/src/DocFxTocGenerator/DocFxTocGenerator/Actions/ContentInventoryAction.cs index c833661..52838e1 100644 --- a/src/DocFxTocGenerator/DocFxTocGenerator/Actions/ContentInventoryAction.cs +++ b/src/DocFxTocGenerator/DocFxTocGenerator/Actions/ContentInventoryAction.cs @@ -127,6 +127,7 @@ public Task RunAsync() if (parent.OverrideList.TryGetValue(folder.Name, out string? name)) { folder.DisplayName = name; + folder.IsDisplayNameOverride = true; } } diff --git a/src/DocFxTocGenerator/DocFxTocGenerator/FileService/FileInfoService.cs b/src/DocFxTocGenerator/DocFxTocGenerator/FileService/FileInfoService.cs index ad93c28..97df253 100644 --- a/src/DocFxTocGenerator/DocFxTocGenerator/FileService/FileInfoService.cs +++ b/src/DocFxTocGenerator/DocFxTocGenerator/FileService/FileInfoService.cs @@ -73,6 +73,7 @@ public FileData CreateFileData(FolderData folder, string file) { // override the display name filedata.DisplayName = name; + filedata.IsDisplayNameOverride = true; } else { diff --git a/src/DocFxTocGenerator/DocFxTocGenerator/FileService/FolderFileBase.cs b/src/DocFxTocGenerator/DocFxTocGenerator/FileService/FolderFileBase.cs index 092996a..ca6bd7d 100644 --- a/src/DocFxTocGenerator/DocFxTocGenerator/FileService/FolderFileBase.cs +++ b/src/DocFxTocGenerator/DocFxTocGenerator/FileService/FolderFileBase.cs @@ -27,6 +27,12 @@ public record FolderFileBase /// public string DisplayName { get; set; } = string.Empty; + /// + /// Gets or sets a value indicating whether the display name is coming from the .override. + /// This should always be the preference then. + /// + public bool IsDisplayNameOverride { get; set; } + /// /// Gets or sets the sequence value of this item. /// diff --git a/src/DocFxTocGenerator/DocFxTocGenerator/TableOfContents/TableOfContentsService.cs b/src/DocFxTocGenerator/DocFxTocGenerator/TableOfContents/TableOfContentsService.cs index c3f4778..d484770 100644 --- a/src/DocFxTocGenerator/DocFxTocGenerator/TableOfContents/TableOfContentsService.cs +++ b/src/DocFxTocGenerator/DocFxTocGenerator/TableOfContents/TableOfContentsService.cs @@ -3,6 +3,7 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. // using System.CodeDom.Compiler; +using System.Xml.Linq; using DocFxTocGenerator.FileService; using Microsoft.Extensions.Logging; @@ -62,6 +63,13 @@ public TocItem GetTocItemsForFolder(FolderData folder, int depth) Base = folder, }; + // we took one of the defaults, but if the parent has an override for this folder + // the override must take preference. (fixing issue #77) + if (folder.IsDisplayNameOverride) + { + tocItem.Name = folder.DisplayName; + } + // first add all sub folders foreach (var subfolder in folder.Folders) {