mirror of
https://github.com/charmbracelet/glow.git
synced 2026-08-23 00:24:17 +02:00
Merge 49129c898a into 53788271b3
This commit is contained in:
commit
4d823cf7f5
2 changed files with 46 additions and 0 deletions
13
ui/sort.go
13
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, "/")
|
||||
}
|
||||
|
|
|
|||
33
ui/sort_test.go
Normal file
33
ui/sort_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue