Skip to content

Commit 21455f2

Browse files
authored
Fix open graph images (#597)
1 parent feb5b53 commit 21455f2

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

Diff for: go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ require (
4141
github.com/otiai10/mint v1.6.3 // indirect
4242
github.com/rivo/uniseg v0.4.7 // indirect
4343
github.com/stretchr/testify v1.9.0 // indirect
44-
golang.org/x/image v0.8.0 // indirect
44+
golang.org/x/image v0.23.0 // indirect
4545
golang.org/x/sys v0.29.0 // indirect
4646
golang.org/x/term v0.28.0 // indirect
4747
gopkg.in/yaml.v2 v2.4.0 // indirect

Diff for: go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -2382,6 +2382,8 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
23822382
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
23832383
golang.org/x/image v0.8.0 h1:agUcRXV/+w6L9ryntYYsF2x9fQTMd4T8fiiYXAVW6Jg=
23842384
golang.org/x/image v0.8.0/go.mod h1:PwLxp3opCYg4WR2WO9P0L6ESnsD6bLTWcw8zanLMVFM=
2385+
golang.org/x/image v0.23.0 h1:HseQ7c2OpPKTPVzNjG5fwJsOTCiiwS4QdsYi5XU6H68=
2386+
golang.org/x/image v0.23.0/go.mod h1:wJJBTdLfCCf3tiHa1fNxpZmUI4mmoZvwMCPP0ddoNKY=
23852387
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
23862388
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
23872389
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=

Diff for: manipulations/opengraphimg/opengraphimg.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,15 @@ func getSuitableImg(runtime manipulations.Runtime, imgPath string) (*genimgs.Gen
9393
}
9494

9595
imgsByTypes := genimgs.GroupByType(imgs)
96-
imgsByType := imgsByTypes[""]
97-
if len(imgsByType) == 0 {
96+
basicImages := imgsByTypes[""]
97+
if len(basicImages) == 0 {
9898
return nil, nil
9999
}
100100

101-
sort.Slice(imgs, func(i, j int) bool {
102-
return imgs[i].Size > imgs[j].Size
101+
sort.Slice(basicImages, func(i, j int) bool {
102+
return basicImages[i].Size > basicImages[j].Size
103103
})
104-
for _, i := range imgs {
104+
for _, i := range basicImages {
105105
if i.Size <= RECOMMENDED_OG_IMG_WIDTH {
106106
addImgCache(imgPath, i)
107107
return &i, nil

Diff for: manipulations/opengraphimg/opengraphimg_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,31 @@ func TestManipulator(t *testing.T) {
115115
},
116116
wantHTML: `<html><head><meta content="http://base-url.com/images/default-social.1200xabc.png" property="og:image"/></head><body></body></html>`,
117117
},
118+
119+
{
120+
description: "use basic image and not other image",
121+
findNodes: htmlparsing.FindNodesByTag,
122+
doc: MustGetNode(t, `<meta property="og:image" content="/images/default-social.png" />`),
123+
genimgsLookupSizes: func(s3 *s3.Client, conf *config.Config, imgPath string) ([]genimgs.GenImg, error) {
124+
return []genimgs.GenImg{
125+
{
126+
Size: RECOMMENDED_OG_IMG_WIDTH,
127+
URL: "/images/default-social.1200xabc.png",
128+
},
129+
{
130+
Size: RECOMMENDED_OG_IMG_WIDTH,
131+
Type: "image/webp",
132+
URL: "/images/default-social.1200xabc.webp",
133+
},
134+
{
135+
Size: RECOMMENDED_OG_IMG_WIDTH,
136+
Type: "image/avif",
137+
URL: "/images/default-social.1200xabc.avif",
138+
},
139+
}, nil
140+
},
141+
wantHTML: `<html><head><meta content="http://base-url.com/images/default-social.1200xabc.png" property="og:image"/></head><body></body></html>`,
142+
},
118143
}
119144

120145
for _, tt := range tests {

0 commit comments

Comments
 (0)