diff --git a/mvc/models/models_pages/post.go b/mvc/models/models_pages/post.go index e982b94..4be95d9 100644 --- a/mvc/models/models_pages/post.go +++ b/mvc/models/models_pages/post.go @@ -14,10 +14,10 @@ const ( ) type Post struct { - Link PostLink - Preview template.HTML - Data template.HTML - CreateTimestamp int64 + Link PostLink + Preview template.HTML + Data template.HTML + ModTimestamp int64 } 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 } -func newPost(link string, data []byte) *Post { +func newPost(link string, data []byte, modTimestamp int64) *Post { previewBuf := make([]byte, 0, 503) if len(data) > 500 { @@ -47,9 +47,9 @@ func newPost(link string, data []byte) *Post { } return &Post{ - Link: PostLink(link), - Preview: template.HTML(previewBuf), - Data: template.HTML(data), - CreateTimestamp: time.Now().Unix(), + Link: PostLink(link), + Preview: template.HTML(previewBuf), + Data: template.HTML(data), + ModTimestamp: modTimestamp, } } diff --git a/mvc/models/models_pages/posts.go b/mvc/models/models_pages/posts.go index 298d0f5..db13b12 100644 --- a/mvc/models/models_pages/posts.go +++ b/mvc/models/models_pages/posts.go @@ -8,6 +8,7 @@ import ( "main/tools" "os" "path/filepath" + "sort" "strings" "time" ) @@ -41,7 +42,8 @@ func LoadPosts(dir string) (Posts, error) { html := tools.MdToHTML(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 }) @@ -56,10 +58,20 @@ func LoadPosts(dir string) (Posts, error) { func (p *Posts) RenderPostsPage(templates *template.Template, version int64) ([]byte, error) { 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{ "version": version, "renderingTimestamp": time.Now().Unix(), - "posts": p, + "posts": postsSlice, } if err := templates.ExecuteTemplate(&pageData, PostsPageTmplName, context); err != nil { diff --git a/mvc/views/blocks/footer.gohtml b/mvc/views/blocks/footer.gohtml index 151d426..8a438c9 100644 --- a/mvc/views/blocks/footer.gohtml +++ b/mvc/views/blocks/footer.gohtml @@ -5,10 +5,7 @@

- and also you can subscribe to my Telegram channel with pictures! -

-

- digital countryside + and also you can subscribe to my telegram channel with pictures!

diff --git a/mvc/views/pages/posts.gohtml b/mvc/views/pages/posts.gohtml index 81664a7..7bed3fc 100644 --- a/mvc/views/pages/posts.gohtml +++ b/mvc/views/pages/posts.gohtml @@ -5,7 +5,7 @@ {{ template "header" . }}
- {{ range $key, $post := .posts }} + {{ range $ind, $post := .posts }}

{{ $post.Preview }} @@ -14,7 +14,7 @@ read more

- created at: {{ $post.CreateTimestamp }} + changed at: {{ $post.ModTimestamp }}

{{ end }} diff --git a/posts/test2.md b/posts/test2.md new file mode 100644 index 0000000..c8c491e --- /dev/null +++ b/posts/test2.md @@ -0,0 +1,3 @@ +# TEST 2 + +123 \ No newline at end of file