repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

commit
431b315
parent
68d2fb1
author
Eric Bower
date
2024-03-03 14:27:45 +0000 UTC
fix(prose): better front-matter error handling on file upload
2 files changed,  +64, -17
M prose/scp_hooks.go
+1, -2
 1@@ -48,9 +48,8 @@ func (p *MarkdownHooks) FileValidate(s ssh.Session, data *filehandlers.PostMetaD
 2 
 3 func (p *MarkdownHooks) FileMeta(s ssh.Session, data *filehandlers.PostMetaData) error {
 4 	parsedText, err := shared.ParseText(data.Text)
 5-	// we return nil here because we don't want the file upload to fail
 6 	if err != nil {
 7-		return nil
 8+		return fmt.Errorf("%s: %w", data.Filename, err)
 9 	}
10 
11 	if parsedText.Title == "" {
M shared/mdparser.go
+63, -15
  1@@ -52,18 +52,28 @@ func HtmlPolicy() *bluemonday.Policy {
  2 
  3 var policy = HtmlPolicy()
  4 
  5-func toString(obj interface{}) string {
  6+func toString(obj interface{}) (string, error) {
  7 	if obj == nil {
  8-		return ""
  9+		return "", nil
 10+	}
 11+	switch val := obj.(type) {
 12+	case string:
 13+		return val, nil
 14+	default:
 15+		return "", fmt.Errorf("incorrect type for value: %T, should be string", val)
 16 	}
 17-	return obj.(string)
 18 }
 19 
 20-func toBool(obj interface{}) bool {
 21+func toBool(obj interface{}) (bool, error) {
 22 	if obj == nil {
 23-		return false
 24+		return false, nil
 25+	}
 26+	switch val := obj.(type) {
 27+	case bool:
 28+		return val, nil
 29+	default:
 30+		return false, fmt.Errorf("incorrect type for value: %T, should be bool", val)
 31 	}
 32-	return obj.(bool)
 33 }
 34 
 35 func toLinks(orderedMetaData yaml.MapSlice) ([]Link, error) {
 36@@ -196,17 +206,55 @@ func ParseText(text string) (*ParsedText, error) {
 37 
 38 	parsed.Html = policy.Sanitize(buf.String())
 39 	metaData := meta.Get(context)
 40-	parsed.MetaData.Title = toString(metaData["title"])
 41-	parsed.MetaData.Description = toString(metaData["description"])
 42-	parsed.MetaData.Layout = toString(metaData["layout"])
 43-	parsed.MetaData.Image = toString(metaData["image"])
 44-	parsed.MetaData.ImageCard = toString(metaData["card"])
 45-	parsed.MetaData.Hidden = toBool(metaData["draft"])
 46-	parsed.MetaData.Favicon = toString(metaData["favicon"])
 47+
 48+	title, err := toString(metaData["title"])
 49+	if err != nil {
 50+		return &parsed, fmt.Errorf("front-matter field (%s): %w", "title", err)
 51+	}
 52+	parsed.MetaData.Title = title
 53+
 54+	description, err := toString(metaData["description"])
 55+	if err != nil {
 56+		return &parsed, fmt.Errorf("front-matter field (%s): %w", "description", err)
 57+	}
 58+	parsed.MetaData.Description = description
 59+
 60+	layout, err := toString(metaData["layout"])
 61+	if err != nil {
 62+		return &parsed, fmt.Errorf("front-matter field (%s): %w", "layout", err)
 63+	}
 64+	parsed.MetaData.Layout = layout
 65+
 66+	image, err := toString(metaData["image"])
 67+	if err != nil {
 68+		return &parsed, fmt.Errorf("front-matter field (%s): %w", "image", err)
 69+	}
 70+	parsed.MetaData.Image = image
 71+
 72+	card, err := toString(metaData["card"])
 73+	if err != nil {
 74+		return &parsed, fmt.Errorf("front-matter field (%s): %w", "card", err)
 75+	}
 76+	parsed.MetaData.ImageCard = card
 77+
 78+	hidden, err := toBool(metaData["draft"])
 79+	if err != nil {
 80+		return &parsed, fmt.Errorf("front-matter field (%s): %w", "draft", err)
 81+	}
 82+	parsed.MetaData.Hidden = hidden
 83+
 84+	favicon, err := toString(metaData["favicon"])
 85+	if err != nil {
 86+		return &parsed, fmt.Errorf("front-matter field (%s): %w", "favicon", err)
 87+	}
 88+	parsed.MetaData.Favicon = favicon
 89 
 90 	var publishAt *time.Time = nil
 91-	var err error
 92-	date := toString(metaData["date"])
 93+	date, err := toString(metaData["date"])
 94+	if err != nil {
 95+		return &parsed, fmt.Errorf("front-matter field (%s): %w", "date", err)
 96+	}
 97+
 98 	if date != "" {
 99 		nextDate, err := dateparse.ParseStrict(date)
100 		if err != nil {