Skip to content

Commit

Permalink
Use more performance emojify code (thanks NyaaaWhatsUpDoc!)
Browse files Browse the repository at this point in the history
  • Loading branch information
blackle committed Sep 1, 2022
1 parent 1ed3cca commit 06d1c04
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions internal/router/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package router
import (
"bytes"
"fmt"
"html"
"html/template"
"os"
"path/filepath"
Expand Down Expand Up @@ -107,29 +108,42 @@ func visibilityIcon(visibility model.Visibility) template.HTML {
// replaces shortcodes in `text` with the emoji in `emojis`
// text is a template.HTML to affirm that the input of this function is already escaped
func emojify(emojis []model.Emoji, text template.HTML) template.HTML {
emojisMap := make(map[string]model.Emoji)
emojisMap := make(map[string]model.Emoji, len(emojis))

for _, emoji := range emojis {
shortcode := ":" + emoji.Shortcode + ":"
emojisMap[shortcode] = emoji
}

emojiTemplate, _ := template.New("emojiTemplate").Parse(`{{define "Emoji"}}<img src="{{.URL}}" title=":{{.Shortcode}}:" alt=":{{.Shortcode}}:" class="emoji"/>{{end}}`)

emojifyer := func(shortcode string) string {
emoji, ok := emojisMap[shortcode]
if ok == false {
return shortcode
}
var buff bytes.Buffer
err := emojiTemplate.ExecuteTemplate(&buff, "Emoji", emoji)
if err != nil {
return shortcode
}
return buff.String()
}
out := regexes.ReplaceAllStringFunc(
regexes.EmojiFinder,
string(text),
func(shortcode string, buf *bytes.Buffer) string {
// Look for emoji according to this shortcode
emoji, ok := emojisMap[shortcode]
if !ok {
return shortcode
}

// Escape raw emoji content
safeURL := html.EscapeString(emoji.URL)
safeCode := html.EscapeString(emoji.Shortcode)

// Write HTML emoji repr to buffer
buf.WriteString(`<img src="`)
buf.WriteString(safeURL)
buf.WriteString(`" title=":`)
buf.WriteString(safeCode)
buf.WriteString(`:" alt=":`)
buf.WriteString(safeCode)
buf.WriteString(`:" class="emoji"/>`)

return buf.String()
},
)

out := regexes.EmojiFinder.ReplaceAllStringFunc(string(text), emojifyer)
/* #nosec G203 */
// (this is escaped above)
return template.HTML(out)
}

Expand Down

0 comments on commit 06d1c04

Please sign in to comment.