Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor og:description to limit the max length #26876

Merged
merged 2 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions modules/templates/util_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func (su *StringUtils) Join(a []string, sep string) string {
return strings.Join(a, sep)
}

func (su *StringUtils) Cut(s, sep string) []any {
before, after, found := strings.Cut(s, sep)
return []any{before, after, found}
}

func (su *StringUtils) EllipsisString(s string, max int) string {
return base.EllipsisString(s, max)
}
15 changes: 10 additions & 5 deletions templates/base/head_opengraph.tmpl
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
{{- /* og:description - a one to two sentence description of your object, maybe it only needs at most 300 bytes */ -}}
{{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}}">
<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="{{.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 (IsMultilineCommitMessage .Commit.Message)}}
<meta property="og:description" content="{{RenderCommitBody $.Context .Commit.Message $.RepoLink $.Repository.ComposeMetas}}">
{{if .PageIsDiff}}
{{- $commitMessageParts := StringUtils.Cut .Commit.Message "\n" -}}
{{- $commitMessageBody := index $commitMessageParts 1 -}}
{{- if $commitMessageBody -}}
Comment on lines +21 to +23
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why whitespace trimming?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why output too many spaces into the HTML code?

<meta property="og:description" content="{{StringUtils.EllipsisString $commitMessageBody 300}}">
{{- end -}}
{{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}}">
<meta property="og:description" content="{{StringUtils.EllipsisString .Repository.Description 300}}">
{{end}}
{{end}}
<meta property="og:type" content="object">
Expand Down