fix(template): fail on invalid template path
This commit is contained in:
parent
5e17d38349
commit
9775c27bbd
2 changed files with 13 additions and 14 deletions
|
|
@ -30,7 +30,9 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an
|
|||
return nil, err
|
||||
}
|
||||
|
||||
data, err := template.RenderTemplate(cmd.TemplatePath, cmd.Title)
|
||||
slug := frontmatter.Slugify(cmd.Title)
|
||||
|
||||
data, err := template.RenderTemplate(cmd.TemplatePath, cmd.Title, slug)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,3 @@
|
|||
package templatepackage template
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
package template
|
||||
|
||||
import (
|
||||
|
|
@ -15,7 +7,7 @@ import (
|
|||
)
|
||||
|
||||
func RenderTemplate(templatePath string, title string, slug string) ([]byte, error) {
|
||||
tmpl, err := readTemplate(templatePath, title)
|
||||
template, err := readTemplate(templatePath, title)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -26,17 +18,22 @@ func RenderTemplate(templatePath string, title string, slug string) ([]byte, err
|
|||
"{{slug}}", slug,
|
||||
)
|
||||
|
||||
return []byte(r.Replace(string(tmpl))), nil
|
||||
return []byte(r.Replace(string(template))), nil
|
||||
}
|
||||
|
||||
func readTemplate(templatePath string, title string) ([]byte, error) {
|
||||
if _, err := os.Stat(templatePath); os.IsNotExist(err) {
|
||||
if templatePath == "" {
|
||||
return createDefaultTemplate(title), nil
|
||||
}
|
||||
return os.ReadFile(templatePath)
|
||||
|
||||
template, err := os.ReadFile(templatePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return template, nil
|
||||
}
|
||||
|
||||
func createDefaultTemplate(title string) []byte {
|
||||
return []byte("---\ntitle: " + title + "\ndate: " + time.Now().Format("2006-01-02") + "\n---\n")
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue