sorted posts

posts
serr 2025-04-12 17:32:35 +03:00
parent 7e620f7308
commit 3c301dbefc
5 changed files with 29 additions and 17 deletions

View File

@ -14,10 +14,10 @@ const (
) )
type Post struct { type Post struct {
Link PostLink Link PostLink
Preview template.HTML Preview template.HTML
Data template.HTML Data template.HTML
CreateTimestamp int64 ModTimestamp int64
} }
func RenderPostPage(templates *template.Template, version int64, data template.HTML) ([]byte, error) { func RenderPostPage(templates *template.Template, version int64, data template.HTML) ([]byte, error) {
@ -36,7 +36,7 @@ func RenderPostPage(templates *template.Template, version int64, data template.H
return pageData.Bytes(), nil return pageData.Bytes(), nil
} }
func newPost(link string, data []byte) *Post { func newPost(link string, data []byte, modTimestamp int64) *Post {
previewBuf := make([]byte, 0, 503) previewBuf := make([]byte, 0, 503)
if len(data) > 500 { if len(data) > 500 {
@ -47,9 +47,9 @@ func newPost(link string, data []byte) *Post {
} }
return &Post{ return &Post{
Link: PostLink(link), Link: PostLink(link),
Preview: template.HTML(previewBuf), Preview: template.HTML(previewBuf),
Data: template.HTML(data), Data: template.HTML(data),
CreateTimestamp: time.Now().Unix(), ModTimestamp: modTimestamp,
} }
} }

View File

@ -8,6 +8,7 @@ import (
"main/tools" "main/tools"
"os" "os"
"path/filepath" "path/filepath"
"sort"
"strings" "strings"
"time" "time"
) )
@ -41,7 +42,8 @@ func LoadPosts(dir string) (Posts, error) {
html := tools.MdToHTML(md) html := tools.MdToHTML(md)
link := fmt.Sprintf("/%s/", strings.TrimSuffix(filepath.Base(path), ".md")) link := fmt.Sprintf("/%s/", strings.TrimSuffix(filepath.Base(path), ".md"))
posts[PostLink(link)] = newPost(link, html) modTimestamp := f.ModTime().Unix()
posts[PostLink(link)] = newPost(link, html, modTimestamp)
} }
return nil return nil
}) })
@ -56,10 +58,20 @@ func LoadPosts(dir string) (Posts, error) {
func (p *Posts) RenderPostsPage(templates *template.Template, version int64) ([]byte, error) { func (p *Posts) RenderPostsPage(templates *template.Template, version int64) ([]byte, error) {
var pageData bytes.Buffer var pageData bytes.Buffer
postsSlice := make([]*Post, 0, len(*p))
for _, post := range *p {
postsSlice = append(postsSlice, post)
}
// Сортирую по ModTimestamp (новые сначала)
sort.Slice(postsSlice, func(i, j int) bool {
return postsSlice[i].ModTimestamp > postsSlice[j].ModTimestamp
})
context := map[string]any{ context := map[string]any{
"version": version, "version": version,
"renderingTimestamp": time.Now().Unix(), "renderingTimestamp": time.Now().Unix(),
"posts": p, "posts": postsSlice,
} }
if err := templates.ExecuteTemplate(&pageData, PostsPageTmplName, context); err != nil { if err := templates.ExecuteTemplate(&pageData, PostsPageTmplName, context); err != nil {

View File

@ -5,10 +5,7 @@
</div> </div>
<div> <div>
<p> <p>
and also you can subscribe to my Telegram channel with pictures! and also you can subscribe to my <a href="https://t.me/lolistack" target="_blank">telegram channel </a> with pictures!
</p>
<p>
<a href="https://t.me/lolistack" target="_blank">digital countryside</a>
</p> </p>
</div> </div>
<div> <div>

View File

@ -5,7 +5,7 @@
{{ template "header" . }} {{ template "header" . }}
<main> <main>
{{ range $key, $post := .posts }} {{ range $ind, $post := .posts }}
<div class="post"> <div class="post">
<p> <p>
{{ $post.Preview }} {{ $post.Preview }}
@ -14,7 +14,7 @@
<a href="{{ $post.Link }}">read more</a> <a href="{{ $post.Link }}">read more</a>
</p> </p>
<p> <p>
<code>created at: {{ $post.CreateTimestamp }}</code> <code>changed at: {{ $post.ModTimestamp }}</code>
</p> </p>
</div> </div>
{{ end }} {{ end }}

3
posts/test2.md Normal file
View File

@ -0,0 +1,3 @@
# TEST 2
123