-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse.go
More file actions
36 lines (32 loc) · 745 Bytes
/
Copy pathparse.go
File metadata and controls
36 lines (32 loc) · 745 Bytes
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
package main
import (
"fmt"
"strings"
"gopkg.in/yaml.v3"
)
func parseDocument(doc string) (meta TemplateMeta, body string, err error) {
frontmatter, body := separateDocument(doc)
if frontmatter == "" {
return TemplateMeta{}, body, nil
}
err = yaml.Unmarshal([]byte(frontmatter), &meta)
if err != nil {
err = fmt.Errorf("Failed to parse frontmatter: %w", err)
return
}
return meta, body, nil
}
func separateDocument(doc string) (frontmatter string, body string) {
doc = strings.TrimLeft(doc, " \n")
sep := "---\n"
if !strings.HasPrefix(doc, sep) {
return "", doc
}
parts := strings.SplitN(doc, sep, 3)
if len(parts) < 3 {
return "", doc
}
frontmatter = parts[1]
body = strings.TrimLeft(parts[2], "\n ")
return
}