Merge pull request #266 from writeas/fix-social-images
Fix image extraction for social metadata
This commit is contained in:
commit
fca864c94a
25
posts.go
25
posts.go
|
@ -16,6 +16,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -62,6 +63,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 +383,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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1541,22 +1544,32 @@ func (rp *RawPost) Created8601() string {
|
||||||
return rp.Created.Format("2006-01-02T15:04:05Z")
|
return rp.Created.Format("2006-01-02T15:04:05Z")
|
||||||
}
|
}
|
||||||
|
|
||||||
var imageURLRegex = regexp.MustCompile(`(?i)^https?:\/\/[^ ]*\.(gif|png|jpg|jpeg|image)$`)
|
var imageURLRegex = regexp.MustCompile(`(?i)[^ ]+\.(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
|
uRaw := matches[i].Text
|
||||||
if !imageURLRegex.MatchString(u) {
|
// Parse the extracted text so we can examine the path
|
||||||
|
u, err := url.Parse(uRaw)
|
||||||
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
urls[u] = true
|
// Ensure the path looks like it leads to an image file
|
||||||
|
if !imageURLRegex.MatchString(u.Path) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
urls[uRaw] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
resURLs := make([]string, 0)
|
resURLs := make([]string, 0)
|
||||||
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…
Reference in New Issue