Skip to content

Commit 5743d7c

Browse files
authored
Improve opengraph previews (#26851)
Add more useful Open Graph metadata for commit and file URLs: - Set `og:title` to the page title, which is a concise summary in both cases (`<commit message> · <commit hash>` and `<filename> at <branch>`, respectively) - Set `og:description` to the commit message body, if available - Set `og:url` to the relevant URLs instead of the repo URL Also move the relevant meta tags into a separate template as they now take up the majority of the base head template.
1 parent 9881b8a commit 5743d7c

File tree

2 files changed

+43
-36
lines changed

2 files changed

+43
-36
lines changed

templates/base/head.tmpl

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,42 +25,7 @@
2525
.ui.secondary.menu .dropdown.item > .menu { margin-top: 0; }
2626
</style>
2727
</noscript>
28-
{{if .PageIsUserProfile}}
29-
<meta property="og:title" content="{{.ContextUser.DisplayName}}">
30-
<meta property="og:type" content="profile">
31-
<meta property="og:image" content="{{.ContextUser.AvatarLink ctx}}">
32-
<meta property="og:url" content="{{.ContextUser.HTMLURL}}">
33-
{{if .ContextUser.Description}}
34-
<meta property="og:description" content="{{.ContextUser.Description}}">
35-
{{end}}
36-
{{else if .Repository}}
37-
{{if .Issue}}
38-
<meta property="og:title" content="{{.Issue.Title}}">
39-
<meta property="og:url" content="{{.Issue.HTMLURL}}">
40-
{{if .Issue.Content}}
41-
<meta property="og:description" content="{{.Issue.Content}}">
42-
{{end}}
43-
{{else}}
44-
<meta property="og:title" content="{{.Repository.Name}}">
45-
<meta property="og:url" content="{{.Repository.HTMLURL}}">
46-
{{if .Repository.Description}}
47-
<meta property="og:description" content="{{.Repository.Description}}">
48-
{{end}}
49-
{{end}}
50-
<meta property="og:type" content="object">
51-
{{if (.Repository.AvatarLink ctx)}}
52-
<meta property="og:image" content="{{.Repository.AvatarLink ctx}}">
53-
{{else}}
54-
<meta property="og:image" content="{{.Repository.Owner.AvatarLink ctx}}">
55-
{{end}}
56-
{{else}}
57-
<meta property="og:title" content="{{AppName}}">
58-
<meta property="og:type" content="website">
59-
<meta property="og:image" content="{{AssetUrlPrefix}}/img/logo.png">
60-
<meta property="og:url" content="{{AppUrl}}">
61-
<meta property="og:description" content="{{MetaDescription}}">
62-
{{end}}
63-
<meta property="og:site_name" content="{{AppName}}">
28+
{{template "base/head_opengraph" .}}
6429
{{template "base/head_style" .}}
6530
{{template "custom/header" .}}
6631
</head>

templates/base/head_opengraph.tmpl

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{{if .PageIsUserProfile}}
2+
<meta property="og:title" content="{{.ContextUser.DisplayName}}">
3+
<meta property="og:type" content="profile">
4+
<meta property="og:image" content="{{.ContextUser.AvatarLink ctx}}">
5+
<meta property="og:url" content="{{.ContextUser.HTMLURL}}">
6+
{{if .ContextUser.Description}}
7+
<meta property="og:description" content="{{.ContextUser.Description}}">
8+
{{end}}
9+
{{else if .Repository}}
10+
{{if .Issue}}
11+
<meta property="og:title" content="{{.Issue.Title}}">
12+
<meta property="og:url" content="{{.Issue.HTMLURL}}">
13+
{{if .Issue.Content}}
14+
<meta property="og:description" content="{{.Issue.Content}}">
15+
{{end}}
16+
{{else if or .PageIsDiff .IsViewFile}}
17+
<meta property="og:title" content="{{.Title}}">
18+
<meta property="og:url" content="{{AppUrl}}{{.Link}}">
19+
{{if and .PageIsDiff (IsMultilineCommitMessage .Commit.Message)}}
20+
<meta property="og:description" content="{{RenderCommitBody $.Context .Commit.Message $.RepoLink $.Repository.ComposeMetas}}">
21+
{{end}}
22+
{{else}}
23+
<meta property="og:title" content="{{.Repository.Name}}">
24+
<meta property="og:url" content="{{.Repository.HTMLURL}}">
25+
{{if .Repository.Description}}
26+
<meta property="og:description" content="{{.Repository.Description}}">
27+
{{end}}
28+
{{end}}
29+
<meta property="og:type" content="object">
30+
{{if (.Repository.AvatarLink ctx)}}
31+
<meta property="og:image" content="{{.Repository.AvatarLink ctx}}">
32+
{{else}}
33+
<meta property="og:image" content="{{.Repository.Owner.AvatarLink ctx}}">
34+
{{end}}
35+
{{else}}
36+
<meta property="og:title" content="{{AppName}}">
37+
<meta property="og:type" content="website">
38+
<meta property="og:image" content="{{AssetUrlPrefix}}/img/logo.png">
39+
<meta property="og:url" content="{{AppUrl}}">
40+
<meta property="og:description" content="{{MetaDescription}}">
41+
{{end}}
42+
<meta property="og:site_name" content="{{AppName}}">

0 commit comments

Comments
 (0)