Include extracted images in draft social metadata

Previously, we didn't extract images for draft posts. This fixes that.
pull/266/head
Matt Baer 5 years ago
parent 8e8eb3c563
commit 34d196376e
  1. 10
      posts.go
  2. 4
      templates/post.tmpl

@ -62,6 +62,7 @@ type (
Description string Description string
Author string Author string
Views int64 Views int64
Images []string
IsPlainText bool IsPlainText bool
IsCode bool IsCode bool
IsLinkable bool IsLinkable bool
@ -381,6 +382,7 @@ func handleViewPost(app *App, w http.ResponseWriter, r *http.Request) error {
} }
if !isRaw { if !isRaw {
post.HTMLContent = template.HTML(applyMarkdown([]byte(content), "", app.cfg)) post.HTMLContent = template.HTML(applyMarkdown([]byte(content), "", app.cfg))
post.Images = extractImages(post.Content)
} }
} }
@ -1544,7 +1546,11 @@ func (rp *RawPost) Created8601() string {
var imageURLRegex = regexp.MustCompile(`(?i)^https?:\/\/[^ ]*\.(gif|png|jpg|jpeg|image)$`) var imageURLRegex = regexp.MustCompile(`(?i)^https?:\/\/[^ ]*\.(gif|png|jpg|jpeg|image)$`)
func (p *Post) extractImages() { func (p *Post) extractImages() {
matches := extract.ExtractUrls(p.Content) p.Images = extractImages(p.Content)
}
func extractImages(content string) []string {
matches := extract.ExtractUrls(content)
urls := map[string]bool{} urls := map[string]bool{}
for i := range matches { for i := range matches {
u := matches[i].Text u := matches[i].Text
@ -1558,5 +1564,5 @@ func (p *Post) extractImages() {
for k := range urls { for k := range urls {
resURLs = append(resURLs, k) resURLs = append(resURLs, k)
} }
p.Images = resURLs return resURLs
} }

@ -23,13 +23,13 @@
<meta name="twitter:description" content="{{.Description}}"> <meta name="twitter:description" content="{{.Description}}">
{{if gt .Views 1}}<meta name="twitter:label1" value="Views"> {{if gt .Views 1}}<meta name="twitter:label1" value="Views">
<meta name="twitter:data1" value="{{largeNumFmt .Views}}">{{end}} <meta name="twitter:data1" value="{{largeNumFmt .Views}}">{{end}}
<meta name="twitter:image" content="{{.Host}}/img/wf-sq.png"> {{if gt (len .Images) 0}}<meta name="twitter:image" content="{{index .Images 0}}">{{else}}<meta name="twitter:image" content="{{.Host}}/img/wf-sq.png">{{end}}
<meta property="og:title" content="{{if .Title}}{{.Title}}{{else}}{{.GenTitle}}{{end}}" /> <meta property="og:title" content="{{if .Title}}{{.Title}}{{else}}{{.GenTitle}}{{end}}" />
<meta property="og:site_name" content="{{.SiteName}}" /> <meta property="og:site_name" content="{{.SiteName}}" />
<meta property="og:type" content="article" /> <meta property="og:type" content="article" />
<meta property="og:url" content="{{.Host}}/{{if .SingleUser}}d/{{end}}{{.ID}}" /> <meta property="og:url" content="{{.Host}}/{{if .SingleUser}}d/{{end}}{{.ID}}" />
<meta property="og:description" content="{{.Description}}" /> <meta property="og:description" content="{{.Description}}" />
<meta property="og:image" content="{{.Host}}/img/wf-sq.png"> {{range .Images}}<meta property="og:image" content="{{.}}" />{{else}}<meta property="og:image" content="{{.Host}}/img/wf-sq.png">{{end}}
{{if .Author}}<meta property="article:author" content="https://{{.Author}}" />{{end}} {{if .Author}}<meta property="article:author" content="https://{{.Author}}" />{{end}}
<!-- Add highlighting logic --> <!-- Add highlighting logic -->
{{template "highlighting" .}} {{template "highlighting" .}}

Loading…
Cancel
Save