display imgur thumbnails. fixes #17

This commit is contained in:
Ryan Stafford 2023-07-07 13:42:57 -04:00
parent 48d12530fe
commit e46820ed3f
2 changed files with 13 additions and 1 deletions

View file

@ -111,6 +111,18 @@ var funcMap = template.FuncMap{
} }
return false return false
}, },
"thumbnail": func(p types.Post) string {
if p.ThumbnailURL.IsValid() {
return p.ThumbnailURL.String() + "?format=jpg&thumbnail=96"
}
re := regexp.MustCompile(`^https:\/\/i.imgur.com\/([a-zA-Z0-9]+)\.([a-z]+)$`)
if re.MatchString(p.URL.String()) {
return re.ReplaceAllString(p.URL.String(), "https://i.imgur.com/${1}s.$2")
} else if p.URL.IsValid() {
return "/_/static/link.png"
}
return "/_/static/text.png"
},
"humanize": humanize.Time, "humanize": humanize.Time,
"markdown": func(host string, body string) template.HTML { "markdown": func(host string, body string) template.HTML {
var buf bytes.Buffer var buf bytes.Buffer

View file

@ -21,7 +21,7 @@
{{ end }} {{ end }}
{{ if ne .State.Op "vote_post" }} {{ if ne .State.Op "vote_post" }}
</div> </div>
<div class="thumb"><div {{ if and .Post.NSFW (not (and .State.Community .State.Community.CommunityView.Community.NSFW))}}class="img-blur"{{end}} style="background-image: url({{if .Post.ThumbnailURL.IsValid}}{{.Post.ThumbnailURL.String}}?format=jpg&thumbnail=96{{else if .Post.URL.IsValid}}/_/static/link.png{{else}}/_/static/text.png{{end}})"></div></div> <div class="thumb"><div {{ if and .Post.NSFW (not (and .State.Community .State.Community.CommunityView.Community.NSFW))}}class="img-blur"{{end}} style="background-image: url({{thumbnail .Post}})"></div></div>
<div class="entry"> <div class="entry">
<div class="title"> <div class="title">
<a class="url" href="{{ if .Post.URL.IsValid }}{{ .Post.URL }}{{ else }}/{{ .State.Host }}/post/{{ .Post.ID }}{{ end }}">{{ rmmarkdown .Post.Name }}</a> <a class="url" href="{{ if .Post.URL.IsValid }}{{ .Post.URL }}{{ else }}/{{ .State.Host }}/post/{{ .Post.ID }}{{ end }}">{{ rmmarkdown .Post.Name }}</a>