Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(blog): add sorting, better rendering #1541

Merged
merged 33 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
51a7593
add sorting by title & pub date
leohhhn Jan 16, 2024
66e6cdc
Merge branch 'master' into feat/blog-sort
leohhhn Jan 16, 2024
57fe188
add publisher, authors, modify publish date. add nicer rendering
leohhhn Jan 20, 2024
013a6a2
remove usage of seqid
leohhhn Jan 20, 2024
2fcd656
reverse sort order
leohhhn Jan 20, 2024
fd4e93d
Merge branch 'master' into feat/blog-sort
leohhhn Jan 22, 2024
4fa2e98
update ModEditPost
leohhhn Jan 22, 2024
524df94
modify post footer, post list item
leohhhn Jan 23, 2024
1f6afa0
wip fixing bugs
leohhhn Jan 23, 2024
a52b6e2
add rendering back
leohhhn Jan 26, 2024
7b44d93
Merge branch 'master' into feat/blog-sort
leohhhn Jan 26, 2024
dd38742
remove leftovers
leohhhn Jan 26, 2024
713f307
update gnomod
leohhhn Jan 26, 2024
f1ef78f
update tests
leohhhn Jan 26, 2024
84b9a53
render tags better
leohhhn Feb 5, 2024
7ed1b5d
return gen txs
leohhhn Feb 5, 2024
6b272f8
remove newlines
leohhhn Feb 6, 2024
a8694e7
fixup genesis blog tx, start reworking tests
leohhhn Feb 8, 2024
132d2ef
wip
leohhhn Feb 9, 2024
4f916c8
extract post errors
leohhhn Feb 9, 2024
e158248
update dates on pages
leohhhn Feb 9, 2024
d06608b
update dates on pages v2
leohhhn Feb 12, 2024
a15bd8b
temp add test1 as admin to blog realm
leohhhn Feb 13, 2024
28a49d0
fix tests
leohhhn Feb 13, 2024
7aeaa66
fix lint
leohhhn Feb 13, 2024
7a6c2e6
Merge branch 'master' into feat/blog-sort
leohhhn Feb 13, 2024
4283930
fixup CI
leohhhn Feb 14, 2024
b1582a0
update rendering, fix tests
leohhhn Feb 22, 2024
50182ee
Merge branch 'master' into feat/blog-sort
leohhhn Feb 22, 2024
db57f7d
Merge branch 'master' into feat/blog-sort
moul Feb 23, 2024
6c32b09
update comments rendering
leohhhn Feb 24, 2024
d3bc364
fix tests
leohhhn Feb 24, 2024
60a5d61
fix tests 2
leohhhn Feb 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions examples/gno.land/p/demo/blog/blog.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@ package blog

import (
"errors"
"gno.land/p/demo/avl"
"gno.land/p/demo/mux"
"gno.land/p/demo/seqid"
"gno.land/p/demo/ufmt"

"std"
"strconv"
"strings"
"time"

"gno.land/p/demo/avl"
"gno.land/p/demo/mux"
"gno.land/p/demo/ufmt"
)

type Blog struct {
Title string
Prefix string // i.e. r/gnoland/blog:
Posts avl.Tree // slug -> Post
NoBreadcrumb bool
Title string
Prefix string // i.e. r/gnoland/blog:
Posts avl.Tree // slug -> *Post
PostsPublished avl.Tree // published-date -> *Post
PostsAlphabetical avl.Tree // title -> *Post
NoBreadcrumb bool
PostIndex seqid.ID
}

func (b Blog) RenderLastPostsWidget(limit int) string {
Expand All @@ -42,7 +46,7 @@ func (b Blog) RenderHome(res *mux.ResponseWriter, req *mux.Request) {
}

res.Write("<div class='columns-3'>")
b.Posts.Iterate("", "", func(key string, value interface{}) bool {
b.PostsPublished.Iterate("", "", func(key string, value interface{}) bool {
post := value.(*Post)
res.Write(post.RenderListItem())
return false
Expand Down Expand Up @@ -157,9 +161,15 @@ func (b *Blog) prepareAndSetPost(post *Post) error {
// more input sanitization?

post.Blog = b
post.UpdatedAt = time.Now()
post.UpdatedAt = post.CreatedAt

trimmedTitleKey := strings.Replace(post.Title, " ", "", -1)
pubDateKey := makePubDate(post.CreatedAt, int64(b.PostIndex.Next()))
b.PostsAlphabetical.Set(trimmedTitleKey, post)
b.PostsPublished.Set(pubDateKey, post)

b.Posts.Set(post.Slug, post)

return nil
}

Expand Down Expand Up @@ -236,6 +246,7 @@ func (p *Post) RenderListItem() string {
output := "<div>\n\n"
output += ufmt.Sprintf("## [%s](%s)\n", p.Title, p.URL())
output += ufmt.Sprintf("**[Learn More](%s)**\n", p.URL())

// output += p.Summary() + "\n\n"
// output += p.RenderTagList() + "\n\n"
// output += formatAuthorAndDate(p.Author, p.CreatedAt) + "\n"
Expand Down
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/blog/gno.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ module gno.land/p/demo/blog
require (
gno.land/p/demo/avl v0.0.0-latest
gno.land/p/demo/mux v0.0.0-latest
gno.land/p/demo/seqid v0.0.0-latest
gno.land/p/demo/ufmt v0.0.0-latest
)
14 changes: 13 additions & 1 deletion examples/gno.land/p/demo/blog/util.gno
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
package blog

import "strings"
import (
"strconv"
"strings"
"time"
)

func breadcrumb(parts []string) string {
return "# " + strings.Join(parts, " / ") + "\n\n"
}

func makePubDate(t time.Time, id int64) string {
// We use the timestamp to sort by publish order.
// In case of multiple msgs to publish in the same transaction
// there will be a timestamp conflict (time.Now() is per transaction, not per msg).
// This functions assures that timestamp IDs are unique in this case.
return strconv.Itoa(int(t.Unix())) + "-" + strconv.Itoa(int(id))
thehowl marked this conversation as resolved.
Show resolved Hide resolved
}
Loading