all repos — vite @ c720289599cf73858c8150484d1b3fdafcaa5595

a fast (this time, actually) and minimal static site generator

frontmatter.go (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
package main

import (
	"bytes"
	"github.com/adrg/frontmatter"
	"time"
)

type Date8601 struct {
	time.Time
}

func (d *Date8601) UnmarshalYAML(unmarshal func(interface{}) error) error {
	var date string

	err := unmarshal(&date)
	if err != nil {
		return err
	}

	d.Time, err = time.Parse("2006-01-02", date)
	return err
}

type Matter struct {
	Template string   `yaml:"template"`
	URL      string   `yaml:"url"`
	Title    string   `yaml:"title"`
	Subtitle string   `yaml:"subtitle"`
	Date     Date8601 `yaml:"date"`
	Body     string
}

// Parses frontmatter, populates the `matter` struct and
// returns the rest
func parseFrontmatter(inputBytes []byte) ([]byte, Matter) {
	m := Matter{}
	input := bytes.NewReader(inputBytes)
	rest, err := frontmatter.Parse(input, &m)

	if err != nil {
		printErr(err)
	}
	return rest, m
}