2019-12-23 14:56:41 -05:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"unicode"
|
|
|
|
|
2022-04-12 14:48:42 -05:00
|
|
|
"github.com/BurntSushi/toml"
|
2022-06-01 16:15:20 -05:00
|
|
|
"gopkg.in/yaml.v3"
|
2019-12-23 14:56:41 -05:00
|
|
|
)
|
|
|
|
|
2022-08-02 15:39:09 -05:00
|
|
|
func extractFrontMatter(input string) (map[string]any, string, error) {
|
2019-12-23 14:56:41 -05:00
|
|
|
// get the bounds of the first non-empty line
|
|
|
|
var firstLineStart, firstLineEnd int
|
|
|
|
lineEmpty := true
|
|
|
|
for i, b := range input {
|
|
|
|
if b == '\n' {
|
|
|
|
firstLineStart = firstLineEnd
|
|
|
|
if firstLineStart > 0 {
|
|
|
|
firstLineStart++ // skip newline character
|
|
|
|
}
|
|
|
|
firstLineEnd = i
|
|
|
|
if !lineEmpty {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
lineEmpty = lineEmpty && unicode.IsSpace(b)
|
|
|
|
}
|
|
|
|
firstLine := input[firstLineStart:firstLineEnd]
|
|
|
|
|
2020-05-18 14:01:04 -05:00
|
|
|
// ensure residue windows carriage return byte is removed
|
|
|
|
firstLine = strings.TrimSpace(firstLine)
|
|
|
|
|
2019-12-23 14:56:41 -05:00
|
|
|
// see what kind of front matter there is, if any
|
2020-06-15 13:38:51 -05:00
|
|
|
var closingFence []string
|
2022-08-02 15:39:09 -05:00
|
|
|
var fmParser func([]byte) (map[string]any, error)
|
2020-06-15 13:38:51 -05:00
|
|
|
for _, fmType := range supportedFrontMatterTypes {
|
|
|
|
if firstLine == fmType.FenceOpen {
|
|
|
|
closingFence = fmType.FenceClose
|
|
|
|
fmParser = fmType.ParseFunc
|
2024-09-25 15:30:56 -05:00
|
|
|
break
|
2020-06-15 13:38:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if fmParser == nil {
|
2019-12-23 14:56:41 -05:00
|
|
|
// no recognized front matter; whole document is body
|
|
|
|
return nil, input, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// find end of front matter
|
2020-06-15 13:38:51 -05:00
|
|
|
var fmEndFence string
|
|
|
|
fmEndFenceStart := -1
|
|
|
|
for _, fence := range closingFence {
|
|
|
|
index := strings.Index(input[firstLineEnd:], "\n"+fence)
|
|
|
|
if index >= 0 {
|
|
|
|
fmEndFenceStart = index
|
|
|
|
fmEndFence = fence
|
2020-07-08 17:46:56 -05:00
|
|
|
break
|
2020-06-15 13:38:51 -05:00
|
|
|
}
|
|
|
|
}
|
2019-12-23 14:56:41 -05:00
|
|
|
if fmEndFenceStart < 0 {
|
|
|
|
return nil, "", fmt.Errorf("unterminated front matter")
|
|
|
|
}
|
|
|
|
fmEndFenceStart += firstLineEnd + 1 // add 1 to account for newline
|
|
|
|
|
|
|
|
// extract and parse front matter
|
|
|
|
frontMatter := input[firstLineEnd:fmEndFenceStart]
|
|
|
|
fm, err := fmParser([]byte(frontMatter))
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// the rest is the body
|
2020-06-15 13:38:51 -05:00
|
|
|
body := input[fmEndFenceStart+len(fmEndFence):]
|
2019-12-23 14:56:41 -05:00
|
|
|
|
|
|
|
return fm, body, nil
|
|
|
|
}
|
|
|
|
|
2022-08-02 15:39:09 -05:00
|
|
|
func yamlFrontMatter(input []byte) (map[string]any, error) {
|
|
|
|
m := make(map[string]any)
|
2019-12-23 14:56:41 -05:00
|
|
|
err := yaml.Unmarshal(input, &m)
|
|
|
|
return m, err
|
|
|
|
}
|
|
|
|
|
2022-08-02 15:39:09 -05:00
|
|
|
func tomlFrontMatter(input []byte) (map[string]any, error) {
|
|
|
|
m := make(map[string]any)
|
2019-12-23 14:56:41 -05:00
|
|
|
err := toml.Unmarshal(input, &m)
|
|
|
|
return m, err
|
|
|
|
}
|
|
|
|
|
2022-08-02 15:39:09 -05:00
|
|
|
func jsonFrontMatter(input []byte) (map[string]any, error) {
|
2019-12-23 14:56:41 -05:00
|
|
|
input = append([]byte{'{'}, input...)
|
|
|
|
input = append(input, '}')
|
2022-08-02 15:39:09 -05:00
|
|
|
m := make(map[string]any)
|
2019-12-23 14:56:41 -05:00
|
|
|
err := json.Unmarshal(input, &m)
|
|
|
|
return m, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type parsedMarkdownDoc struct {
|
2022-08-02 15:39:09 -05:00
|
|
|
Meta map[string]any `json:"meta,omitempty"`
|
|
|
|
Body string `json:"body,omitempty"`
|
2019-12-23 14:56:41 -05:00
|
|
|
}
|
|
|
|
|
2020-06-15 13:38:51 -05:00
|
|
|
type frontMatterType struct {
|
|
|
|
FenceOpen string
|
|
|
|
FenceClose []string
|
2022-08-02 15:39:09 -05:00
|
|
|
ParseFunc func(input []byte) (map[string]any, error)
|
2020-06-15 13:38:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var supportedFrontMatterTypes = []frontMatterType{
|
|
|
|
{
|
|
|
|
FenceOpen: "---",
|
|
|
|
FenceClose: []string{"---", "..."},
|
|
|
|
ParseFunc: yamlFrontMatter,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
FenceOpen: "+++",
|
|
|
|
FenceClose: []string{"+++"},
|
|
|
|
ParseFunc: tomlFrontMatter,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
FenceOpen: "{",
|
|
|
|
FenceClose: []string{"}"},
|
|
|
|
ParseFunc: jsonFrontMatter,
|
|
|
|
},
|
|
|
|
}
|