my website's cms.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667package main
import ( "fmt" "strings" "time")
type Frontmatter struct { Title string Tags []string CreatedAt time.Time}
func setValue(f *Frontmatter, key string, value string) { switch strings.ToLower(key) { case "title": f.Title = value case "tags": tags := strings.Split(value, ",") for _, tag := range tags { f.Tags = append(f.Tags, strings.TrimSpace(tag)) } case "timestamp": timestamp, err := time.Parse("2-1-2006", value) if err != nil { fmt.Printf("Invalid timestamp in post '%s': %s\n", f.Title, err) } else { f.CreatedAt = timestamp } }}
// splits a markdown file into content and frontmatter, removing any whitespace from the former.-------func splitFrontmatter(input string) (content, frontmatter string) { // the frontmatter content is the middle part splits := strings.SplitN(input, "---\n", 3)
// no frontmatter if len(splits) < 2 { return input, "" }
frontmatter = splits[1] content = strings.TrimSpace(splits[2]) // trim leading newline
return content, frontmatter}
// returns key:value. feed it ONLY the frontmatter. it doesn't matter if it contains "---" or not.func parseFrontmatter(input string) Frontmatter { var frontmatter Frontmatter
for line := range strings.Lines(input) { matches := strings.SplitN(line, ":", 2) // split (but might have a space)
if len(matches) == 2 { key := strings.ToLower(matches[0]) value := strings.TrimSpace(matches[1])
setValue(&frontmatter, key, value) } }
return frontmatter}