Skip to content

Update emoji regex #11584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions modules/emoji/emoji.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package emoji

import (
"regexp"
"strings"
"sync"
)
Expand Down Expand Up @@ -35,6 +36,15 @@ var (
aliasReplacer *strings.Replacer

once sync.Once

// codePoints used for building regex string more efficiently
codePoints strings.Builder

// RegexpStr to store string of emoji for matching
regexpStr string

// EmojiUnicodeRegex to match any emoji present in emoji_data.go exactly
emojiUnicodeRegex *regexp.Regexp
)

func loadMap() {
Expand All @@ -48,6 +58,7 @@ func loadMap() {
// process emoji codes and aliases
codePairs := make([]string, 0)
aliasPairs := make([]string, 0)

for i, e := range GemojiData {
if e.Emoji == "" || len(e.Aliases) == 0 {
continue
Expand All @@ -57,6 +68,9 @@ func loadMap() {
codeMap[e.Emoji] = i
codePairs = append(codePairs, e.Emoji, ":"+e.Aliases[0]+":")

//used to build "or" type regex string with each emoji we support
codePoints.WriteString(e.Emoji + "|")

// setup aliases
for _, a := range e.Aliases {
if a == "" {
Expand All @@ -71,7 +85,18 @@ func loadMap() {
// create replacers
codeReplacer = strings.NewReplacer(codePairs...)
aliasReplacer = strings.NewReplacer(aliasPairs...)

//create regex to match all stored unicode values
regexpStr = "[" + codePoints.String() + "]"
emojiUnicodeRegex = regexp.MustCompile(regexpStr)
})

}

// UnicodeRegex returns a regex that matches any individual emoji in emoji_data.go
func UnicodeRegex() *regexp.Regexp {
loadMap()
return emojiUnicodeRegex
}

// FromCode retrieves the emoji data based on the provided unicode code (ie,
Expand Down
7 changes: 1 addition & 6 deletions modules/markup/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ var (

// EmojiShortCodeRegex find emoji by alias like :smile:
EmojiShortCodeRegex = regexp.MustCompile(`\:[\w\+\-]+\:{1}`)

// find emoji literal: search all emoji hex range as many times as they appear as
// some emojis (skin color etc..) are just two or more chained together
emojiRegex = regexp.MustCompile(`[\x{1F000}-\x{1FFFF}|\x{2000}-\x{32ff}|\x{fe4e5}-\x{fe4ee}|\x{200D}|\x{FE0F}|\x{e0000}-\x{e007f}]+`)
)

// CSS class for action keywords (e.g. "closes: #1")
Expand Down Expand Up @@ -922,8 +918,7 @@ func emojiShortCodeProcessor(ctx *postProcessCtx, node *html.Node) {

// emoji processor to match emoji and add emoji class
func emojiProcessor(ctx *postProcessCtx, node *html.Node) {
m := emojiRegex.FindStringSubmatchIndex(node.Data)

m := emoji.UnicodeRegex().FindStringSubmatchIndex(node.Data)
if m == nil {
return
}
Expand Down