From 1f9f24e2cef08f40fe2597b3644d08b28e31d370 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Tue, 18 Jun 2024 20:19:05 +0200 Subject: pass rendered html to homepage --- internal/builder/builder.go | 10 +++++++++- internal/builder/homepage.templ | 17 ++--------------- internal/content/posts.go | 4 ++-- 3 files changed, 13 insertions(+), 18 deletions(-) (limited to 'internal') diff --git a/internal/builder/builder.go b/internal/builder/builder.go index 141bd6f..b4ade4a 100644 --- a/internal/builder/builder.go +++ b/internal/builder/builder.go @@ -206,7 +206,15 @@ func build(outDir string, config config.Config) (*Result, error) { r.Hashes = append(r.Hashes, h) log.Debug("rendering homepage") - if err := renderToFile(homepage(config, posts), publicDir, "index.html"); err != nil { + _, text, err := content.GetPost(path.Join("content", "_index.md")) + if err != nil { + return nil, err + } + content, err := content.RenderMarkdown(text) + if err != nil { + return nil, err + } + if err := renderToFile(homepage(config, posts, content), publicDir, "index.html"); err != nil { return nil, err } // it would be nice to set LastMod here, but using the latest post diff --git a/internal/builder/homepage.templ b/internal/builder/homepage.templ index 1df401b..df5bc32 100644 --- a/internal/builder/homepage.templ +++ b/internal/builder/homepage.templ @@ -2,23 +2,10 @@ package builder import ( "website/internal/config" - "path" "website/internal/content" ) -func getContent(filename string) templ.Component { - return templ.ComponentFunc(func(ctx context.Context, w io.Writer) error { - _, index, err := content.GetPost(path.Join("content", filename)) - if err != nil { - return err - } - _, err = io.WriteString(w, string(index)) - - return err - }) -} - -templ homepage(config config.Config, posts []content.Post) { +templ homepage(config config.Config, posts []content.Post, content string) { @page(config, PageSettings{ Title: config.Title, TitleAttrs: templ.Attributes{ @@ -30,7 +17,7 @@ templ homepage(config config.Config, posts []content.Post) { }, }) {
- @getContent("_index.md") + @Unsafe(content)

Latest Posts

diff --git a/internal/content/posts.go b/internal/content/posts.go index c3511cf..5185d06 100644 --- a/internal/content/posts.go +++ b/internal/content/posts.go @@ -70,7 +70,7 @@ func GetPost(filename string) (*PostMatter, []byte, error) { return &matter, rest, nil } -func renderMarkdown(content []byte) (string, error) { +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") @@ -105,7 +105,7 @@ func ReadPosts(root string, inputDir string, outputDir string) ([]Post, Tags, er } log.Debug("rendering markdown in post", "post", pathFromRoot) - html, err := renderMarkdown(content) + html, err := RenderMarkdown(content) if err != nil { return nil, nil, err } -- cgit 1.4.1