Skip to content

Commit c802c46

Browse files
authored
Refactor og:description to limit the max length (#26876)
1. The `og:description` should be "a one to two sentence description of your object" * It shouldn't output all the user inputted content -- it would be pretty huge. * Maybe it only needs at most 300 bytes. 2. Do not render commit message as HTML
1 parent 9a3de43 commit c802c46

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

modules/templates/util_string.go

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ func (su *StringUtils) Join(a []string, sep string) string {
3333
return strings.Join(a, sep)
3434
}
3535

36+
func (su *StringUtils) Cut(s, sep string) []any {
37+
before, after, found := strings.Cut(s, sep)
38+
return []any{before, after, found}
39+
}
40+
3641
func (su *StringUtils) EllipsisString(s string, max int) string {
3742
return base.EllipsisString(s, max)
3843
}

templates/base/head_opengraph.tmpl

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
1+
{{- /* og:description - a one to two sentence description of your object, maybe it only needs at most 300 bytes */ -}}
12
{{if .PageIsUserProfile}}
23
<meta property="og:title" content="{{.ContextUser.DisplayName}}">
34
<meta property="og:type" content="profile">
45
<meta property="og:image" content="{{.ContextUser.AvatarLink ctx}}">
56
<meta property="og:url" content="{{.ContextUser.HTMLURL}}">
67
{{if .ContextUser.Description}}
7-
<meta property="og:description" content="{{.ContextUser.Description}}">
8+
<meta property="og:description" content="{{StringUtils.EllipsisString .ContextUser.Description 300}}">
89
{{end}}
910
{{else if .Repository}}
1011
{{if .Issue}}
1112
<meta property="og:title" content="{{.Issue.Title}}">
1213
<meta property="og:url" content="{{.Issue.HTMLURL}}">
1314
{{if .Issue.Content}}
14-
<meta property="og:description" content="{{.Issue.Content}}">
15+
<meta property="og:description" content="{{StringUtils.EllipsisString .Issue.Content 300}}">
1516
{{end}}
1617
{{else if or .PageIsDiff .IsViewFile}}
1718
<meta property="og:title" content="{{.Title}}">
1819
<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}}">
20+
{{if .PageIsDiff}}
21+
{{- $commitMessageParts := StringUtils.Cut .Commit.Message "\n" -}}
22+
{{- $commitMessageBody := index $commitMessageParts 1 -}}
23+
{{- if $commitMessageBody -}}
24+
<meta property="og:description" content="{{StringUtils.EllipsisString $commitMessageBody 300}}">
25+
{{- end -}}
2126
{{end}}
2227
{{else}}
2328
<meta property="og:title" content="{{.Repository.Name}}">
2429
<meta property="og:url" content="{{.Repository.HTMLURL}}">
2530
{{if .Repository.Description}}
26-
<meta property="og:description" content="{{.Repository.Description}}">
31+
<meta property="og:description" content="{{StringUtils.EllipsisString .Repository.Description 300}}">
2732
{{end}}
2833
{{end}}
2934
<meta property="og:type" content="object">

0 commit comments

Comments
 (0)