feat: add frontmatter extraction

This commit is contained in:
Justin Lin committed 2025-07-25 01:07:50 +10:00
1 parent 121f5a4511
commit 70837bf6b1
1 file changed
+26 -1
+26 -1
View File
@@ -9,6 +9,9 @@ import (
)
var tmpl = `
---
title: This is the title
---
Hello this is the text:
{{ sh "hledger bal assets" }}
`
@@ -22,11 +25,33 @@ func sh(command string) string {
return fmt.Sprintf("\n```\n%s\n```\n", string(out))
}
func parseDocument(doc string) (frontmatter string, body string, err error) {
doc = strings.TrimLeft(doc, " \n")
sep := "---\n"
if !strings.HasPrefix(doc, sep) {
err = fmt.Errorf("Invalid document: frontmatter required.\n%s", doc)
return
}
parts := strings.Split(doc, sep)
if len(parts) < 3 {
err = fmt.Errorf("Invalid document: frontmatter required.\n%s", doc)
return
}
frontmatter = parts[1]
body = parts[2]
return
}
func main() {
funcMap := template.FuncMap{
"sh": sh,
}
t, err := template.New("foo").Funcs(funcMap).Parse(tmpl)
frontmatter, body, err := parseDocument(tmpl)
if err != nil {
panic(err)
}
fmt.Printf("'%s'", frontmatter)
t, err := template.New("foo").Funcs(funcMap).Parse(body)
if err != nil {
panic(err)
}