From 8e91e917754e0ba7802c82bd95967ef232ebfe7e Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Thu, 10 Oct 2024 19:14:50 +0200 Subject: [PATCH] keepDescription: initialize bluemonday policy only once The policy can be reused, thus there's no need to recreate it for every processed calendar event. --- internal/transformation/keepDescription.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/internal/transformation/keepDescription.go b/internal/transformation/keepDescription.go index 75b61fc..dce90c0 100644 --- a/internal/transformation/keepDescription.go +++ b/internal/transformation/keepDescription.go @@ -2,6 +2,7 @@ package transformation import ( "strings" + "sync" "github.com/aquilax/truncate" "github.com/inovex/CalendarSync/internal/models" @@ -9,17 +10,23 @@ import ( ) // KeepDescription allows to keep the description of an event. -type KeepDescription struct{} +type KeepDescription struct { + policy *bluemonday.Policy + initPolicy sync.Once +} func (t *KeepDescription) Name() string { return "KeepDescription" } func (t *KeepDescription) Transform(source models.Event, sink models.Event) (models.Event, error) { + t.initPolicy.Do(func() { + t.policy = bluemonday.UGCPolicy() + }) + // need to remove microsoft html overhead (the description in outlook contains a lot of '\r\n's) - p := bluemonday.UGCPolicy() description := strings.ReplaceAll(source.Description, "\r\n", "") - sanitizedDescription := p.Sanitize(description) + sanitizedDescription := t.policy.Sanitize(description) sanitizedDescription2 := strings.TrimSpace(sanitizedDescription) // Since the description cannot exceed a specified amount in some sinks (e.g. google)