mirror of
https://github.com/lancatlin/mdsh.git
synced 2026-11-08 03:30:12 +08:00
chore: make frontmatter optional
This commit is contained in:
1 parent
f976860f33
commit
88d6d98282
2 files changed
+12
-27
No files matched your search
@@ -7,26 +7,28 @@ import (
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func parseFrontmatter(input string) (meta TemplateMeta, err error) {
|
||||
err = yaml.Unmarshal([]byte(input), &meta)
|
||||
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
|
||||
return meta, body, nil
|
||||
}
|
||||
|
||||
func parseDocument(doc string) (frontmatter string, body string, err error) {
|
||||
func separateDocument(doc string) (frontmatter string, body string) {
|
||||
doc = strings.TrimLeft(doc, " \n")
|
||||
sep := "---\n"
|
||||
if !strings.HasPrefix(doc, sep) {
|
||||
err = fmt.Errorf("Invalid document: frontmatter required.\n%s", doc)
|
||||
return
|
||||
return "", doc
|
||||
}
|
||||
parts := strings.SplitN(doc, sep, 3)
|
||||
if len(parts) < 3 {
|
||||
err = fmt.Errorf("Invalid document: frontmatter required.\n%s", doc)
|
||||
return
|
||||
return "", doc
|
||||
}
|
||||
frontmatter = parts[1]
|
||||
body = strings.TrimLeft(parts[2], "\n ")
|
||||
|
||||
Reference in new issue
Block a user