From b27c96688785372787eb5c3c71a32767fab71ac4 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Tue, 18 Jun 2024 20:13:26 +0200 Subject: split content and sitemap code from builder --- internal/content/posts.go | 129 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 internal/content/posts.go (limited to 'internal/content') diff --git a/internal/content/posts.go b/internal/content/posts.go new file mode 100644 index 0000000..c3511cf --- /dev/null +++ b/internal/content/posts.go @@ -0,0 +1,129 @@ +package content + +import ( + "bytes" + "os" + "path" + "path/filepath" + "slices" + "strings" + "time" + "website/internal/log" + + "github.com/adrg/frontmatter" + mapset "github.com/deckarep/golang-set/v2" + "github.com/pkg/errors" + fences "github.com/stefanfritsch/goldmark-fences" + "github.com/yuin/goldmark" + "github.com/yuin/goldmark/extension" + htmlrenderer "github.com/yuin/goldmark/renderer/html" +) + +type PostMatter struct { + Date time.Time `toml:"date"` + Description string `toml:"description"` + Title string `toml:"title"` + Taxonomies struct { + Tags []string `toml:"tags"` + } `toml:"taxonomies"` +} + +type Post struct { + Input string + Output string + Basename string + URL string + Content string + PostMatter +} + +type Tags mapset.Set[string] + +var markdown = goldmark.New( + goldmark.WithRendererOptions( + htmlrenderer.WithUnsafe(), + ), + goldmark.WithExtensions( + extension.GFM, + extension.Footnote, + extension.Typographer, + &fences.Extender{}, + ), +) + +func GetPost(filename string) (*PostMatter, []byte, error) { + matter := PostMatter{} + content, err := os.Open(filename) + if err != nil { + return nil, nil, errors.WithMessagef(err, "could not open post %s", filename) + } + defer content.Close() + rest, err := frontmatter.MustParse(content, &matter) + if err != nil { + return nil, nil, errors.WithMessagef( + err, + "could not parse front matter of post %s", + filename, + ) + } + + return &matter, rest, nil +} + +func renderMarkdown(content []byte) (string, error) { + var buf bytes.Buffer + if err := markdown.Convert(content, &buf); err != nil { + return "", errors.WithMessage(err, "could not convert markdown content") + } + + return buf.String(), nil +} + +func ReadPosts(root string, inputDir string, outputDir string) ([]Post, Tags, error) { + tags := mapset.NewSet[string]() + posts := []Post{} + subdir := filepath.Join(root, inputDir) + files, err := os.ReadDir(subdir) + if err != nil { + return nil, nil, errors.WithMessagef(err, "could not read post directory %s", subdir) + } + outputReplacer := strings.NewReplacer(root, outputDir, ".md", "/index.html") + urlReplacer := strings.NewReplacer(root, "", ".md", "/") + for _, f := range files { + pathFromRoot := filepath.Join(subdir, f.Name()) + if !f.IsDir() && path.Ext(pathFromRoot) == ".md" { + output := outputReplacer.Replace(pathFromRoot) + url := urlReplacer.Replace(pathFromRoot) + log.Debug("reading post", "post", pathFromRoot) + matter, content, err := GetPost(pathFromRoot) + if err != nil { + return nil, nil, err + } + + for _, tag := range matter.Taxonomies.Tags { + tags.Add(strings.ToLower(tag)) + } + + log.Debug("rendering markdown in post", "post", pathFromRoot) + html, err := renderMarkdown(content) + if err != nil { + return nil, nil, err + } + post := Post{ + Input: pathFromRoot, + Output: output, + Basename: filepath.Base(url), + URL: url, + PostMatter: *matter, + Content: html, + } + + posts = append(posts, post) + } + } + slices.SortFunc(posts, func(a, b Post) int { + return b.Date.Compare(a.Date) + }) + + return posts, tags, nil +} -- cgit 1.4.1