-
Notifications
You must be signed in to change notification settings - Fork 17
/
content.go
50 lines (40 loc) · 896 Bytes
/
content.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 docs
import (
"embed"
"fmt"
"path"
"strings"
)
//go:embed content
var content embed.FS
type Page struct {
Content string `yaml:"-"`
}
func GetPagesContent() map[string]string {
docs := map[string]string{}
entries, err := content.ReadDir(path.Join("content", "en", "rules"))
if err != nil {
return docs
}
for _, entry := range entries {
ruleId := strings.TrimSuffix(entry.Name(), ".md")
page, err := GetPage(ruleId)
if err != nil {
continue
}
docs[ruleId] = page.Content
}
return docs
}
func GetPage(ruleId string) (*Page, error) {
doc, err := content.ReadFile(
path.Join("content", "en", "rules", ruleId+".md"))
if err != nil {
return nil, err
}
parts := strings.SplitAfterN(string(doc), "---\n", 3)
if len(parts) != 3 {
return nil, fmt.Errorf("invalid doc page %s.md", ruleId)
}
return &Page{Content: strings.TrimSpace(parts[2])}, nil
}