From 49129c898a39265dc3f141cf53c5802f01a08bc5 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Mon, 25 May 2026 03:17:45 +0530 Subject: [PATCH] feat: sort markdown files by directory depth The TUI file list sorted purely by note path, so markdown files in child directories could appear before files in the directory where Glow was launched. Rank discovered files by path depth first, then preserve the existing path sort within each depth so root-level documents are listed before nested documents. Fixes #272 --- ui/sort.go | 13 +++++++++++++ ui/sort_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 ui/sort_test.go diff --git a/ui/sort.go b/ui/sort.go index 4f389fb..5387e75 100644 --- a/ui/sort.go +++ b/ui/sort.go @@ -2,11 +2,24 @@ package ui import ( "cmp" + "path/filepath" "slices" + "strings" ) func sortMarkdowns(mds []*markdown) { slices.SortStableFunc(mds, func(a, b *markdown) int { + if n := cmp.Compare(markdownPathDepth(a.Note), markdownPathDepth(b.Note)); n != 0 { + return n + } return cmp.Compare(a.Note, b.Note) }) } + +func markdownPathDepth(path string) int { + path = filepath.ToSlash(filepath.Clean(path)) + if path == "." { + return 0 + } + return strings.Count(path, "/") +} diff --git a/ui/sort_test.go b/ui/sort_test.go new file mode 100644 index 0000000..3185850 --- /dev/null +++ b/ui/sort_test.go @@ -0,0 +1,33 @@ +package ui + +import "testing" + +func TestSortMarkdownsOrdersByDirectoryDepth(t *testing.T) { + mds := []*markdown{ + {Note: "docs/reference/api.md"}, + {Note: "docs/guide.md"}, + {Note: "README.md"}, + {Note: "CONTRIBUTING.md"}, + {Note: "docs/reference/cli.md"}, + } + + sortMarkdowns(mds) + + got := make([]string, 0, len(mds)) + for _, md := range mds { + got = append(got, md.Note) + } + + want := []string{ + "CONTRIBUTING.md", + "README.md", + "docs/guide.md", + "docs/reference/api.md", + "docs/reference/cli.md", + } + for i := range want { + if got[i] != want[i] { + t.Fatalf("expected %v, got %v", want, got) + } + } +}