Parse dates from page filenames

This commit is contained in:
adnano 2020-09-22 17:04:29 -04:00
parent 36aaa50752
commit 40ae714d6d

19
main.go
View file

@ -2,6 +2,7 @@ package main
import ( import (
"log" "log"
"path/filepath"
"time" "time"
) )
@ -46,13 +47,23 @@ type Page struct {
} }
func NewPage(path string, content string) *Page { func NewPage(path string, content string) *Page {
// TODO: Parse date from path name page := &Page{
permalink := "/" + path
return &Page{
Path: path, Path: path,
Permalink: permalink, Permalink: "/" + path,
Content: content, Content: content,
} }
// Try to parse the date from the page filename
const layout = "2006-01-02"
base := filepath.Base(path)
if len(base) >= len(layout) {
dateStr := base[:len(layout)]
if date, err := time.Parse(layout, dateStr); err == nil {
page.Date = date
}
}
return page
} }
// Section represents a section (i.e., a directory). // Section represents a section (i.e., a directory).