-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathblog.go
78 lines (71 loc) · 2.02 KB
/
blog.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package website
import "sort"
// TagStatus is used for rendering the tag status to a user.
type TagStatus int
const (
// TagStatusNone indicates the tag is not present in the filtered posts.
// The tag should be disabled and not checked.
TagStatusNone TagStatus = iota
// TagStatusChecked indicates the tag is checked.
// The tag should be clickable and checked.
TagStatusChecked
// TagStatusRemaining indicates the tag is present in the filtered posts.
// The tag should be clickable and not checked.
TagStatusRemaining
)
func sortTags[T any](tags map[string]T) []string {
sorted := make([]string, 0, len(tags))
for tag := range tags {
sorted = append(sorted, tag)
}
sort.Strings(sorted)
return sorted
}
// func authorNames(authors []Member) []string {
// names := make([]string, 0, len(authors))
// for _, author := range authors {
// names = append(names, author.Name)
// }
// return names
// }
// FilterBlogPosts filters the blog posts by the given tags.
// It returns the filtered posts and the status of each tag.
func FilterBlogPosts(
posts *Posts,
filterTags []string,
) ([]*Post, map[string]TagStatus) {
filtered := make([]*Post, 0)
remainingTags := make(map[string]TagStatus, len(posts.Tags))
defaultTagStatus := TagStatusNone
if len(filterTags) == 0 {
defaultTagStatus = TagStatusRemaining
}
for tag := range posts.Tags {
remainingTags[tag] = defaultTagStatus
}
if len(filterTags) == 0 {
return posts.Blog, remainingTags
}
// Iterate over each blog post.
// Check all the filter tags are present in the post, else skip it.
for _, post := range posts.Blog {
filterPost := false
for _, tag := range filterTags {
if _, ok := post.Tags[tag]; !ok {
filterPost = true
}
}
if filterPost {
continue
}
filtered = append(filtered, post)
for tag := range post.Tags {
remainingTags[tag] = TagStatusRemaining
}
}
// Mark all tags we are filtering by as checked (because they are).
for _, tag := range filterTags {
remainingTags[tag] = TagStatusChecked
}
return filtered, remainingTags
}