mirror of
https://github.com/lancatlin/mdsh.git
synced 2026-11-08 11:40:12 +08:00
feat: add frontmatter extraction
This commit is contained in:
1 parent
121f5a4511
commit
70837bf6b1
1 file changed
+26
-1
@@ -9,6 +9,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var tmpl = `
|
var tmpl = `
|
||||||
|
---
|
||||||
|
title: This is the title
|
||||||
|
---
|
||||||
Hello this is the text:
|
Hello this is the text:
|
||||||
{{ sh "hledger bal assets" }}
|
{{ sh "hledger bal assets" }}
|
||||||
`
|
`
|
||||||
@@ -22,11 +25,33 @@ func sh(command string) string {
|
|||||||
return fmt.Sprintf("\n```\n%s\n```\n", string(out))
|
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() {
|
func main() {
|
||||||
funcMap := template.FuncMap{
|
funcMap := template.FuncMap{
|
||||||
"sh": sh,
|
"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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user