1
0
mirror of https://github.com/go-gitea/gitea.git synced 2024-12-04 14:46:57 -05:00

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
This commit is contained in:
wxiaoguang 2023-09-03 09:43:29 +08:00 committed by GitHub
parent 9a3de436f4
commit c802c46a9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 5 deletions

View File

@ -33,6 +33,11 @@ func (su *StringUtils) Join(a []string, sep string) string {
return strings.Join(a, sep) 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 { func (su *StringUtils) EllipsisString(s string, max int) string {
return base.EllipsisString(s, max) return base.EllipsisString(s, max)
} }

View File

@ -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}} {{if .PageIsUserProfile}}
<meta property="og:title" content="{{.ContextUser.DisplayName}}"> <meta property="og:title" content="{{.ContextUser.DisplayName}}">
<meta property="og:type" content="profile"> <meta property="og:type" content="profile">
<meta property="og:image" content="{{.ContextUser.AvatarLink ctx}}"> <meta property="og:image" content="{{.ContextUser.AvatarLink ctx}}">
<meta property="og:url" content="{{.ContextUser.HTMLURL}}"> <meta property="og:url" content="{{.ContextUser.HTMLURL}}">
{{if .ContextUser.Description}} {{if .ContextUser.Description}}
<meta property="og:description" content="{{.ContextUser.Description}}"> <meta property="og:description" content="{{StringUtils.EllipsisString .ContextUser.Description 300}}">
{{end}} {{end}}
{{else if .Repository}} {{else if .Repository}}
{{if .Issue}} {{if .Issue}}
<meta property="og:title" content="{{.Issue.Title}}"> <meta property="og:title" content="{{.Issue.Title}}">
<meta property="og:url" content="{{.Issue.HTMLURL}}"> <meta property="og:url" content="{{.Issue.HTMLURL}}">
{{if .Issue.Content}} {{if .Issue.Content}}
<meta property="og:description" content="{{.Issue.Content}}"> <meta property="og:description" content="{{StringUtils.EllipsisString .Issue.Content 300}}">
{{end}} {{end}}
{{else if or .PageIsDiff .IsViewFile}} {{else if or .PageIsDiff .IsViewFile}}
<meta property="og:title" content="{{.Title}}"> <meta property="og:title" content="{{.Title}}">
<meta property="og:url" content="{{AppUrl}}{{.Link}}"> <meta property="og:url" content="{{AppUrl}}{{.Link}}">
{{if and .PageIsDiff (IsMultilineCommitMessage .Commit.Message)}} {{if .PageIsDiff}}
<meta property="og:description" content="{{RenderCommitBody $.Context .Commit.Message $.RepoLink $.Repository.ComposeMetas}}"> {{- $commitMessageParts := StringUtils.Cut .Commit.Message "\n" -}}
{{- $commitMessageBody := index $commitMessageParts 1 -}}
{{- if $commitMessageBody -}}
<meta property="og:description" content="{{StringUtils.EllipsisString $commitMessageBody 300}}">
{{- end -}}
{{end}} {{end}}
{{else}} {{else}}
<meta property="og:title" content="{{.Repository.Name}}"> <meta property="og:title" content="{{.Repository.Name}}">
<meta property="og:url" content="{{.Repository.HTMLURL}}"> <meta property="og:url" content="{{.Repository.HTMLURL}}">
{{if .Repository.Description}} {{if .Repository.Description}}
<meta property="og:description" content="{{.Repository.Description}}"> <meta property="og:description" content="{{StringUtils.EllipsisString .Repository.Description 300}}">
{{end}} {{end}}
{{end}} {{end}}
<meta property="og:type" content="object"> <meta property="og:type" content="object">