feat(blog): wrap output html in page template
This commit is contained in:
@@ -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() {
|
||||
<div class="w-100% py-10rem flex justify-center">
|
||||
<main class="w-60vw">
|
||||
@templ.Raw(string(html))
|
||||
</main>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user