loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>

authored by

Xavier Vello
Xavier Vello
and committed by
Earl Warren
147ae2c5 9b8622bc

+157
+1
release-notes/9.0.0/fix/4427.md
··· 1 + Fixed social media previews for links to wiki pages.
+6
templates/base/head_opengraph.tmpl
··· 24 24 <meta property="og:description" content="{{StringUtils.EllipsisString $commitMessageBody 300}}"> 25 25 {{- end -}} 26 26 {{end}} 27 + {{else if .Pages}} 28 + <meta property="og:title" content="{{.Title}}"> 29 + <meta property="og:url" content="{{AppUrl}}{{.Link}}"> 30 + {{if .Repository.Description}} 31 + <meta property="og:description" content="{{StringUtils.EllipsisString .Repository.Description 300}}"> 32 + {{end}} 27 33 {{else}} 28 34 <meta property="og:title" content="{{.Repository.Name}}"> 29 35 <meta property="og:url" content="{{.Repository.HTMLURL}}">
+150
tests/integration/opengraph_test.go
··· 1 + // Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved. 2 + // SPDX-License-Identifier: MIT 3 + 4 + package integration 5 + 6 + import ( 7 + "net/http" 8 + "testing" 9 + 10 + "code.gitea.io/gitea/modules/setting" 11 + "code.gitea.io/gitea/tests" 12 + 13 + "github.com/PuerkitoBio/goquery" 14 + "github.com/stretchr/testify/assert" 15 + ) 16 + 17 + func TestOpenGraphProperties(t *testing.T) { 18 + defer tests.PrepareTestEnv(t)() 19 + siteName := "Forgejo: Beyond coding. We Forge." 20 + 21 + cases := []struct { 22 + name string 23 + url string 24 + expected map[string]string 25 + }{ 26 + { 27 + name: "website root", 28 + url: "/", 29 + expected: map[string]string{ 30 + "og:title": siteName, 31 + "og:url": setting.AppURL, 32 + "og:description": "Forgejo is a self-hosted lightweight software forge. Easy to install and low maintenance, it just does the job.", 33 + "og:type": "website", 34 + "og:image": "/assets/img/logo.png", 35 + "og:site_name": siteName, 36 + }, 37 + }, 38 + { 39 + name: "profile page without description", 40 + url: "/user30", 41 + expected: map[string]string{ 42 + "og:title": "User Thirty", 43 + "og:url": setting.AppURL + "user30", 44 + "og:type": "profile", 45 + "og:image": "https://secure.gravatar.com/avatar/eae1f44b34ff27284cb0792c7601c89c?d=identicon", 46 + "og:site_name": siteName, 47 + }, 48 + }, 49 + { 50 + name: "profile page with description", 51 + url: "/the_34-user.with.all.allowedchars", 52 + expected: map[string]string{ 53 + "og:title": "the_1-user.with.all.allowedChars", 54 + "og:url": setting.AppURL + "the_34-user.with.all.allowedChars", 55 + "og:description": "some [commonmark](https://commonmark.org/)!", 56 + "og:type": "profile", 57 + "og:image": setting.AppURL + "avatars/avatar34", 58 + "og:site_name": siteName, 59 + }, 60 + }, 61 + { 62 + name: "issue", 63 + url: "/user2/repo1/issues/1", 64 + expected: map[string]string{ 65 + "og:title": "issue1", 66 + "og:url": setting.AppURL + "user2/repo1/issues/1", 67 + "og:description": "content for the first issue", 68 + "og:type": "object", 69 + "og:image": "https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?d=identicon", 70 + "og:site_name": siteName, 71 + }, 72 + }, 73 + { 74 + name: "pull request", 75 + url: "/user2/repo1/pulls/2", 76 + expected: map[string]string{ 77 + "og:title": "issue2", 78 + "og:url": setting.AppURL + "user2/repo1/pulls/2", 79 + "og:description": "content for the second issue", 80 + "og:type": "object", 81 + "og:image": "https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?d=identicon", 82 + "og:site_name": siteName, 83 + }, 84 + }, 85 + { 86 + name: "file in repo", 87 + url: "/user27/repo49/src/branch/master/test/test.txt", 88 + expected: map[string]string{ 89 + "og:title": "repo49/test/test.txt at master", 90 + "og:url": setting.AppURL + "/user27/repo49/src/branch/master/test/test.txt", 91 + "og:type": "object", 92 + "og:image": "https://secure.gravatar.com/avatar/7095710e927665f1bdd1ced94152f232?d=identicon", 93 + "og:site_name": siteName, 94 + }, 95 + }, 96 + { 97 + name: "wiki page for repo without description", 98 + url: "/user2/repo1/wiki/Page-With-Spaced-Name", 99 + expected: map[string]string{ 100 + "og:title": "Page With Spaced Name", 101 + "og:url": setting.AppURL + "/user2/repo1/wiki/Page-With-Spaced-Name", 102 + "og:type": "object", 103 + "og:image": "https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?d=identicon", 104 + "og:site_name": siteName, 105 + }, 106 + }, 107 + { 108 + name: "index page for repo without description", 109 + url: "/user2/repo1", 110 + expected: map[string]string{ 111 + "og:title": "repo1", 112 + "og:url": setting.AppURL + "user2/repo1", 113 + "og:type": "object", 114 + "og:image": "https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?d=identicon", 115 + "og:site_name": siteName, 116 + }, 117 + }, 118 + { 119 + name: "index page for repo with description", 120 + url: "/user27/repo49", 121 + expected: map[string]string{ 122 + "og:title": "repo49", 123 + "og:url": setting.AppURL + "user27/repo49", 124 + "og:description": "A wonderful repository with more than just a README.md", 125 + "og:type": "object", 126 + "og:image": "https://secure.gravatar.com/avatar/7095710e927665f1bdd1ced94152f232?d=identicon", 127 + "og:site_name": siteName, 128 + }, 129 + }, 130 + } 131 + 132 + for _, tc := range cases { 133 + t.Run(tc.name, func(t *testing.T) { 134 + req := NewRequest(t, "GET", tc.url) 135 + resp := MakeRequest(t, req, http.StatusOK) 136 + doc := NewHTMLParser(t, resp.Body) 137 + 138 + foundProps := make(map[string]string) 139 + doc.Find("head meta[property^=\"og:\"]").Each(func(_ int, selection *goquery.Selection) { 140 + prop, foundProp := selection.Attr("property") 141 + assert.True(t, foundProp) 142 + content, foundContent := selection.Attr("content") 143 + assert.True(t, foundContent, "opengraph meta tag without a content property") 144 + foundProps[prop] = content 145 + }) 146 + 147 + assert.EqualValues(t, tc.expected, foundProps, "mismatching opengraph properties") 148 + }) 149 + } 150 + }