Debian Codegraph @

modules/caddyhttp/templates/frontmatter.go

raw symbols SCIP
1 package templates
2
3 import (
4 "encoding/json"
5 "fmt"
6 "strings"
7 "unicode"
8
9 "github.com/BurntSushi/toml"
10 "gopkg.in/yaml.v3"
11 )
12
13 func extractFrontMatter(input string) (map[string]any, string, error) {
14 // get the bounds of the first non-empty line
15 var firstLineStart, firstLineEnd int
16 lineEmpty := true
17 for i, b := range input {
18 if b == '\n' {
19 firstLineStart = firstLineEnd
20 if firstLineStart > 0 {
21 firstLineStart++ // skip newline character
22 }
23 firstLineEnd = i
24 if !lineEmpty {
25 break
26 }
27 continue
28 }
29 lineEmpty = lineEmpty && unicode.IsSpace(b)
30 }
31 firstLine := input[firstLineStart:firstLineEnd]
32
33 // ensure residue windows carriage return byte is removed
34 firstLine = strings.TrimSpace(firstLine)
35
36 // see what kind of front matter there is, if any
37 var closingFence []string
38 var fmParser func([]byte) (map[string]any, error)
39 for _, fmType := range supportedFrontMatterTypes {
40 if firstLine == fmType.FenceOpen {
41 closingFence = fmType.FenceClose
42 fmParser = fmType.ParseFunc
43 break
44 }
45 }
46
47 if fmParser == nil {
48 // no recognized front matter; whole document is body
49 return nil, input, nil
50 }
51
52 // find end of front matter
53 var fmEndFence string
54 fmEndFenceStart := -1
55 for _, fence := range closingFence {
56 index := strings.Index(input[firstLineEnd:], "\n"+fence)
57 if index >= 0 {
58 fmEndFenceStart = index
59 fmEndFence = fence
60 break
61 }
62 }
63 if fmEndFenceStart < 0 {
64 return nil, "", fmt.Errorf("unterminated front matter")
65 }
66 fmEndFenceStart += firstLineEnd + 1 // add 1 to account for newline
67
68 // extract and parse front matter
69 frontMatter := input[firstLineEnd:fmEndFenceStart]
70 fm, err := fmParser([]byte(frontMatter))
71 if err != nil {
72 return nil, "", err
73 }
74
75 // the rest is the body
76 body := input[fmEndFenceStart+len(fmEndFence):]
77
78 return fm, body, nil
79 }
80
81 func yamlFrontMatter(input []byte) (map[string]any, error) {
82 m := make(map[string]any)
83 err := yaml.Unmarshal(input, &m)
84 return m, err
85 }
86
87 func tomlFrontMatter(input []byte) (map[string]any, error) {
88 m := make(map[string]any)
89 err := toml.Unmarshal(input, &m)
90 return m, err
91 }
92
93 func jsonFrontMatter(input []byte) (map[string]any, error) {
94 input = append([]byte{'{'}, input...)
95 input = append(input, '}')
96 m := make(map[string]any)
97 err := json.Unmarshal(input, &m)
98 return m, err
99 }
100
101 type parsedMarkdownDoc struct {
102 Meta map[string]any `json:"meta,omitempty"`
103 Body string `json:"body,omitempty"`
104 }
105
106 type frontMatterType struct {
107 FenceOpen string
108 FenceClose []string
109 ParseFunc func(input []byte) (map[string]any, error)
110 }
111
112 var supportedFrontMatterTypes = []frontMatterType{
113 {
114 FenceOpen: "---",
115 FenceClose: []string{"---", "..."},
116 ParseFunc: yamlFrontMatter,
117 },
118 {
119 FenceOpen: "+++",
120 FenceClose: []string{"+++"},
121 ParseFunc: tomlFrontMatter,
122 },
123 {
124 FenceOpen: "{",
125 FenceClose: []string{"}"},
126 ParseFunc: jsonFrontMatter,
127 },
128 }