Fix opengraph meta for wiki pages (#4427)

Fixes https://codeberg.org/forgejo/forgejo/issues/4417 by adding a conditional branch to the `head_opengraph` template to match wiki pages. I tried to be consistent with the other types:

- `og:title` is the wiki page title
- `og:url` is built via `{{AppUrl}}{{.Link}}` like it is done for commit and file views. This has the caveat of doubling the slash (see test below). Should we `{{trimSuffix "/" AppUrl}}` to remove this, if sprig is available?
- `og:description` is the repository description to match GH behaviour. Also, the first sentences of the page might not be descriptive enough. Should we prefix the repo description with the repo name?
- `og:type` and `og:image` are common

Added a `TestOpenGraphProperties` integration test using existing fixtures. Coverage is not 100% but can be improved later.

## Output on a test repo

```html
<meta property="og:title" content="Project architecture">
<meta property="og:url" content="http://localhost:3000//xvello/wiki-test/wiki/Project-architecture">
<meta property="og:description" content="description for a test project">
<meta property="og:type" content="object">
<meta property="og:image" content="http://localhost:3000/avatars/3dd4d1e4eef065d1b4ad4bdb081ab6e7">
```

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4427
Co-authored-by: Xavier Vello <xavier.vello@gmail.com>
Co-committed-by: Xavier Vello <xavier.vello@gmail.com>
This commit is contained in:
Xavier Vello 2024-07-10 18:29:35 +00:00 committed by Earl Warren
parent 9b8622bc58
commit 147ae2c5be
3 changed files with 157 additions and 0 deletions

View file

@ -0,0 +1 @@
Fixed social media previews for links to wiki pages.

View file

@ -24,6 +24,12 @@
<meta property="og:description" content="{{StringUtils.EllipsisString $commitMessageBody 300}}">
{{- end -}}
{{end}}
{{else if .Pages}}
<meta property="og:title" content="{{.Title}}">
<meta property="og:url" content="{{AppUrl}}{{.Link}}">
{{if .Repository.Description}}
<meta property="og:description" content="{{StringUtils.EllipsisString .Repository.Description 300}}">
{{end}}
{{else}}
<meta property="og:title" content="{{.Repository.Name}}">
<meta property="og:url" content="{{.Repository.HTMLURL}}">

View file

@ -0,0 +1,150 @@
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
// SPDX-License-Identifier: MIT
package integration
import (
"net/http"
"testing"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/tests"
"github.com/PuerkitoBio/goquery"
"github.com/stretchr/testify/assert"
)
func TestOpenGraphProperties(t *testing.T) {
defer tests.PrepareTestEnv(t)()
siteName := "Forgejo: Beyond coding. We Forge."
cases := []struct {
name string
url string
expected map[string]string
}{
{
name: "website root",
url: "/",
expected: map[string]string{
"og:title": siteName,
"og:url": setting.AppURL,
"og:description": "Forgejo is a self-hosted lightweight software forge. Easy to install and low maintenance, it just does the job.",
"og:type": "website",
"og:image": "/assets/img/logo.png",
"og:site_name": siteName,
},
},
{
name: "profile page without description",
url: "/user30",
expected: map[string]string{
"og:title": "User Thirty",
"og:url": setting.AppURL + "user30",
"og:type": "profile",
"og:image": "https://secure.gravatar.com/avatar/eae1f44b34ff27284cb0792c7601c89c?d=identicon",
"og:site_name": siteName,
},
},
{
name: "profile page with description",
url: "/the_34-user.with.all.allowedchars",
expected: map[string]string{
"og:title": "the_1-user.with.all.allowedChars",
"og:url": setting.AppURL + "the_34-user.with.all.allowedChars",
"og:description": "some [commonmark](https://commonmark.org/)!",
"og:type": "profile",
"og:image": setting.AppURL + "avatars/avatar34",
"og:site_name": siteName,
},
},
{
name: "issue",
url: "/user2/repo1/issues/1",
expected: map[string]string{
"og:title": "issue1",
"og:url": setting.AppURL + "user2/repo1/issues/1",
"og:description": "content for the first issue",
"og:type": "object",
"og:image": "https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?d=identicon",
"og:site_name": siteName,
},
},
{
name: "pull request",
url: "/user2/repo1/pulls/2",
expected: map[string]string{
"og:title": "issue2",
"og:url": setting.AppURL + "user2/repo1/pulls/2",
"og:description": "content for the second issue",
"og:type": "object",
"og:image": "https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?d=identicon",
"og:site_name": siteName,
},
},
{
name: "file in repo",
url: "/user27/repo49/src/branch/master/test/test.txt",
expected: map[string]string{
"og:title": "repo49/test/test.txt at master",
"og:url": setting.AppURL + "/user27/repo49/src/branch/master/test/test.txt",
"og:type": "object",
"og:image": "https://secure.gravatar.com/avatar/7095710e927665f1bdd1ced94152f232?d=identicon",
"og:site_name": siteName,
},
},
{
name: "wiki page for repo without description",
url: "/user2/repo1/wiki/Page-With-Spaced-Name",
expected: map[string]string{
"og:title": "Page With Spaced Name",
"og:url": setting.AppURL + "/user2/repo1/wiki/Page-With-Spaced-Name",
"og:type": "object",
"og:image": "https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?d=identicon",
"og:site_name": siteName,
},
},
{
name: "index page for repo without description",
url: "/user2/repo1",
expected: map[string]string{
"og:title": "repo1",
"og:url": setting.AppURL + "user2/repo1",
"og:type": "object",
"og:image": "https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?d=identicon",
"og:site_name": siteName,
},
},
{
name: "index page for repo with description",
url: "/user27/repo49",
expected: map[string]string{
"og:title": "repo49",
"og:url": setting.AppURL + "user27/repo49",
"og:description": "A wonderful repository with more than just a README.md",
"og:type": "object",
"og:image": "https://secure.gravatar.com/avatar/7095710e927665f1bdd1ced94152f232?d=identicon",
"og:site_name": siteName,
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
req := NewRequest(t, "GET", tc.url)
resp := MakeRequest(t, req, http.StatusOK)
doc := NewHTMLParser(t, resp.Body)
foundProps := make(map[string]string)
doc.Find("head meta[property^=\"og:\"]").Each(func(_ int, selection *goquery.Selection) {
prop, foundProp := selection.Attr("property")
assert.True(t, foundProp)
content, foundContent := selection.Attr("content")
assert.True(t, foundContent, "opengraph meta tag without a content property")
foundProps[prop] = content
})
assert.EqualValues(t, tc.expected, foundProps, "mismatching opengraph properties")
})
}
}