chore: simplify storing and sending custom emoji's

- Using a map to store the CustomEmojis is unncessary, the values can be
derived by the key easily. We still need to know if a certain name is
custom emoji, so store the custom emojis in a `Set`.
- Instead of constructing a map in `window.config`, construct a `Set`
and reconstruct the value in `emoji.js`. This has the main benefit of
reducing the amount of text being sent for each request, which is quite
noticable if a Forgejo instance has many (>1000) custom emojis.
- Remove the default value of `CustomEmojisMap`, it will be constructed anyway.
This commit is contained in:
Gusted 2025-08-10 21:36:11 +02:00
commit da635229bf
No known key found for this signature in database
GPG key ID: FD821B732837125F
6 changed files with 10 additions and 13 deletions

View file

@ -1142,7 +1142,7 @@ func emojiShortCodeProcessor(ctx *RenderContext, node *html.Node) {
converted := emoji.FromAlias(alias)
if converted == nil {
// check if this is a custom reaction
if _, exist := setting.UI.CustomEmojisMap[alias]; exist {
if setting.UI.CustomEmojisLookup.Contains(alias) {
replaceContent(node, m[0], m[1], createCustomEmoji(alias))
node = node.NextSibling.NextSibling
start = 0

View file

@ -358,7 +358,7 @@ func TestRender_emoji(t *testing.T) {
test(
":custom-emoji:",
`<p>:custom-emoji:</p>`)
setting.UI.CustomEmojisMap["custom-emoji"] = ":custom-emoji:"
setting.UI.CustomEmojisLookup.Add("custom-emoji")
test(
":custom-emoji:",
`<p><span class="emoji" aria-label="custom-emoji" data-alias="custom-emoji"><img alt=":custom-emoji:" src="`+setting.StaticURLPrefix+`/assets/img/emoji/custom-emoji.png"/></span></p>`)

View file

@ -31,7 +31,7 @@ var UI = struct {
Reactions []string
ReactionsLookup container.Set[string] `ini:"-"`
CustomEmojis []string
CustomEmojisMap map[string]string `ini:"-"`
CustomEmojisLookup container.Set[string] `ini:"-"`
SearchRepoDescription bool
OnlyShowRelevantRepos bool
ExploreDefaultSort string `ini:"EXPLORE_PAGING_DEFAULT_SORT"`
@ -87,7 +87,6 @@ var UI = struct {
Themes: []string{`forgejo-auto`, `forgejo-light`, `forgejo-dark`, `gitea-auto`, `gitea-light`, `gitea-dark`, `forgejo-auto-deuteranopia-protanopia`, `forgejo-light-deuteranopia-protanopia`, `forgejo-dark-deuteranopia-protanopia`, `forgejo-auto-tritanopia`, `forgejo-light-tritanopia`, `forgejo-dark-tritanopia`},
Reactions: []string{`+1`, `-1`, `laugh`, `hooray`, `confused`, `heart`, `rocket`, `eyes`},
CustomEmojis: []string{`git`, `gitea`, `codeberg`, `gitlab`, `github`, `gogs`, `forgejo`},
CustomEmojisMap: map[string]string{"git": ":git:", "gitea": ":gitea:", "codeberg": ":codeberg:", "gitlab": ":gitlab:", "github": ":github:", "gogs": ":gogs:", "forgejo": ":forgejo:"},
ExploreDefaultSort: "recentupdate",
PreferredTimestampTense: "mixed",
@ -163,8 +162,6 @@ func loadUIFrom(rootCfg ConfigProvider) {
for _, reaction := range UI.Reactions {
UI.ReactionsLookup.Add(reaction)
}
UI.CustomEmojisMap = make(map[string]string)
for _, emoji := range UI.CustomEmojis {
UI.CustomEmojisMap[emoji] = ":" + emoji + ":"
}
UI.CustomEmojisLookup = make(container.Set[string])
UI.CustomEmojisLookup.AddMultiple(UI.CustomEmojis...)
}

View file

@ -128,8 +128,8 @@ func NewFuncMap() template.FuncMap {
"AllowedReactions": func() []string {
return setting.UI.Reactions
},
"CustomEmojis": func() map[string]string {
return setting.UI.CustomEmojisMap
"CustomEmojis": func() []string {
return setting.UI.CustomEmojis
},
"MetaAuthor": func() string {
return setting.UI.Meta.Author

View file

@ -13,7 +13,7 @@ If you introduce mistakes in it, Gitea JavaScript code wouldn't run correctly.
assetVersionEncoded: encodeURIComponent('{{AssetVersion}}'), // will be used in URL construction directly
assetUrlPrefix: '{{AssetUrlPrefix}}',
runModeIsProd: {{.RunModeIsProd}},
customEmojis: {{CustomEmojis}},
customEmojis: new Set({{CustomEmojis}}),
csrfToken: '{{.CsrfToken}}',
pageData: {{.PageData}},
notificationSettings: {{NotificationSettings}}, {{/*a map provided by NewFuncMap in helper.go*/}}

View file

@ -2,7 +2,7 @@ import emojis from '../../../assets/emoji.json';
const {assetUrlPrefix, customEmojis} = window.config;
const tempMap = {...customEmojis};
const tempMap = Object.assign(...Array.from(customEmojis, (v) => ({[v]: `:${v}:`})));
for (const {emoji, aliases} of emojis) {
for (const alias of aliases || []) {
tempMap[alias] = emoji;
@ -23,7 +23,7 @@ for (const key of emojiKeys) {
// retrieve HTML for given emoji name
export function emojiHTML(name) {
let inner;
if (Object.hasOwn(customEmojis, name)) {
if (customEmojis.has(name)) {
inner = `<img alt=":${name}:" src="${assetUrlPrefix}/img/emoji/${name}.png">`;
} else {
inner = emojiString(name);