Skip to content

Commit 26f4491

Browse files
Comparison should ignore accents (#10)
1 parent 55f49a5 commit 26f4491

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ require (
2222
go.uber.org/zap v1.8.0 // indirect
2323
golang.org/x/net v0.0.0-20180706051357-32a936f46389 // indirect
2424
golang.org/x/sync v0.0.0-20190423024810-112230192c58 // indirect
25+
golang.org/x/text v0.3.0
2526
google.golang.org/genproto v0.0.0-20180627194029-ff3583edef7d // indirect
2627
google.golang.org/grpc v1.13.0 // indirect
2728
gopkg.in/natefinch/lumberjack.v2 v2.0.0-20170531160350-a96e63847dc3 // indirect

server/plugin.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ package main
33
import (
44
"net/http"
55
"strings"
6+
"unicode"
7+
8+
"golang.org/x/text/runes"
9+
"golang.org/x/text/transform"
10+
"golang.org/x/text/unicode/norm"
611

712
"github.com/mattermost/mattermost-server/model"
813
"github.com/mattermost/mattermost-server/plugin"
@@ -23,7 +28,7 @@ func main() {
2328
func (p *Plugin) OnActivate() error {
2429
p.badWords = make(map[string]bool, len(badWords))
2530
for _, word := range badWords {
26-
p.badWords[strings.ToLower(word)] = true
31+
p.badWords[strings.ToLower(removeAccents(word))] = true
2732
}
2833

2934
return nil
@@ -37,7 +42,7 @@ func (p *Plugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Req
3742
}
3843

3944
func (p *Plugin) WordIsBad(word string) bool {
40-
_, ok := p.badWords[strings.ToLower(word)]
45+
_, ok := p.badWords[strings.ToLower(removeAccents(word))]
4146
return ok
4247
}
4348

@@ -64,3 +69,13 @@ func (p *Plugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*mode
6469
func (p *Plugin) MessageWillBeUpdated(c *plugin.Context, newPost *model.Post, _ *model.Post) (*model.Post, string) {
6570
return p.FilterPost(newPost)
6671
}
72+
73+
func removeAccents(s string) string {
74+
t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
75+
output, _, e := transform.String(t, s)
76+
if e != nil {
77+
return s
78+
}
79+
80+
return output
81+
}

0 commit comments

Comments
 (0)