diff --git a/handlers/pages/blog.templ b/handlers/pages/blog.templ index 9c9251f..fda6ae5 100644 --- a/handlers/pages/blog.templ +++ b/handlers/pages/blog.templ @@ -1,6 +1,8 @@ package pages import ( + "bytes" + "io" "encoding/json" "errors" "fmt" @@ -18,15 +20,29 @@ import ( "github.com/yuin/goldmark-meta" ) +type EntryTemplate func(html []byte) templ.Component + type Blog struct { repo string owner string endpoint string - md goldmark.Markdown + md goldmark.Markdown + entryTemplate EntryTemplate } -func NewBlog(owner, repo, endpoint string) *Blog { +type BlogOptions struct { + EntryTemplate EntryTemplate +} + +func NewBlog(owner, repo, endpoint string, opts ...BlogOptions) *Blog { + /* + opt := BlogOptions{} + if len(opts) > 0 { + opt = opts[0] + } + */ + u, err := url.Parse(endpoint) if err != nil { panic(fmt.Sprintf("Blog Forgejo endpoint is not a valid URL: %v", err)) @@ -37,7 +53,14 @@ func NewBlog(owner, repo, endpoint string) *Blog { goldmark.WithParserOptions(parser.WithAutoHeadingID()), ) - return &Blog{repo: repo, owner: owner, endpoint: u.String(), md: md} + return &Blog{ + repo: repo, + owner: owner, + endpoint: u.String(), + md: md, + // entryTemplate: opt.EntryTemplate, + entryTemplate: template, + } } func (p *Blog) Routes() router.Router { @@ -93,14 +116,20 @@ func (p *Blog) blogEntry(w http.ResponseWriter, r *http.Request) { return } - var buf bytes.Buffer - err := p.md.Convert(body, &buf) + buf := bytes.NewBuffer([]byte("")) + err := p.md.Convert(body, buf) if err != nil { rerrors.InternalError(errors.New("failed to render markdown"), err).ServeHTTP(w, r) return } - _, err = buf.WriteTo(w) + html, err := io.ReadAll(buf) + if err != nil { + rerrors.InternalError(errors.New("failed to read markdown html")).ServeHTTP(w, r) + return + } + + err = p.entryTemplate(html).Render(r.Context(), w) if err != nil { rerrors.InternalError(errors.New("failed to write response"), err).ServeHTTP(w, r) return @@ -144,3 +173,13 @@ type forgejoFile struct { LastCommitSha string `json:"last_commit_sha"` Type string `json:"type"` } + +templ template(html []byte) { + @layouts.Page() { +
+
+ @templ.Raw(string(html)) +
+
+ } +}