Support index page generation
Anirudh Oppiliappan x@icyphox.sh
Sat, 05 Dec 2020 11:22:32 +0530
M
build.go
→
build.go
@@ -2,22 +2,33 @@ package main
import ( "fmt" - "github.com/cross-cpm/go-shutil" "io/ioutil" "os" "os/exec" "path/filepath" + "sort" "strings" "text/template" + + "github.com/cross-cpm/go-shutil" ) var cfg = parseConfig() type Post struct { - fm Matter + Fm Matter } var posts []Post + +type NewFm struct { + Template string + URL string + Title string + Subtitle string + Date string + Body string +} func execute(cmds []string) { for _, cmd := range cmds {@@ -69,16 +80,7 @@ if strings.Contains(relPath, "blog/") {
posts = append(posts, Post{fm}) } - type NewFm struct { - Template string - URL string - Title string - Subtitle string - Date string - Body string - } - - var newFm = NewFm { + var newFm = NewFm{ fm.Template, fm.URL, fm.Title,@@ -109,6 +111,48 @@ }
htmlFile.Close() } +func renderIndex(posts []Post) { + indexTmpl := processTemplate("index.html") + path := filepath.Join("pages", "_index.md") + + // Sort posts by date + sort.Slice(posts, func(i, j int) bool { + return posts[j].Fm.Date.Time.Before(posts[i].Fm.Date.Time) + }) + + content, err := ioutil.ReadFile(path) + if err != nil { + printErr(err) + } + + restContent, fm := parseFrontmatter(content) + bodyHtml := mdRender(restContent) + fm.Body = string(bodyHtml) + + var newFm = NewFm{ + fm.Template, + fm.URL, + fm.Title, + fm.Subtitle, + fm.Date.Time.Format(cfg.DateFmt), + fm.Body, + } + + combined := struct { + Fm NewFm + Posts []Post + Cfg Config + }{newFm, posts, cfg} + + htmlFile, err := os.Create(filepath.Join("build", "index.html")) + err = indexTmpl.Execute(htmlFile, combined) + if err != nil { + printErr(err) + return + } + htmlFile.Close() +} + func viteBuild() { if len(cfg.Prebuild) != 0 { printMsg("executing pre-build actions...")@@ -119,7 +163,7 @@ if err != nil {
printErr(err) return err } - if filepath.Ext(path) == ".md" { + if filepath.Ext(path) == ".md" && path != filepath.Join("pages", "_index.md") { handleMd(path) } else { f, err := os.Stat(path)@@ -140,9 +184,13 @@ }
} return nil }) + if err != nil { printErr(err) } + + // Deal with the special snowflake '_index.md' + renderIndex(posts) _, err = shutil.CopyTree("static", filepath.Join("build", "static"), nil) if err != nil {
M
config.go
→
config.go
@@ -6,16 +6,16 @@ "io/ioutil"
) type Config struct { - Title string `yaml:"title"` - Header string `yaml:"header"` - DateFmt string `yaml:datefmt` - SiteURL string `yaml:"siteurl"` - Description string `yaml:"description"` - Author map[string]string `yaml:"author"` - Footer string `yaml:"footer"` - Prebuild []string `yaml:"prebuild"` - Postbuild []string `yaml:"postbuild"` - RSSPrefixURL string `yaml:"rssprefixurl"` + Title string `yaml:"title"` + Header string `yaml:"header"` + DateFmt string `yaml:datefmt` + SiteURL string `yaml:"siteurl"` + Description string `yaml:"description"` + Author map[string]string `yaml:"author"` + Footer string `yaml:"footer"` + Prebuild []string `yaml:"prebuild"` + Postbuild []string `yaml:"postbuild"` + RSSPrefixURL string `yaml:"rssprefixurl"` } func parseConfig() Config {
M
rss.go
→
rss.go
@@ -1,11 +1,12 @@
package main import ( - . "github.com/gorilla/feeds" + "os" + "path/filepath" "sort" "time" - "os" - "path/filepath" + + . "github.com/gorilla/feeds" ) func generateRSS(posts []Post, cfg Config) {@@ -23,7 +24,7 @@ }
// Sort posts by date sort.Slice(posts, func(i, j int) bool { - return posts[j].fm.Date.Time.Before(posts[i].fm.Date.Time) + return posts[j].Fm.Date.Time.Before(posts[i].Fm.Date.Time) }) atomfile, err := os.Create(filepath.Join("build", "blog", "feed.xml"))@@ -32,10 +33,10 @@ 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, + Title: p.Fm.Title, + Link: &Link{Href: cfg.RSSPrefixURL + p.Fm.URL}, + Description: string(p.Fm.Body), + Created: p.Fm.Date.Time, }) }