Rewrite OpenGraph Header

This commit is contained in:
JakobDev 2025-01-01 20:43:20 +01:00
parent e28bd3c4d6
commit e99bf7f16d
No known key found for this signature in database
GPG key ID: 39DEF62C3ED6DC4C
9 changed files with 49 additions and 59 deletions

View file

@ -61,6 +61,9 @@ func Home(ctx *context.Context) {
ctx.Data["PageIsHome"] = true ctx.Data["PageIsHome"] = true
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
ctx.Data["OpenGraphDescription"] = setting.UI.Meta.Description
ctx.HTML(http.StatusOK, tplHome) ctx.HTML(http.StatusOK, tplHome)
} }

View file

@ -47,6 +47,12 @@ func Home(ctx *context.Context) {
ctx.Data["PageIsUserProfile"] = true ctx.Data["PageIsUserProfile"] = true
ctx.Data["Title"] = org.DisplayName() ctx.Data["Title"] = org.DisplayName()
ctx.Data["OpenGraphTitle"] = ctx.ContextUser.DisplayName()
ctx.Data["OpenGraphType"] = "profile"
ctx.Data["OpenGraphImageURL"] = ctx.ContextUser.AvatarLink(ctx)
ctx.Data["OpenGraphURL"] = ctx.ContextUser.HTMLURL()
ctx.Data["OpenGraphDescription"] = ctx.ContextUser.Description
var orderBy db.SearchOrderBy var orderBy db.SearchOrderBy
sortOrder := ctx.FormString("sort") sortOrder := ctx.FormString("sort")
if _, ok := repo_model.OrderByFlatMap[sortOrder]; !ok { if _, ok := repo_model.OrderByFlatMap[sortOrder]; !ok {

View file

@ -419,6 +419,10 @@ func Diff(ctx *context.Context) {
} }
} }
ctx.Data["OpenGraphTitle"] = commit.Summary() + " · " + base.ShortSha(commitID)
ctx.Data["OpenGraphURL"] = fmt.Sprintf("%s/commit/%s", ctx.Repo.Repository.HTMLURL(), commitID)
_, ctx.Data["OpenGraphDescription"], _ = strings.Cut(commit.Message(), "\n")
ctx.HTML(http.StatusOK, tplCommitPage) ctx.HTML(http.StatusOK, tplCommitPage)
} }

View file

@ -2070,6 +2070,9 @@ func ViewIssue(ctx *context.Context) {
ctx.Data["RefEndName"] = git.RefName(issue.Ref).ShortName() ctx.Data["RefEndName"] = git.RefName(issue.Ref).ShortName()
ctx.Data["NewPinAllowed"] = pinAllowed ctx.Data["NewPinAllowed"] = pinAllowed
ctx.Data["PinEnabled"] = setting.Repository.Issue.MaxPinned != 0 ctx.Data["PinEnabled"] = setting.Repository.Issue.MaxPinned != 0
ctx.Data["OpenGraphTitle"] = issue.Title
ctx.Data["OpenGraphURL"] = issue.HTMLURL()
ctx.Data["OpenGraphDescription"] = issue.Content
ctx.Data["OpenGraphImageURL"] = issue.SummaryCardURL() ctx.Data["OpenGraphImageURL"] = issue.SummaryCardURL()
ctx.Data["OpenGraphImageAltText"] = ctx.Tr("repo.issues.summary_card_alt", issue.Title, issue.Repo.FullName()) ctx.Data["OpenGraphImageAltText"] = ctx.Tr("repo.issues.summary_card_alt", issue.Title, issue.Repo.FullName())

View file

@ -394,6 +394,10 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry) {
ctx.Data["FileName"] = blob.Name() ctx.Data["FileName"] = blob.Name()
ctx.Data["RawFileLink"] = ctx.Repo.RepoLink + "/raw/" + ctx.Repo.BranchNameSubURL() + "/" + util.PathEscapeSegments(ctx.Repo.TreePath) ctx.Data["RawFileLink"] = ctx.Repo.RepoLink + "/raw/" + ctx.Repo.BranchNameSubURL() + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
ctx.Data["OpenGraphTitle"] = ctx.Data["Title"]
ctx.Data["OpenGraphURL"] = fmt.Sprintf("%s%s", setting.AppURL, ctx.Data["Link"])
ctx.Data["OpenGraphNoDescription"] = true
if entry.IsLink() { if entry.IsLink() {
_, link, err := entry.FollowLinks() _, link, err := entry.FollowLinks()
// Errors should be allowed, because this shouldn't // Errors should be allowed, because this shouldn't

View file

@ -535,6 +535,9 @@ func Wiki(ctx *context.Context) {
} }
ctx.Data["Author"] = lastCommit.Author ctx.Data["Author"] = lastCommit.Author
ctx.Data["OpenGraphTitle"] = ctx.Data["Title"]
ctx.Data["OpenGraphURL"] = fmt.Sprintf("%s%s", setting.AppURL, ctx.Data["Link"])
ctx.HTML(http.StatusOK, tplWikiView) ctx.HTML(http.StatusOK, tplWikiView)
} }

View file

@ -63,6 +63,12 @@ func userProfile(ctx *context.Context) {
ctx.Data["Title"] = ctx.ContextUser.DisplayName() ctx.Data["Title"] = ctx.ContextUser.DisplayName()
ctx.Data["PageIsUserProfile"] = true ctx.Data["PageIsUserProfile"] = true
ctx.Data["OpenGraphTitle"] = ctx.ContextUser.DisplayName()
ctx.Data["OpenGraphType"] = "profile"
ctx.Data["OpenGraphImageURL"] = ctx.ContextUser.AvatarLink(ctx)
ctx.Data["OpenGraphURL"] = ctx.ContextUser.HTMLURL()
ctx.Data["OpenGraphDescription"] = ctx.ContextUser.Description
// prepare heatmap data // prepare heatmap data
if setting.Service.EnableUserHeatmap { if setting.Service.EnableUserHeatmap {
data, err := activities_model.GetUserHeatmapDataByUser(ctx, ctx.ContextUser, ctx.Doer) data, err := activities_model.GetUserHeatmapDataByUser(ctx, ctx.ContextUser, ctx.Doer)

View file

@ -634,6 +634,10 @@ func RepoAssignment(ctx *Context) context.CancelFunc {
} }
cardWidth, cardHeight := card.DefaultSize() cardWidth, cardHeight := card.DefaultSize()
ctx.Data["OpenGraphTitle"] = repo.Name
ctx.Data["OpenGraphURL"] = repo.HTMLURL()
ctx.Data["OpenGraphType"] = "object"
ctx.Data["OpenGraphDescription"] = repo.Description
ctx.Data["OpenGraphImageURL"] = repo.SummaryCardURL() ctx.Data["OpenGraphImageURL"] = repo.SummaryCardURL()
ctx.Data["OpenGraphImageWidth"] = cardWidth ctx.Data["OpenGraphImageWidth"] = cardWidth
ctx.Data["OpenGraphImageHeight"] = cardHeight ctx.Data["OpenGraphImageHeight"] = cardHeight

View file

@ -1,12 +1,24 @@
{{- /* og:description - a one to two sentence description of your object, maybe it only needs at most 300 bytes */ -}} {{- /* See https://ogp.me for specification */ -}}
{{if .OpenGraphTitle}} {{if .OpenGraphTitle}}
<meta property="og:title" content="{{.OpenGraphTitle}}"> <meta property="og:title" content="{{.OpenGraphTitle}}">
{{else if .Title}}
<meta property="og:title" content="{{.Title}}">
{{else}}
<meta property="og:title" content="{{AppDisplayName}}">
{{end}} {{end}}
{{if .OpenGraphDescription}} {{- /* og:description - a one to two sentence description of your object, maybe it only needs at most 300 bytes */ -}}
<meta property="og:description" content="{{.OpenGraphDescription}}"> {{if and .OpenGraphDescription (not .OpenGraphNoDescription)}}
<meta property="og:description" content="{{StringUtils.EllipsisString .OpenGraphDescription 300}}">
{{end}} {{end}}
{{if .OpenGraphURL}} {{if .OpenGraphURL}}
<meta property="og:url" content="{{.OpenGraphURL}}"> <meta property="og:url" content="{{.OpenGraphURL}}">
{{else}}
<meta property="og:url" content="{{AppUrl}}{{.Link}}">
{{end}}
{{if .OpenGraphType}}
<meta property="og:type" content="{{.OpenGraphType}}">
{{else}}
<meta property="og:type" content="website">
{{end}} {{end}}
{{if .OpenGraphImageURL}} {{if .OpenGraphImageURL}}
<meta property="og:image" content="{{.OpenGraphImageURL}}"> <meta property="og:image" content="{{.OpenGraphImageURL}}">
@ -19,62 +31,7 @@
{{if .OpenGraphImageAltText}} {{if .OpenGraphImageAltText}}
<meta property="og:image:alt" content="{{.OpenGraphImageAltText}}"> <meta property="og:image:alt" content="{{.OpenGraphImageAltText}}">
{{end}} {{end}}
{{end}}
{{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="{{StringUtils.EllipsisString .ContextUser.Description 300}}">
{{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="{{StringUtils.EllipsisString .Issue.Content 300}}">
{{end}}
{{else if or .PageIsDiff .IsViewFile}}
<meta property="og:title" content="{{.Title}}">
<meta property="og:url" content="{{AppUrl}}{{.Link}}">
{{if and .PageIsDiff .Commit}}
{{- $commitMessageParts := StringUtils.Cut .Commit.Message "\n" -}}
{{- $commitMessageBody := index $commitMessageParts 1 -}}
{{- if $commitMessageBody -}}
<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}}
{{if not .OpenGraphTitle}}
<meta property="og:title" content="{{.Repository.Name}}">
{{end}}
{{if not .OpenGraphURL}}
<meta property="og:url" content="{{.Repository.HTMLURL}}">
{{end}}
{{if and (.Repository.Description) (not .OpenGraphDescription)}}
<meta property="og:description" content="{{StringUtils.EllipsisString .Repository.Description 300}}">
{{end}}
{{end}}
<meta property="og:type" content="object">
{{if and (not .Issue) (not .OpenGraphImageURL)}}
{{if (.Repository.AvatarLink ctx)}}
<meta property="og:image" content="{{.Repository.AvatarLink ctx}}">
{{else}}
<meta property="og:image" content="{{.Repository.Owner.AvatarLink ctx}}">
{{end}}
{{end}}
{{else}} {{else}}
<meta property="og:title" content="{{AppDisplayName}}"> <meta property="og:image" content="{{AssetUrlPrefix}}/img/logo.png">
<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}} {{end}}
<meta property="og:site_name" content="{{AppDisplayName}}"> <meta property="og:site_name" content="{{AppDisplayName}}">