-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kindy.go
230 lines (201 loc) · 5.66 KB
/
kindy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package gerbendev
import (
"bytes"
"fmt"
"html/template"
"log/slog"
"path/filepath"
"strings"
"time"
"github.com/dustin/go-humanize"
"github.com/microcosm-cc/bluemonday"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
)
var gm goldmark.Markdown
type KindyType string
const (
KindyEditorPath = "/kindy"
KindyDataPath = "/kd/"
KindyContentPath = "content/kindy/"
KindyURLLikes = "/likes/"
KindyURLNotes = "/notes/"
KindyURLPhotos = "/photos/"
KindyURLPosts = "/posts/"
KindyURLReposts = "/reposts/"
KindyURLReplies = "/replies/"
KindySummaryLike = "Liked"
KindySummaryRepost = "Reposted"
KindyTypeNote KindyType = "note"
KindyTypePost KindyType = "post"
KindyTypePhoto KindyType = "photo"
KindyTypeLike KindyType = "like"
KindyTypeRepost KindyType = "repost"
KindyTypeReplies KindyType = "reply"
)
func init() {
gm = goldmark.New(
goldmark.WithExtensions(extension.GFM),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
),
goldmark.WithRendererOptions(
html.WithHardWraps(),
),
)
}
// Kindy is a datastructure for content that adheres to Microformats 2
type Kindy struct {
Type KindyType `json:"type"`
Title string `json:"title,omitempty"`
Summary template.HTML `json:"summary,omitempty"`
Content template.HTML `json:"content,omitempty"`
Markdown string `json:"markdown,omitempty"`
PublishedAt time.Time `json:"publishedAt"`
Slug string `json:"slug,omitempty"`
Permalink string `json:"permalink,omitempty"`
Author *KindyAuthor `json:"author,omitempty"`
Syndication []KindySyndication `json:"syndication,omitempty"`
LikeOf string `json:"likeOf,omitempty"`
RepostOf string `json:"repostOf,omitempty"`
ReplyTo string `json:"replyTo,omitempty"`
Geo *KindyGeo `json:"geo,omitempty"`
Tags []string `json:"tags,omitempty"`
}
type KindyAuthor struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
Photo string `json:"photo,omitempty"`
}
type KindySyndication struct {
Type string `json:"type,omitempty"` // Free form field to be set by the user, can be used to display different type of icons
URL string `json:"url,omitempty"`
}
type KindyGeo struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
}
// MFType returns the microformat h-type based on the type of Kindy
func (k Kindy) MFType() string {
return "h-entry"
}
func (k Kindy) Thumbnail() string {
if k.Type == KindyTypePhoto {
filePath := string(k.Content)
ext := filepath.Ext(filePath)
return fmt.Sprintf("%s_thumb%s", strings.TrimSuffix(filePath, ext), ext)
}
return ""
}
// HasContent returns true if the Kindy has content
// either as Markdown or as HTML
func (k Kindy) HasContent() bool {
return k.Markdown != "" || k.Content != ""
}
// GetContent returns content or Markdown if it exists
func (k Kindy) GetContent() template.HTML {
if k.Markdown != "" {
return template.HTML(MarkdownToHTML(k.Markdown))
}
return k.Content
}
// ContentStripped strips all HTML with a strict policy
// but still returns a template.HTML so that properly escaped HTML entities still work
// It has an 'optional' args list, but we really only except 1 int which limits the length
func (k Kindy) ContentStripped(args ...int) template.HTML {
p := bluemonday.StrictPolicy()
content := p.Sanitize(string(k.GetContent()))
content = strings.Join(strings.Fields(content), " ")
if len(args) > 0 && len(content) > args[0] {
content = content[:args[0]] + "…"
}
return template.HTML(content)
}
func (k Kindy) MustTitle() string {
if k.Type == KindyTypeLike || k.Type == KindyTypeRepost {
url := k.LikeOf
if k.Type == KindyTypeRepost {
url = k.RepostOf
}
return string(k.Summary) + " " + url
}
if k.Title != "" {
return k.Title
}
if k.Summary != "" {
return string(k.Summary)
}
if k.Type == KindyTypeNote || k.Type == KindyTypeReplies {
// for Notes, we might as well use the content
p := bluemonday.StrictPolicy()
return p.Sanitize(string(k.GetContent()))
}
if k.Permalink != "" {
return k.Permalink
}
return string(k.Type)
}
func (k Kindy) MustDescription() template.HTML {
p := bluemonday.StrictPolicy()
if k.Summary != "" {
return template.HTML(p.Sanitize(string(k.Summary)))
}
content := k.GetContent()
if content != "" {
return template.HTML(p.Sanitize(string(content)))
}
return template.HTML(k.Type)
}
func (k Kindy) HasFlickrSyndication() bool {
for _, s := range k.Syndication {
if s.Type == "flickr" {
return true
}
}
return false
}
func (k Kindy) TimeAgo() string {
return humanize.Time(k.PublishedAt)
}
func (kt KindyType) Emoji() string {
emojis := map[KindyType]string{
KindyTypePost: "📝",
KindyTypeNote: "📜",
KindyTypePhoto: "📸",
KindyTypeRepost: "🔁",
KindyTypeLike: "⭐",
KindyTypeReplies: "💬",
}
if v, ok := emojis[kt]; ok {
return v
}
// default to post
return emojis[KindyTypePost]
}
func (kt KindyType) URL() string {
switch kt {
case KindyTypeNote:
return KindyURLNotes
case KindyTypePhoto:
return KindyURLPhotos
case KindyTypePost:
return KindyURLPosts
case KindyTypeRepost:
return KindyURLReposts
case KindyTypeLike:
return KindyURLLikes
case KindyTypeReplies:
return KindyURLReplies
default:
return ""
}
}
func MarkdownToHTML(md string) string {
var buf bytes.Buffer
if err := gm.Convert([]byte(md), &buf); err != nil {
slog.Error("failed to convert markdown to html", "error", err)
}
return buf.String()
}