diff options
author | Linus Groh <mail@linusgroh.de> | 2023-09-01 18:59:24 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-01 16:59:24 +0000 |
commit | 5743d7cb5bcd85c88ad7d128e0162893a074418b (patch) | |
tree | a35eeaeb6c1addc4fe564305ddd9eac01de0ff0f | |
parent | 9881b8a4e24bc81b1acd80b51c5c2541b063149e (diff) | |
download | gitea-5743d7cb5bcd85c88ad7d128e0162893a074418b.tar.gz gitea-5743d7cb5bcd85c88ad7d128e0162893a074418b.zip |
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.
-rw-r--r-- | templates/base/head.tmpl | 37 | ||||
-rw-r--r-- | templates/base/head_opengraph.tmpl | 42 |
2 files changed, 43 insertions, 36 deletions
diff --git a/templates/base/head.tmpl b/templates/base/head.tmpl index 8eebaebd70..c3645209cd 100644 --- a/templates/base/head.tmpl +++ b/templates/base/head.tmpl @@ -25,42 +25,7 @@ .ui.secondary.menu .dropdown.item > .menu { margin-top: 0; } </style> </noscript> -{{if .PageIsUserProfile}} - <meta property="og:title" content="{{.ContextUser.DisplayName}}"> - <meta property="og:type" content="profile"> - <meta property="og:image" content="{{.ContextUser.AvatarLink ctx}}"> - <meta property="og:url" content="{{.ContextUser.HTMLURL}}"> - {{if .ContextUser.Description}} - <meta property="og:description" content="{{.ContextUser.Description}}"> - {{end}} -{{else if .Repository}} - {{if .Issue}} - <meta property="og:title" content="{{.Issue.Title}}"> - <meta property="og:url" content="{{.Issue.HTMLURL}}"> - {{if .Issue.Content}} - <meta property="og:description" content="{{.Issue.Content}}"> - {{end}} - {{else}} - <meta property="og:title" content="{{.Repository.Name}}"> - <meta property="og:url" content="{{.Repository.HTMLURL}}"> - {{if .Repository.Description}} - <meta property="og:description" content="{{.Repository.Description}}"> - {{end}} - {{end}} - <meta property="og:type" content="object"> - {{if (.Repository.AvatarLink ctx)}} - <meta property="og:image" content="{{.Repository.AvatarLink ctx}}"> - {{else}} - <meta property="og:image" content="{{.Repository.Owner.AvatarLink ctx}}"> - {{end}} -{{else}} - <meta property="og:title" content="{{AppName}}"> - <meta property="og:type" content="website"> - <meta property="og:image" content="{{AssetUrlPrefix}}/img/logo.png"> - <meta property="og:url" content="{{AppUrl}}"> - <meta property="og:description" content="{{MetaDescription}}"> -{{end}} - <meta property="og:site_name" content="{{AppName}}"> + {{template "base/head_opengraph" .}} {{template "base/head_style" .}} {{template "custom/header" .}} </head> diff --git a/templates/base/head_opengraph.tmpl b/templates/base/head_opengraph.tmpl new file mode 100644 index 0000000000..fc3958b6b6 --- /dev/null +++ b/templates/base/head_opengraph.tmpl @@ -0,0 +1,42 @@ +{{if .PageIsUserProfile}} + <meta property="og:title" content="{{.ContextUser.DisplayName}}"> + <meta property="og:type" content="profile"> + <meta property="og:image" content="{{.ContextUser.AvatarLink ctx}}"> + <meta property="og:url" content="{{.ContextUser.HTMLURL}}"> + {{if .ContextUser.Description}} + <meta property="og:description" content="{{.ContextUser.Description}}"> + {{end}} +{{else if .Repository}} + {{if .Issue}} + <meta property="og:title" content="{{.Issue.Title}}"> + <meta property="og:url" content="{{.Issue.HTMLURL}}"> + {{if .Issue.Content}} + <meta property="og:description" content="{{.Issue.Content}}"> + {{end}} + {{else if or .PageIsDiff .IsViewFile}} + <meta property="og:title" content="{{.Title}}"> + <meta property="og:url" content="{{AppUrl}}{{.Link}}"> + {{if and .PageIsDiff (IsMultilineCommitMessage .Commit.Message)}} + <meta property="og:description" content="{{RenderCommitBody $.Context .Commit.Message $.RepoLink $.Repository.ComposeMetas}}"> + {{end}} + {{else}} + <meta property="og:title" content="{{.Repository.Name}}"> + <meta property="og:url" content="{{.Repository.HTMLURL}}"> + {{if .Repository.Description}} + <meta property="og:description" content="{{.Repository.Description}}"> + {{end}} + {{end}} + <meta property="og:type" content="object"> + {{if (.Repository.AvatarLink ctx)}} + <meta property="og:image" content="{{.Repository.AvatarLink ctx}}"> + {{else}} + <meta property="og:image" content="{{.Repository.Owner.AvatarLink ctx}}"> + {{end}} +{{else}} + <meta property="og:title" content="{{AppName}}"> + <meta property="og:type" content="website"> + <meta property="og:image" content="{{AssetUrlPrefix}}/img/logo.png"> + <meta property="og:url" content="{{AppUrl}}"> + <meta property="og:description" content="{{MetaDescription}}"> +{{end}} +<meta property="og:site_name" content="{{AppName}}"> |