Search Apps Documentation Source Content File Folder Download Copy Actions Download

format.gno

1.53 Kb · 82 lines
 1package boards2
 2
 3import (
 4	"strconv"
 5	"strings"
 6
 7	"gno.land/p/gnoland/boards"
 8	"gno.land/p/moul/md"
 9
10	"gno.land/r/sys/users"
11)
12
13const dateFormat = "2006-01-02 3:04pm MST"
14
15func padLeft(s string, length int) string {
16	if len(s) >= length {
17		return s
18	}
19	return strings.Repeat(" ", length-len(s)) + s
20}
21
22func padZero(u64 uint64, length int) string {
23	s := strconv.Itoa(int(u64))
24	if len(s) >= length {
25		return s
26	}
27	return strings.Repeat("0", length-len(s)) + s
28}
29
30func indentBody(indent string, body string) string {
31	var (
32		res   string
33		lines = strings.Split(body, "\n")
34	)
35	for i, line := range lines {
36		if i > 0 {
37			// Add two spaces to keep newlines within Markdown
38			res += "  \n"
39		}
40		res += indent + line
41	}
42	return res
43}
44
45func summaryOf(text string, length int) string {
46	lines := strings.SplitN(text, "\n", 2)
47	line := lines[0]
48	if len(line) > length {
49		line = line[:(length-3)] + "..."
50	} else if len(lines) > 1 {
51		line = line + "..."
52	}
53	return line
54}
55
56func userLink(addr address) string {
57	if u := users.ResolveAddress(addr); u != nil {
58		return md.UserLink(u.Name())
59	}
60	return md.UserLink(addr.String())
61}
62
63func getRoleBadge(post *boards.Post) string {
64	if post == nil || post.Board == nil || post.Board.Permissions == nil {
65		return ""
66	}
67
68	perms := post.Board.Permissions
69	creator := post.Creator
70
71	// Check roles in order of priority
72	if perms.HasRole(creator, RoleOwner) {
73		return " `owner`"
74	}
75	if perms.HasRole(creator, RoleAdmin) {
76		return " `admin`"
77	}
78	if perms.HasRole(creator, RoleModerator) {
79		return " `mod`"
80	}
81	return ""
82}