all repos — vite @ c720289599cf73858c8150484d1b3fdafcaa5595

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
package main

import (
	. "github.com/gorilla/feeds"
	"sort"
	"time"
	"os"
	"path/filepath"
)

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)
	}
}