From c802c46a9beeaed44d41f50de31a4db146cdd8f7 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sun, 3 Sep 2023 09:43:29 +0800 Subject: [PATCH] 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 --- modules/templates/util_string.go | 5 +++++ templates/base/head_opengraph.tmpl | 15 ++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/modules/templates/util_string.go b/modules/templates/util_string.go index 459380aee..18a0d5cac 100644 --- a/modules/templates/util_string.go +++ b/modules/templates/util_string.go @@ -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) } diff --git a/templates/base/head_opengraph.tmpl b/templates/base/head_opengraph.tmpl index fc3958b6b..19d924610 100644 --- a/templates/base/head_opengraph.tmpl +++ b/templates/base/head_opengraph.tmpl @@ -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 .ContextUser.Description}} - + {{end}} {{else if .Repository}} {{if .Issue}} {{if .Issue.Content}} - + {{end}} {{else if or .PageIsDiff .IsViewFile}} - {{if and .PageIsDiff (IsMultilineCommitMessage .Commit.Message)}} - + {{if .PageIsDiff}} + {{- $commitMessageParts := StringUtils.Cut .Commit.Message "\n" -}} + {{- $commitMessageBody := index $commitMessageParts 1 -}} + {{- if $commitMessageBody -}} + + {{- end -}} {{end}} {{else}} {{if .Repository.Description}} - + {{end}} {{end}}