This commit is contained in:
Asish Kumar 2026-05-25 03:28:53 +05:30 committed by GitHub
commit 4d823cf7f5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 46 additions and 0 deletions

View file

@ -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, "/")
}

33
ui/sort_test.go Normal file
View file

@ -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)
}
}
}