Modified models to support a DB
This commit is contained in:
parent
3591e0c723
commit
ebaec3d296
33 changed files with 675 additions and 64 deletions
|
|
@ -16,16 +16,17 @@ import (
|
|||
"github.com/yuin/goldmark/parser"
|
||||
"github.com/yuin/goldmark/renderer/html"
|
||||
"lmika.dev/lmika/weiro/models"
|
||||
"lmika.dev/lmika/weiro/models/pubmodel"
|
||||
)
|
||||
|
||||
type Builder struct {
|
||||
site models.Site
|
||||
site pubmodel.Site
|
||||
gmMarkdown goldmark.Markdown
|
||||
opts Options
|
||||
tmpls *template.Template
|
||||
}
|
||||
|
||||
func New(site models.Site, opts Options) (*Builder, error) {
|
||||
func New(site pubmodel.Site, opts Options) (*Builder, error) {
|
||||
tmpls, err := template.New("").
|
||||
Funcs(templateFns(site, opts)).
|
||||
ParseFS(opts.TemplatesFS, tmplNamePostSingle, tmplNamePostList, tmplNameLayoutMain)
|
||||
|
|
@ -75,11 +76,11 @@ func (b *Builder) renderPostList(ctx buildContext, postList []*models.Post) erro
|
|||
copy(postCopy, postList)
|
||||
|
||||
sort.Slice(postCopy, func(i, j int) bool {
|
||||
return postCopy[i].Meta.Date.After(postCopy[j].Meta.Date)
|
||||
return postCopy[i].PublishedAt.After(postCopy[j].PublishedAt)
|
||||
})
|
||||
|
||||
pl := postListData{
|
||||
commonData: commonData{Site: b.site.Meta},
|
||||
commonData: commonData{Site: b.site},
|
||||
}
|
||||
for _, post := range postCopy {
|
||||
rp, err := b.renderPost(post)
|
||||
|
|
@ -95,20 +96,20 @@ func (b *Builder) renderPostList(ctx buildContext, postList []*models.Post) erro
|
|||
}
|
||||
|
||||
func (b *Builder) renderPost(post *models.Post) (postSingleData, error) {
|
||||
postPath := post.Meta.Slug
|
||||
postPath := post.Slug
|
||||
if b.opts.BasePosts != "" {
|
||||
postPath = filepath.Join(b.opts.BasePosts, strings.TrimPrefix(postPath, "/"))
|
||||
}
|
||||
|
||||
var md bytes.Buffer
|
||||
if err := b.gmMarkdown.Convert([]byte(post.Content), &md); err != nil {
|
||||
return postSingleData{}, fmt.Errorf("failed to write post %s: %w", post.Meta.Slug, err)
|
||||
if err := b.gmMarkdown.Convert([]byte(post.Body), &md); err != nil {
|
||||
return postSingleData{}, fmt.Errorf("failed to write post %s: %w", post.Slug, err)
|
||||
}
|
||||
|
||||
return postSingleData{
|
||||
commonData: commonData{Site: b.site.Meta},
|
||||
commonData: commonData{Site: b.site},
|
||||
Path: postPath,
|
||||
Meta: post.Meta,
|
||||
Meta: post,
|
||||
HTML: template.HTML(md.String()),
|
||||
}, nil
|
||||
}
|
||||
|
|
@ -152,7 +153,7 @@ func (b *Builder) renderTemplate(w io.Writer, name string, data interface{}) err
|
|||
}
|
||||
|
||||
return b.tmpls.ExecuteTemplate(w, tmplNameLayoutMain, layoutData{
|
||||
commonData: commonData{Site: b.site.Meta},
|
||||
commonData: commonData{Site: b.site},
|
||||
Body: template.HTML(buf.String()),
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import (
|
|||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"lmika.dev/lmika/weiro/models"
|
||||
"lmika.dev/lmika/weiro/models/pubmodel"
|
||||
"lmika.dev/lmika/weiro/providers/sitebuilder"
|
||||
)
|
||||
|
||||
|
|
@ -19,24 +20,18 @@ func TestBuilder_BuildSite(t *testing.T) {
|
|||
"layout_main.html": {Data: []byte(`{{ .Body }}`)},
|
||||
}
|
||||
|
||||
site := models.Site{
|
||||
Meta: models.SiteMeta{
|
||||
BaseURL: "https://example.com",
|
||||
},
|
||||
site := pubmodel.Site{
|
||||
BaseURL: "https://example.com",
|
||||
Posts: []*models.Post{
|
||||
{
|
||||
Meta: models.PostMeta{
|
||||
Title: "Test Post",
|
||||
Slug: "/2026/02/18/test-post",
|
||||
},
|
||||
Content: "This is a test post",
|
||||
Title: "Test Post",
|
||||
Slug: "/2026/02/18/test-post",
|
||||
Body: "This is a test post",
|
||||
},
|
||||
{
|
||||
Meta: models.PostMeta{
|
||||
Title: "Another Post",
|
||||
Slug: "/2026/02/20/another-post",
|
||||
},
|
||||
Content: "This is **another** test post",
|
||||
Title: "Another Post",
|
||||
Slug: "/2026/02/20/another-post",
|
||||
Body: "This is **another** test post",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
@ -64,4 +59,4 @@ func TestBuilder_BuildSite(t *testing.T) {
|
|||
}
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -6,17 +6,17 @@ import (
|
|||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"lmika.dev/lmika/weiro/models"
|
||||
"lmika.dev/lmika/weiro/models/pubmodel"
|
||||
)
|
||||
|
||||
func templateFns(site models.Site, opts Options) template.FuncMap {
|
||||
func templateFns(site pubmodel.Site, opts Options) template.FuncMap {
|
||||
return template.FuncMap{
|
||||
"url_abs": func(basePath string) (string, error) {
|
||||
if site.Meta.BaseURL == "" {
|
||||
if site.BaseURL == "" {
|
||||
return basePath, nil
|
||||
}
|
||||
|
||||
pu, err := url.Parse(site.Meta.BaseURL)
|
||||
pu, err := url.Parse(site.BaseURL)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"time"
|
||||
|
||||
"lmika.dev/lmika/weiro/models"
|
||||
"lmika.dev/lmika/weiro/models/pubmodel"
|
||||
)
|
||||
|
||||
const (
|
||||
|
|
@ -22,8 +23,6 @@ const (
|
|||
)
|
||||
|
||||
type Options struct {
|
||||
SiteMeta models.SiteMeta
|
||||
|
||||
// BasePosts is the base path for posts.
|
||||
BasePosts string
|
||||
|
||||
|
|
@ -34,12 +33,12 @@ type Options struct {
|
|||
}
|
||||
|
||||
type commonData struct {
|
||||
Site models.SiteMeta
|
||||
Site pubmodel.Site
|
||||
}
|
||||
|
||||
type postSingleData struct {
|
||||
commonData
|
||||
Meta models.PostMeta
|
||||
Meta *models.Post
|
||||
HTML template.HTML
|
||||
Path string
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue