From e99bf7f16d4503c4d2f40b11ce69ca062a208e93 Mon Sep 17 00:00:00 2001 From: JakobDev Date: Wed, 1 Jan 2025 20:43:20 +0100 Subject: [PATCH] Rewrite OpenGraph Header --- routers/web/home.go | 3 ++ routers/web/org/home.go | 6 +++ routers/web/repo/commit.go | 4 ++ routers/web/repo/issue.go | 3 ++ routers/web/repo/view.go | 4 ++ routers/web/repo/wiki.go | 3 ++ routers/web/user/profile.go | 6 +++ services/context/repo.go | 4 ++ templates/base/head_opengraph.tmpl | 75 +++++++----------------------- 9 files changed, 49 insertions(+), 59 deletions(-) diff --git a/routers/web/home.go b/routers/web/home.go index d4be0931e8..4ea961c055 100644 --- a/routers/web/home.go +++ b/routers/web/home.go @@ -61,6 +61,9 @@ func Home(ctx *context.Context) { ctx.Data["PageIsHome"] = true ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled + + ctx.Data["OpenGraphDescription"] = setting.UI.Meta.Description + ctx.HTML(http.StatusOK, tplHome) } diff --git a/routers/web/org/home.go b/routers/web/org/home.go index 9ebefa334c..1b58d8fde9 100644 --- a/routers/web/org/home.go +++ b/routers/web/org/home.go @@ -47,6 +47,12 @@ func Home(ctx *context.Context) { ctx.Data["PageIsUserProfile"] = true 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 sortOrder := ctx.FormString("sort") if _, ok := repo_model.OrderByFlatMap[sortOrder]; !ok { diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index a06da71429..2feb898224 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -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) } diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index 779cbd4b6a..b537afdab5 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -2070,6 +2070,9 @@ func ViewIssue(ctx *context.Context) { ctx.Data["RefEndName"] = git.RefName(issue.Ref).ShortName() ctx.Data["NewPinAllowed"] = pinAllowed 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["OpenGraphImageAltText"] = ctx.Tr("repo.issues.summary_card_alt", issue.Title, issue.Repo.FullName()) diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index fd8c1da058..9030b03a90 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -394,6 +394,10 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry) { ctx.Data["FileName"] = blob.Name() 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() { _, link, err := entry.FollowLinks() // Errors should be allowed, because this shouldn't diff --git a/routers/web/repo/wiki.go b/routers/web/repo/wiki.go index 3d55afe294..070d07cdf3 100644 --- a/routers/web/repo/wiki.go +++ b/routers/web/repo/wiki.go @@ -535,6 +535,9 @@ func Wiki(ctx *context.Context) { } 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) } diff --git a/routers/web/user/profile.go b/routers/web/user/profile.go index 9cb392d878..de1c6850aa 100644 --- a/routers/web/user/profile.go +++ b/routers/web/user/profile.go @@ -63,6 +63,12 @@ func userProfile(ctx *context.Context) { ctx.Data["Title"] = ctx.ContextUser.DisplayName() 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 if setting.Service.EnableUserHeatmap { data, err := activities_model.GetUserHeatmapDataByUser(ctx, ctx.ContextUser, ctx.Doer) diff --git a/services/context/repo.go b/services/context/repo.go index 462d843bfc..d294c00455 100644 --- a/services/context/repo.go +++ b/services/context/repo.go @@ -634,6 +634,10 @@ func RepoAssignment(ctx *Context) context.CancelFunc { } 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["OpenGraphImageWidth"] = cardWidth ctx.Data["OpenGraphImageHeight"] = cardHeight diff --git a/templates/base/head_opengraph.tmpl b/templates/base/head_opengraph.tmpl index 692f1797b6..7f6eae3f49 100644 --- a/templates/base/head_opengraph.tmpl +++ b/templates/base/head_opengraph.tmpl @@ -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}} +{{else if .Title}} + +{{else}} + {{end}} -{{if .OpenGraphDescription}} - +{{- /* og:description - a one to two sentence description of your object, maybe it only needs at most 300 bytes */ -}} +{{if and .OpenGraphDescription (not .OpenGraphNoDescription)}} + {{end}} {{if .OpenGraphURL}} +{{else}} + +{{end}} +{{if .OpenGraphType}} + +{{else}} + {{end}} {{if .OpenGraphImageURL}} @@ -19,62 +31,7 @@ {{if .OpenGraphImageAltText}} {{end}} -{{end}} -{{if .PageIsUserProfile}} - - - - - {{if .ContextUser.Description}} - - {{end}} -{{else if .Repository}} - {{if .Issue}} - - - {{if .Issue.Content}} - - {{end}} - {{else if or .PageIsDiff .IsViewFile}} - - - {{if and .PageIsDiff .Commit}} - {{- $commitMessageParts := StringUtils.Cut .Commit.Message "\n" -}} - {{- $commitMessageBody := index $commitMessageParts 1 -}} - {{- if $commitMessageBody -}} - - {{- end -}} - {{end}} - {{else if .Pages}} - - - {{if .Repository.Description}} - - {{end}} - {{else}} - {{if not .OpenGraphTitle}} - - {{end}} - {{if not .OpenGraphURL}} - - {{end}} - {{if and (.Repository.Description) (not .OpenGraphDescription)}} - - {{end}} - {{end}} - - {{if and (not .Issue) (not .OpenGraphImageURL)}} - {{if (.Repository.AvatarLink ctx)}} - - {{else}} - - {{end}} - {{end}} {{else}} - - - - - + {{end}}