Skip to content

Commit

Permalink
keepDescription: initialize bluemonday policy only once
Browse files Browse the repository at this point in the history
The policy can be reused, thus there's no need to recreate it for every
processed calendar event.
  • Loading branch information
MichaelEischer committed Oct 10, 2024
1 parent ccdebc5 commit 8e91e91
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions internal/transformation/keepDescription.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,31 @@ package transformation

import (
"strings"
"sync"

"github.com/aquilax/truncate"
"github.com/inovex/CalendarSync/internal/models"
"github.com/microcosm-cc/bluemonday"
)

// 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)
Expand Down

0 comments on commit 8e91e91

Please sign in to comment.