@@ -92,7 +92,7 @@ func (b *Blog) getTags() []Tag {
9292 return tags
9393}
9494
95- func (b * Blog ) getArchivesByYear () map [string ][]Post {
95+ func (b * Blog ) getArchivesByYear () ([] string , map [string ][]Post ) {
9696 archive := make (map [string ][]Post )
9797 posts := b .GetPosts (false )
9898 for _ , post := range posts {
@@ -102,22 +102,32 @@ func (b *Blog) getArchivesByYear() map[string][]Post {
102102 }
103103 archive [year ] = append (archive [year ], post )
104104 }
105- return archive
105+ keys := make ([]string , 0 , len (archive ))
106+ for k := range archive {
107+ keys = append (keys , k )
108+ }
109+ sort .Sort (sort .Reverse (sort .StringSlice (keys )))
110+ return keys , archive
106111}
107112
108- func (b * Blog ) getArchivesByYearMonth () map [string ][]Post {
113+ func (b * Blog ) getArchivesByYearMonth () ([] string , map [string ][]Post ) {
109114 archive := make (map [string ][]Post )
110115 posts := b .GetPosts (false )
111116 for _ , post := range posts {
112117 year := strconv .Itoa (post .CreatedAt .Year ())
113- month := strconv . Itoa ( int (post .CreatedAt .Month ()))
118+ month := fmt . Sprintf ( "%02d" , int (post .CreatedAt .Month ()))
114119 yearMonth := year + "/" + month
115120 if _ , ok := archive [yearMonth ]; ! ok {
116121 archive [yearMonth ] = make ([]Post , 0 )
117122 }
118123 archive [yearMonth ] = append (archive [yearMonth ], post )
119124 }
120- return archive
125+ keys := make ([]string , 0 , len (archive ))
126+ for k := range archive {
127+ keys = append (keys , k )
128+ }
129+ sort .Sort (sort .Reverse (sort .StringSlice (keys )))
130+ return keys , archive
121131}
122132
123133func (b * Blog ) GetPostObject (c * gin.Context ) (* Post , error ) {
@@ -477,11 +487,15 @@ func (b *Blog) DynamicPage(c *gin.Context, page *Page) {
477487 "nav_pages" : navPages ,
478488 })
479489 case PageTypeArchives :
490+ yearKeys , byYear := b .getArchivesByYear ()
491+ monthKeys , byYearMonth := b .getArchivesByYearMonth ()
480492 c .HTML (http .StatusOK , "page_archives.html" , gin.H {
481- "logged_in" : b .auth .IsLoggedIn (c ),
482- "is_admin" : b .auth .IsAdmin (c ),
483- "byYear" : b .getArchivesByYear (),
484- "byYearMonth" : b .getArchivesByYearMonth (),
493+ "logged_in" : b .auth .IsLoggedIn (c ),
494+ "is_admin" : b .auth .IsAdmin (c ),
495+ "yearKeys" : yearKeys ,
496+ "byYear" : byYear ,
497+ "yearMonthKeys" : monthKeys ,
498+ "byYearMonth" : byYearMonth ,
485499 "page" : page ,
486500 "version" : b .Version ,
487501 "title" : page .Title ,
@@ -902,17 +916,21 @@ func (b *Blog) About(c *gin.Context) {
902916
903917// Archives shows the posts by year, month, etc.
904918func (b * Blog ) Archives (c * gin.Context ) {
919+ yearKeys , byYear := b .getArchivesByYear ()
920+ monthKeys , byYearMonth := b .getArchivesByYearMonth ()
905921 c .HTML (http .StatusOK , "archives.html" , gin.H {
906- "logged_in" : b .auth .IsLoggedIn (c ),
907- "is_admin" : b .auth .IsAdmin (c ),
908- "version" : b .Version ,
909- "title" : "Blog Archives" ,
910- "byYear" : b .getArchivesByYear (),
911- "byYearMonth" : b .getArchivesByYearMonth (),
912- "recent" : b .GetLatest (),
913- "admin_page" : false ,
914- "settings" : b .GetSettings (),
915- "nav_pages" : b .GetNavPages (),
922+ "logged_in" : b .auth .IsLoggedIn (c ),
923+ "is_admin" : b .auth .IsAdmin (c ),
924+ "version" : b .Version ,
925+ "title" : "Blog Archives" ,
926+ "yearKeys" : yearKeys ,
927+ "byYear" : byYear ,
928+ "yearMonthKeys" : monthKeys ,
929+ "byYearMonth" : byYearMonth ,
930+ "recent" : b .GetLatest (),
931+ "admin_page" : false ,
932+ "settings" : b .GetSettings (),
933+ "nav_pages" : b .GetNavPages (),
916934 })
917935}
918936
0 commit comments