all repos — vite @ 0a2b893a2ec830c9de1a0e25555e9068820eef65

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

rss.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
 46
 47
package main

import (
	"os"
	"path/filepath"
	"sort"
	"time"

	. "github.com/gorilla/feeds"
)

func generateRSS(posts []Post, cfg Config) {
	now := time.Now()
	feed := &Feed{
		Title:       cfg.Title,
		Link:        &Link{Href: cfg.SiteURL},
		Description: cfg.Description,
		Author: &Author{
			Name:  cfg.Author["name"],
			Email: cfg.Author["email"],
		},
		Created: now,
	}

	// Sort posts by date
	sort.Slice(posts, func(i, j int) bool {
		return posts[j].Fm.Date.Time.Before(posts[i].Fm.Date.Time)
	})

	atomfile, err := os.Create(filepath.Join("build", "blog", "feed.xml"))
	if err != nil {
		printErr(err)
	}
	for _, p := range posts {
		feed.Items = append(feed.Items, &Item{
			Title:       p.Fm.Title,
			Link:        &Link{Href: cfg.RSSPrefixURL + p.Fm.URL},
			Description: string(p.Fm.Body),
			Created:     p.Fm.Date.Time,
		})
	}

	err = feed.WriteAtom(atomfile)
	if err != nil {
		printErr(err)
	}
}