Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DocFxTocGenerator: fixed ordering of folders #88

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
344 changes: 344 additions & 0 deletions src/DocFxTocGenerator/DocFxTocGenerator.Test/GenerateTocActionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,348 @@ public async Task Run_Issue_77()
string toc = _fileService.ReadAllText(_fileService.GetFullPath("toc.yml"));
toc.Should().Be(expected);
}

[Fact]
public async Task Run_Issue_86_OrderingAll()
{
// arrange
Issue_86_Setup();

ContentInventoryAction content = new(_fileService.Root, useOrder: true, useIgnore: false, useOverride: false, 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.None,
orderStrategy: TocOrderStrategy.All, // this is distinctive for this test
maxDepth: 0,
_fileService,
_logger);

int originalCount = _fileService.Files.Count();

string expected =
@"# This is an automatically generated file
- name: Docs
- name: FilesOnly
items:
- name: C Document
href: FilesOnly/C.md
- name: B Document
href: FilesOnly/B.md
- name: A Document
href: FilesOnly/A.md
- name: A1 Document
href: FilesOnly/A1.md
- name: A2 Document
href: FilesOnly/A2.md
- name: FoldersAndFiles
items:
- name: BB
items:
- name: BB Document
href: FoldersAndFiles/BB/README.md
- name: B Document
href: FoldersAndFiles/B.md
- name: AA
items:
- name: AA Document
href: FoldersAndFiles/AA/README.md
- name: A Document
href: FoldersAndFiles/A.md
- name: C Document
href: FoldersAndFiles/C.md
- name: CC
items:
- name: CC Document
href: FoldersAndFiles/CC/README.md
- name: A1 Document
href: FoldersAndFiles/A1.md
- name: A2 Document
href: FoldersAndFiles/A2.md
- name: FoldersOnly
items:
- name: CC
items:
- name: CC Document
href: FoldersOnly/CC/README.md
- name: BB
items:
- name: BB Document
href: FoldersOnly/BB/README.md
- name: AA
items:
- name: AA Document
href: FoldersOnly/AA/README.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);
}

[Fact]
public async Task Run_Issue_86_OrderingFilesFirst()
{
// arrange
Issue_86_Setup();

ContentInventoryAction content = new(_fileService.Root, useOrder: true, useIgnore: false, useOverride: false, 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.None,
orderStrategy: TocOrderStrategy.FilesFirst, // this is distinctive for this test
maxDepth: 0,
_fileService,
_logger);

int originalCount = _fileService.Files.Count();

string expected =
@"# This is an automatically generated file
- name: Docs
- name: FilesOnly
items:
- name: C Document
href: FilesOnly/C.md
- name: B Document
href: FilesOnly/B.md
- name: A Document
href: FilesOnly/A.md
- name: A1 Document
href: FilesOnly/A1.md
- name: A2 Document
href: FilesOnly/A2.md
- name: FoldersAndFiles
items:
- name: B Document
href: FoldersAndFiles/B.md
- name: A Document
href: FoldersAndFiles/A.md
- name: C Document
href: FoldersAndFiles/C.md
- name: A1 Document
href: FoldersAndFiles/A1.md
- name: A2 Document
href: FoldersAndFiles/A2.md
- name: BB
items:
- name: BB Document
href: FoldersAndFiles/BB/README.md
- name: AA
items:
- name: AA Document
href: FoldersAndFiles/AA/README.md
- name: CC
items:
- name: CC Document
href: FoldersAndFiles/CC/README.md
- name: FoldersOnly
items:
- name: CC
items:
- name: CC Document
href: FoldersOnly/CC/README.md
- name: BB
items:
- name: BB Document
href: FoldersOnly/BB/README.md
- name: AA
items:
- name: AA Document
href: FoldersOnly/AA/README.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);
}

[Fact]
public async Task Run_Issue_86_OrderingFoldersFirst()
{
// arrange
Issue_86_Setup();

ContentInventoryAction content = new(_fileService.Root, useOrder: true, useIgnore: false, useOverride: false, 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.None,
orderStrategy: TocOrderStrategy.FoldersFirst, // this is distinctive for this test
maxDepth: 0,
_fileService,
_logger);

int originalCount = _fileService.Files.Count();

string expected =
@"# This is an automatically generated file
- name: Docs
- name: FilesOnly
items:
- name: C Document
href: FilesOnly/C.md
- name: B Document
href: FilesOnly/B.md
- name: A Document
href: FilesOnly/A.md
- name: A1 Document
href: FilesOnly/A1.md
- name: A2 Document
href: FilesOnly/A2.md
- name: FoldersAndFiles
items:
- name: BB
items:
- name: BB Document
href: FoldersAndFiles/BB/README.md
- name: AA
items:
- name: AA Document
href: FoldersAndFiles/AA/README.md
- name: CC
items:
- name: CC Document
href: FoldersAndFiles/CC/README.md
- name: B Document
href: FoldersAndFiles/B.md
- name: A Document
href: FoldersAndFiles/A.md
- name: C Document
href: FoldersAndFiles/C.md
- name: A1 Document
href: FoldersAndFiles/A1.md
- name: A2 Document
href: FoldersAndFiles/A2.md
- name: FoldersOnly
items:
- name: CC
items:
- name: CC Document
href: FoldersOnly/CC/README.md
- name: BB
items:
- name: BB Document
href: FoldersOnly/BB/README.md
- name: AA
items:
- name: AA Document
href: FoldersOnly/AA/README.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);
}

private void Issue_86_Setup()
{
_fileService.Files.Clear();

var folder = _fileService.AddFolder("FilesOnly");
_fileService.AddFile(folder, ".order",
@"C
B
A");
_fileService.AddFile(folder, "A.md", string.Empty
.AddHeading("A Document", 1)
.AddParagraphs(1));
_fileService.AddFile(folder, "B.md", string.Empty
.AddHeading("B Document", 1)
.AddParagraphs(1));
_fileService.AddFile(folder, "C.md", string.Empty
.AddHeading("C Document", 1)
.AddParagraphs(1));
_fileService.AddFile(folder, "A1.md", string.Empty
.AddHeading("A1 Document", 1)
.AddParagraphs(1));
_fileService.AddFile(folder, "A2.md", string.Empty
.AddHeading("A2 Document", 1)
.AddParagraphs(1));

folder = _fileService.AddFolder("FoldersOnly");
_fileService.AddFile(folder, ".order",
@"CC
BB
AA");
folder = _fileService.AddFolder("FoldersOnly/AA");
_fileService.AddFile(folder, "README.md", string.Empty
.AddHeading("AA Document", 1)
.AddParagraphs(1));
folder = _fileService.AddFolder("FoldersOnly/BB");
_fileService.AddFile(folder, "README.md", string.Empty
.AddHeading("BB Document", 1)
.AddParagraphs(1));
folder = _fileService.AddFolder("FoldersOnly/CC");
_fileService.AddFile(folder, "README.md", string.Empty
.AddHeading("CC Document", 1)
.AddParagraphs(1));

folder = _fileService.AddFolder("FoldersAndFiles");
_fileService.AddFile(folder, ".order",
@"BB
B
AA
A
C
CC");
_fileService.AddFile(folder, "A.md", string.Empty
.AddHeading("A Document", 1)
.AddParagraphs(1));
_fileService.AddFile(folder, "B.md", string.Empty
.AddHeading("B Document", 1)
.AddParagraphs(1));
_fileService.AddFile(folder, "C.md", string.Empty
.AddHeading("C Document", 1)
.AddParagraphs(1));
_fileService.AddFile(folder, "A1.md", string.Empty
.AddHeading("A1 Document", 1)
.AddParagraphs(1));
_fileService.AddFile(folder, "A2.md", string.Empty
.AddHeading("A2 Document", 1)
.AddParagraphs(1));
folder = _fileService.AddFolder("FoldersAndFiles/AA");
_fileService.AddFile(folder, "README.md", string.Empty
.AddHeading("AA Document", 1)
.AddParagraphs(1));
folder = _fileService.AddFolder("FoldersAndFiles/BB");
_fileService.AddFile(folder, "README.md", string.Empty
.AddHeading("BB Document", 1)
.AddParagraphs(1));
folder = _fileService.AddFolder("FoldersAndFiles/CC");
_fileService.AddFile(folder, "README.md", string.Empty
.AddHeading("CC Document", 1)
.AddParagraphs(1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Copyright (c) DocFx Companion Tools. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>
using System.Diagnostics;
using System.Linq;
using DocFxTocGenerator.ConfigFiles;
using DocFxTocGenerator.FileService;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -129,6 +131,14 @@ public Task<ReturnCode> RunAsync()
folder.DisplayName = name;
folder.IsDisplayNameOverride = true;
}

// see if we have a sequence for the folder name
if (parent.OrderList.FindIndex(x => x.Equals(folder.Name, StringComparison.Ordinal)) != -1)
{
// set the order
folder.Sequence = parent.OrderList.FindIndex(x => x.Equals(folder.Name, StringComparison.Ordinal));
Debug.WriteLine($"Folder '{parent.Path}' Subfolder '{folder.Name}' Sequence '{folder.Sequence}'");
}
}

// read config files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) DocFx Companion Tools. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>
using System.Diagnostics;
using System.Text.RegularExpressions;
using Markdig;
using Markdig.Syntax;
Expand Down Expand Up @@ -65,6 +66,7 @@ public FileData CreateFileData(FolderData folder, string file)
{
// set the order
filedata.Sequence = folder.OrderList.FindIndex(x => x.Equals(fname, comparison));
Debug.WriteLine($"Folder '{folder.Path}' File '{filedata.Name}' Sequence '{filedata.Sequence}'");
}

var title = GetFileDisplayName(file, _camelCasing);
Expand Down
Loading