Skip to content

Commit 119e16c

Browse files
committed
Add MJML templating support to campaigns.
1 parent cdf0a5c commit 119e16c

File tree

11 files changed

+97
-9
lines changed

11 files changed

+97
-9
lines changed

cmd/campaigns.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,8 @@ func (a *App) validateCampaignFields(c campReq) (campReq, error) {
609609
c.ContentType != models.CampaignContentTypeHTML &&
610610
c.ContentType != models.CampaignContentTypePlain &&
611611
c.ContentType != models.CampaignContentTypeVisual &&
612-
c.ContentType != models.CampaignContentTypeMarkdown {
612+
c.ContentType != models.CampaignContentTypeMarkdown &&
613+
c.ContentType != models.CampaignContentTypeMJML {
613614
c.ContentType = models.CampaignContentTypeRichtext
614615
}
615616

cmd/install.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,15 @@ func installTemplates(q *models.Queries) (int, int) {
235235
lo.Fatalf("error creating default campaign template: %v", err)
236236
}
237237

238+
// Insert MLML template.
239+
tpl, err := fs.Get("/static/email-templates/sample-mjml.tpl")
240+
if err != nil {
241+
lo.Fatalf("error reading sample mjml template: %v", err)
242+
}
243+
if _, err := q.CreateTemplate.Exec("Sample MJML template", models.TemplateTypeCampaign, "", tpl.ReadBytes(), nil); err != nil {
244+
lo.Fatalf("error creating mjml campaign template: %v", err)
245+
}
246+
238247
return campTplID, archiveTplID
239248
}
240249

frontend/src/components/CodeEditor.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ export default {
5050
case 'html':
5151
langs = [html()];
5252
break;
53+
case 'mjml':
54+
langs = [html()];
55+
break;
5356
case 'css':
5457
langs = [css()];
5558
break;

frontend/src/components/Editor.vue

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
<!-- raw html editor //-->
6868
<code-editor lang="html" v-if="self.contentType === 'html'" v-model="self.body" key="editor-html" />
6969

70+
<!-- mjml editor //-->
71+
<code-editor lang="mjml" v-if="self.contentType === 'mjml'" v-model="self.body" key="editor-mjml" />
72+
7073
<!-- markdown editor //-->
7174
<code-editor lang="markdown" v-if="self.contentType === 'markdown'" v-model="self.body" key="editor-markdown" />
7275

@@ -162,7 +165,7 @@ export default {
162165
163166
// If `from` is HTML content, strip out `<body>..` etc. and keep the beautified HTML.
164167
let isHTML = false;
165-
if (from === 'richtext' || from === 'html' || from === 'visual') {
168+
if (from === 'richtext' || from === 'html' || from === 'visual' || from === 'mjml') {
166169
const d = document.createElement('div');
167170
d.innerHTML = body;
168171
body = this.beautifyHTML(d.innerHTML.trim());
@@ -198,7 +201,7 @@ export default {
198201
}
199202
200203
// Markdown to HTML requires a backend call.
201-
} else if (from === 'markdown' && (to === 'richtext' || to === 'html')) {
204+
} else if (from === 'markdown' && (to === 'richtext' || to === 'html' || to === 'mjml')) {
202205
skip = true;
203206
this.$api.convertCampaignContent({
204207
id: 1, body, from, to,
@@ -212,8 +215,21 @@ export default {
212215
});
213216
214217
// Plain to an HTML type, change plain line breaks to HTML breaks.
215-
} else if (from === 'plain' && (to === 'richtext' || to === 'html')) {
218+
} else if (from === 'plain' && (to === 'richtext' || to === 'html' || to === 'mjml')) {
216219
body = body.replace(/\n/ig, '<br>\n');
220+
} else if (from === 'mjml' && (to === 'richtext' || to === 'html')) {
221+
// MJML to HTML requires a backend call.
222+
skip = true;
223+
this.$api.convertCampaignContent({
224+
id: 1, body, from, to,
225+
}).then((data) => {
226+
this.$nextTick(() => {
227+
// Both type + body should be updated in one cycle to avoid firing
228+
// multiple events.
229+
this.self.contentType = to;
230+
this.self.body = this.beautifyHTML(data.trim());
231+
});
232+
});
217233
} else if (to === 'visual') {
218234
bodySource = JSON.stringify(markdownToVisualBlock(body));
219235
}

frontend/src/views/Campaign.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ export default Vue.extend({
340340
markdown: this.$t('campaigns.markdown'),
341341
plain: this.$t('campaigns.plainText'),
342342
visual: this.$t('campaigns.visual'),
343+
mjml: 'MJML',
343344
}),
344345
345346
isNew: false,

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/knadh/listmonk
22

3-
go 1.24.1
3+
go 1.24.4
44

55
require (
66
github.com/Masterminds/sprig/v3 v3.3.0
@@ -29,6 +29,7 @@ require (
2929
github.com/labstack/echo/v4 v4.13.4
3030
github.com/lib/pq v1.10.9
3131
github.com/paulbellamy/ratecounter v0.2.0
32+
github.com/preslavrachev/gomjml v0.10.0
3233
github.com/rhnvrm/simples3 v0.9.1
3334
github.com/spf13/pflag v1.0.6
3435
github.com/yuin/goldmark v1.7.12

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ github.com/Masterminds/semver/v3 v3.4.0 h1:Zog+i5UMtVoCU8oKka5P7i9q9HgrJeGzI9SA1
88
github.com/Masterminds/semver/v3 v3.4.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
99
github.com/Masterminds/sprig/v3 v3.3.0 h1:mQh0Yrg1XPo6vjYXgtf5OtijNAKJRNcTdOOGZe3tPhs=
1010
github.com/Masterminds/sprig/v3 v3.3.0/go.mod h1:Zy1iXRYNqNLUolqCpL4uhk6SHUMAOSCzdgBfDb35Lz0=
11+
github.com/PuerkitoBio/goquery v1.10.3 h1:pFYcNSqHxBD06Fpj/KsbStFRsgRATgnf3LeXiUkhzPo=
12+
github.com/PuerkitoBio/goquery v1.10.3/go.mod h1:tMUX0zDMHXYlAQk6p35XxQMqMweEKB7iK7iLNd4RH4Y=
1113
github.com/altcha-org/altcha-lib-go v0.2.2 h1:KY7a7jFUf6tFKZF6MzuZMhSWuGMv0MtVkK/Kj4Oas38=
1214
github.com/altcha-org/altcha-lib-go v0.2.2/go.mod h1:I8ESLVWR9C58uvGufB/AJDPhaSU4+4Oh3DLpVtgwDAk=
15+
github.com/andybalholm/cascadia v1.3.3 h1:AG2YHrzJIm4BZ19iwJ/DAua6Btl3IwJX+VI4kktS1LM=
16+
github.com/andybalholm/cascadia v1.3.3/go.mod h1:xNd9bqTn98Ln4DwST8/nG+H0yuB8Hmgu1YHNnWw0GeA=
1317
github.com/coreos/go-oidc/v3 v3.14.1 h1:9ePWwfdwC4QKRlCXsJGou56adA/owXczOzwKdOumLqk=
1418
github.com/coreos/go-oidc/v3 v3.14.1/go.mod h1:HaZ3szPaZ0e4r6ebqvsLWlk2Tn+aejfmrfah6hnSYEU=
1519
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -106,6 +110,10 @@ github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3v
106110
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
107111
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
108112
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
113+
github.com/preslavrachev/gomjml v0.5.0 h1:Ca6OxHx7AAK1R3KHx6aRBU6zTex/kezWIp7Z14GrUQM=
114+
github.com/preslavrachev/gomjml v0.5.0/go.mod h1:10tpMJhl+46mqf+5wG18fOXaWNB+OOllCpksDRJlJTU=
115+
github.com/preslavrachev/gomjml v0.10.0 h1:GdcLph92E3aADmBgR6DnDS7NsnK1DsHeYWmDhZGrJEA=
116+
github.com/preslavrachev/gomjml v0.10.0/go.mod h1:10tpMJhl+46mqf+5wG18fOXaWNB+OOllCpksDRJlJTU=
109117
github.com/rhnvrm/simples3 v0.9.1 h1:pYfEe2wTjx8B2zFzUdy4kZn3I3Otd9ZvzIhHkFR85kE=
110118
github.com/rhnvrm/simples3 v0.9.1/go.mod h1:Y+3vYm2V7Y4VijFoJHHTrja6OgPrJ2cBti8dPGkC3sA=
111119
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=

internal/migrations/v5.1.0.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,10 @@ func V5_1_0(db *sqlx.DB, fs stuffbin.FileSystem, ko *koanf.Koanf, lo *log.Logger
5050
return err
5151
}
5252

53+
// Add MJML to content_type enum if not exists
54+
if _, err = db.Exec(`ALTER TYPE content_type ADD VALUE IF NOT EXISTS 'mjml';`); err != nil {
55+
return err
56+
}
57+
5358
return nil
5459
}

internal/migrations/v5.2.0.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,21 @@ func V5_2_0(db *sqlx.DB, fs stuffbin.FileSystem, ko *koanf.Koanf, lo *log.Logger
1616
return err
1717
}
1818

19+
if _, err := db.Exec(`
20+
ALTER TYPE content_type ADD VALUE IF NOT EXISTS 'mjml';
21+
`); err != nil {
22+
return err
23+
}
24+
25+
// Insert MLML template.
26+
tpl, err := fs.Get("/static/email-templates/sample-mjml.tpl")
27+
if err != nil {
28+
return err
29+
}
30+
if _, err := db.Exec(`INSERT INTO templates (name, type, subject, body) VALUES($1, $2, $3, $4)`,
31+
"Sample MJML template", "campaign", "", tpl.ReadBytes()); err != nil {
32+
return err
33+
}
34+
1935
return nil
2036
}

models/models.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/jmoiron/sqlx"
1717
"github.com/jmoiron/sqlx/types"
1818
"github.com/lib/pq"
19+
"github.com/preslavrachev/gomjml/mjml"
1920
"github.com/yuin/goldmark"
2021
"github.com/yuin/goldmark/extension"
2122
"github.com/yuin/goldmark/parser"
@@ -49,6 +50,7 @@ const (
4950
CampaignContentTypeMarkdown = "markdown"
5051
CampaignContentTypePlain = "plain"
5152
CampaignContentTypeVisual = "visual"
53+
CampaignContentTypeMJML = "mjml"
5254

5355
// List.
5456
ListTypePrivate = "private"
@@ -545,19 +547,38 @@ func (c *Campaign) CompileTemplate(f template.FuncMap) error {
545547
body = r.regExp.ReplaceAllString(body, r.replace)
546548
}
547549

550+
// Parse the base template also as MJML if the campaign content type is MJML.
551+
if c.ContentType == CampaignContentTypeMJML {
552+
htmlBody, err := mjml.Render(body)
553+
if err != nil {
554+
return fmt.Errorf("error compiling MJML: %v", err)
555+
}
556+
body = htmlBody
557+
}
558+
548559
baseTPL, err := template.New(BaseTpl).Funcs(f).Parse(body)
549560
if err != nil {
550561
return fmt.Errorf("error compiling base template: %v", err)
551562
}
552563

553-
// If the format is markdown, convert Markdown to HTML.
554-
if c.ContentType == CampaignContentTypeMarkdown {
564+
// If the campaign format is markdown, convert Markdown to HTML.
565+
body = c.Body
566+
switch c.ContentType {
567+
case CampaignContentTypeMarkdown:
555568
var b bytes.Buffer
556569
if err := markdown.Convert([]byte(c.Body), &b); err != nil {
557570
return err
558571
}
559572
body = b.String()
560-
} else {
573+
574+
// Is it MJML? Convert to HTML.
575+
case CampaignContentTypeMJML:
576+
htmlBody, err := mjml.Render(c.Body)
577+
if err != nil {
578+
return fmt.Errorf("error compiling MJML: %v", err)
579+
}
580+
body = htmlBody
581+
default:
561582
body = c.Body
562583
}
563584

@@ -609,6 +630,13 @@ func (c *Campaign) ConvertContent(from, to string) (string, error) {
609630
return out, err
610631
}
611632
out = b.String()
633+
} else if from == CampaignContentTypeMJML &&
634+
(to == CampaignContentTypeHTML || to == CampaignContentTypeRichtext) {
635+
htmlBody, err := mjml.Render(c.Body)
636+
if err != nil {
637+
return out, fmt.Errorf("error converting MJML: %v", err)
638+
}
639+
out = htmlBody
612640
} else {
613641
return out, errors.New("unknown formats to convert")
614642
}

0 commit comments

Comments
 (0)