-
Notifications
You must be signed in to change notification settings - Fork 0
/
docs.go
50 lines (39 loc) · 877 Bytes
/
docs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package builder
import (
"fmt"
"sort"
"strings"
"github.com/bmatcuk/doublestar/v4"
"github.com/samber/lo"
)
type Docs []*Doc
func NewDocs(
sourcePath string,
pattern string,
limit int,
filterIndexes bool,
) (Docs, error) {
matches, err := doublestar.FilepathGlob(pattern)
if err != nil {
return nil, fmt.Errorf("could not matches (%q): %w", pattern, err)
}
if filterIndexes {
matches = lo.Filter(matches, func(match string, _ int) bool {
return !strings.HasSuffix(match, "index.md")
})
}
sort.Strings(matches)
matches = lo.Reverse(matches)
if limit > 0 && len(matches) > limit {
matches = matches[:limit]
}
docs := Docs{}
for _, match := range matches {
doc, err := NewDoc(match, sourcePath)
if err != nil {
return nil, fmt.Errorf("could not open doc (%s): %w", match, err)
}
docs = append(docs, doc)
}
return docs, nil
}