all repos — homestead @ 526002b13309812ce647faff2bdf66d6991ec467

Code for my website

render .md files under root as pages (x.md => /x)

Alan Pearce
commit

526002b13309812ce647faff2bdf66d6991ec467

parent

b0c19c83493787485428221330c654defcd18505

1 file changed, 58 insertions(+), 21 deletions(-)

changed files
M internal/content/posts.gointernal/content/posts.go
@@ -42,11 +42,11 @@ content []byte
} type Config struct { - Root string - InputDir string + Root string + PostDir string } -type PostsCollection struct { +type Collection struct { config *Config Posts []Post
@@ -65,42 +65,79 @@ &fences.Extender{},
), ) -var outputReplacer = strings.NewReplacer( +var postOutputReplacer = strings.NewReplacer( "index.md", "index.html", ".md", "/index.html", ) -var urlReplacer = strings.NewReplacer( +var postURLReplacer = strings.NewReplacer( "index.md", "", ".md", "/", ) -func (pc *PostsCollection) GetPost(filename string) (*Post, error) { - fp := filepath.Join(pc.config.Root, filename) - url := path.Join("/", urlReplacer.Replace(filename)) + "/" +func (cc *Collection) GetPost(filename string) (*Post, error) { + fp := filepath.Join(cc.config.Root, cc.config.PostDir, filename) + url := path.Join("/", cc.config.PostDir, postURLReplacer.Replace(filename)) + "/" + post := &Post{ + Input: fp, + Output: path.Join(cc.config.PostDir, postOutputReplacer.Replace(filename)), + Basename: filepath.Base(url), + URL: url, + PostMatter: &PostMatter{}, + } + + err := parse(fp, post) + if err != nil { + return nil, err + } + + return post, nil +} + +var pageOutputReplacer = strings.NewReplacer( + "index.md", "index.html", + ".md", ".html", +) +var pageURLReplacer = strings.NewReplacer( + "index.md", "", + ".md", "", +) + +func (cc *Collection) GetPage(filename string) (*Post, error) { + fp := filepath.Join(cc.config.Root, filename) + url := path.Join("/", pageURLReplacer.Replace(filename)) post := &Post{ Input: fp, - Output: path.Join(pc.config.InputDir, outputReplacer.Replace(filename)), + Output: pageOutputReplacer.Replace(filename), Basename: filepath.Base(url), URL: url, PostMatter: &PostMatter{}, } + err := parse(fp, post) + if err != nil { + return nil, err + } + + return post, nil +} + +func parse(fp string, post *Post) error { content, err := os.Open(fp) if err != nil { - return nil, errors.WithMessagef(err, "could not open post %s", fp) + return errors.WithMessagef(err, "could not open post %s", fp) } defer content.Close() post.content, err = frontmatter.Parse(content, post.PostMatter) if err != nil { - return nil, errors.WithMessagef( + return errors.WithMessagef( err, "could not parse front matter of post %s", fp, ) } - return post, nil + return nil } // implements templ.Component
@@ -117,15 +154,15 @@
return buf.String(), nil } -func NewPostsCollection(config *Config, log *log.Logger) (*PostsCollection, error) { - pc := &PostsCollection{ +func NewContentCollection(config *Config, log *log.Logger) (*Collection, error) { + cc := &Collection{ Posts: []Post{}, Tags: mapset.NewSet[string](), config: config, } - log.Debug("reading posts", "root", config.Root, "input_dir", config.InputDir) - subdir := filepath.Join(config.Root, config.InputDir) + log.Debug("reading posts", "root", config.Root, "input_dir", config.PostDir) + subdir := filepath.Join(config.Root, config.PostDir) files, err := os.ReadDir(subdir) if err != nil { return nil, errors.WithMessagef(err, "could not read post directory %s", subdir)
@@ -134,21 +171,21 @@ for _, f := range files {
fn := f.Name() if !f.IsDir() && path.Ext(fn) == ".md" { log.Debug("reading post", "post", fn) - post, err := pc.GetPost(filepath.Join(config.InputDir, fn)) + post, err := cc.GetPost(fn) if err != nil { return nil, err } for _, tag := range post.PostMatter.Taxonomies.Tags { - pc.Tags.Add(strings.ToLower(tag)) + cc.Tags.Add(strings.ToLower(tag)) } - pc.Posts = append(pc.Posts, *post) + cc.Posts = append(cc.Posts, *post) } } - slices.SortFunc(pc.Posts, func(a, b Post) int { + slices.SortFunc(cc.Posts, func(a, b Post) int { return b.Date.Compare(a.Date) }) - return pc, nil + return cc, nil }