diff options
author | 6543 <6543@obermui.de> | 2021-06-29 16:28:38 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-29 16:28:38 +0200 |
commit | 65548359cc5c78455b638c5be6fdec3e321e717a (patch) | |
tree | 0236a4aaa8e0c2a781e0383a39a667cf03528a1e /modules | |
parent | aac663e0da0af644ae1011d268d027160265dce3 (diff) | |
download | gitea-65548359cc5c78455b638c5be6fdec3e321e717a.tar.gz gitea-65548359cc5c78455b638c5be6fdec3e321e717a.zip |
Add custom emoji support (#16004)
Diffstat (limited to 'modules')
-rw-r--r-- | modules/markup/html.go | 23 | ||||
-rw-r--r-- | modules/markup/html_test.go | 13 | ||||
-rw-r--r-- | modules/setting/setting.go | 10 | ||||
-rw-r--r-- | modules/structs/settings.go | 1 | ||||
-rw-r--r-- | modules/templates/helper.go | 3 |
5 files changed, 33 insertions, 17 deletions
diff --git a/modules/markup/html.go b/modules/markup/html.go index 0cc0e23b5c..1e55629ab5 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -6,7 +6,6 @@ package markup import ( "bytes" - "fmt" "io" "io/ioutil" "net/url" @@ -66,7 +65,7 @@ var ( blackfridayExtRegex = regexp.MustCompile(`[^:]*:user-content-`) // EmojiShortCodeRegex find emoji by alias like :smile: - EmojiShortCodeRegex = regexp.MustCompile(`\:[\w\+\-]+\:{1}`) + EmojiShortCodeRegex = regexp.MustCompile(`:[\w\+\-]+:`) ) // CSS class for action keywords (e.g. "closes: #1") @@ -460,17 +459,14 @@ func createEmoji(content, class, name string) *html.Node { return span } -func createCustomEmoji(alias, class string) *html.Node { - +func createCustomEmoji(alias string) *html.Node { span := &html.Node{ Type: html.ElementNode, Data: atom.Span.String(), Attr: []html.Attribute{}, } - if class != "" { - span.Attr = append(span.Attr, html.Attribute{Key: "class", Val: class}) - span.Attr = append(span.Attr, html.Attribute{Key: "aria-label", Val: alias}) - } + span.Attr = append(span.Attr, html.Attribute{Key: "class", Val: "emoji"}) + span.Attr = append(span.Attr, html.Attribute{Key: "aria-label", Val: alias}) img := &html.Node{ Type: html.ElementNode, @@ -478,10 +474,8 @@ func createCustomEmoji(alias, class string) *html.Node { Data: "img", Attr: []html.Attribute{}, } - if class != "" { - img.Attr = append(img.Attr, html.Attribute{Key: "alt", Val: fmt.Sprintf(`:%s:`, alias)}) - img.Attr = append(img.Attr, html.Attribute{Key: "src", Val: fmt.Sprintf(`%s/assets/img/emoji/%s.png`, setting.StaticURLPrefix, alias)}) - } + img.Attr = append(img.Attr, html.Attribute{Key: "alt", Val: ":" + alias + ":"}) + img.Attr = append(img.Attr, html.Attribute{Key: "src", Val: setting.StaticURLPrefix + "/assets/img/emoji/" + alias + ".png"}) span.AppendChild(img) return span @@ -948,9 +942,8 @@ func emojiShortCodeProcessor(ctx *RenderContext, node *html.Node) { converted := emoji.FromAlias(alias) if converted == nil { // check if this is a custom reaction - s := strings.Join(setting.UI.Reactions, " ") + "gitea" - if strings.Contains(s, alias) { - replaceContent(node, m[0], m[1], createCustomEmoji(alias, "emoji")) + if _, exist := setting.UI.CustomEmojisMap[alias]; exist { + replaceContent(node, m[0], m[1], createCustomEmoji(alias)) node = node.NextSibling.NextSibling start = 0 continue diff --git a/modules/markup/html_test.go b/modules/markup/html_test.go index 8c3d2b5395..85418892ef 100644 --- a/modules/markup/html_test.go +++ b/modules/markup/html_test.go @@ -284,7 +284,18 @@ func TestRender_emoji(t *testing.T) { test( ":gitea:", `<p><span class="emoji" aria-label="gitea"><img alt=":gitea:" src="`+setting.StaticURLPrefix+`/assets/img/emoji/gitea.png"/></span></p>`) - + test( + ":custom-emoji:", + `<p>:custom-emoji:</p>`) + setting.UI.CustomEmojisMap["custom-emoji"] = ":custom-emoji:" + test( + ":custom-emoji:", + `<p><span class="emoji" aria-label="custom-emoji"><img alt=":custom-emoji:" src="`+setting.StaticURLPrefix+`/assets/img/emoji/custom-emoji.png"/></span></p>`) + test( + "θΏζ―ε符:1::+1: someπ \U0001f44d:custom-emoji: :gitea:", + `<p>θΏζ―ε符:1:<span class="emoji" aria-label="thumbs up">π</span> some<span class="emoji" aria-label="crocodile">π</span> `+ + `<span class="emoji" aria-label="thumbs up">π</span><span class="emoji" aria-label="custom-emoji"><img alt=":custom-emoji:" src="`+setting.StaticURLPrefix+`/assets/img/emoji/custom-emoji.png"/></span> `+ + `<span class="emoji" aria-label="gitea"><img alt=":gitea:" src="`+setting.StaticURLPrefix+`/assets/img/emoji/gitea.png"/></span></p>`) test( "Some text with π in the middle", `<p>Some text with <span class="emoji" aria-label="grinning face with smiling eyes">π</span> in the middle</p>`) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index de167e288a..e37b788342 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -208,7 +208,9 @@ var ( DefaultTheme string Themes []string Reactions []string - ReactionsMap map[string]bool + ReactionsMap map[string]bool `ini:"-"` + CustomEmojis []string + CustomEmojisMap map[string]string `ini:"-"` SearchRepoDescription bool UseServiceWorker bool @@ -256,6 +258,8 @@ var ( DefaultTheme: `gitea`, Themes: []string{`gitea`, `arc-green`}, Reactions: []string{`+1`, `-1`, `laugh`, `hooray`, `confused`, `heart`, `rocket`, `eyes`}, + CustomEmojis: []string{`gitea`}, + CustomEmojisMap: map[string]string{"gitea": ":gitea:"}, Notification: struct { MinTimeout time.Duration TimeoutStep time.Duration @@ -983,6 +987,10 @@ func NewContext() { for _, reaction := range UI.Reactions { UI.ReactionsMap[reaction] = true } + UI.CustomEmojisMap = make(map[string]string) + for _, emoji := range UI.CustomEmojis { + UI.CustomEmojisMap[emoji] = ":" + emoji + ":" + } } func parseAuthorizedPrincipalsAllow(values []string) ([]string, bool) { diff --git a/modules/structs/settings.go b/modules/structs/settings.go index 842b12792d..90c4a2107b 100644 --- a/modules/structs/settings.go +++ b/modules/structs/settings.go @@ -18,6 +18,7 @@ type GeneralRepoSettings struct { type GeneralUISettings struct { DefaultTheme string `json:"default_theme"` AllowedReactions []string `json:"allowed_reactions"` + CustomEmojis []string `json:"custom_emojis"` } // GeneralAPISettings contains global api settings exposed by it diff --git a/modules/templates/helper.go b/modules/templates/helper.go index 83359a6ef2..f9b2dafd22 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -90,6 +90,9 @@ func NewFuncMap() []template.FuncMap { "AllowedReactions": func() []string { return setting.UI.Reactions }, + "CustomEmojis": func() map[string]string { + return setting.UI.CustomEmojisMap + }, "Safe": Safe, "SafeJS": SafeJS, "JSEscape": JSEscape, |