Skip to content

Commit 2e400a5

Browse files
committed
fix(mailer): isolate toText and toHTML, minor changes
1 parent 5386203 commit 2e400a5

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

src/mailer.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,17 @@ export class CreateEmail {
9999
}
100100

101101
// parses pnid name and links. set the plaintext bool (false by default) to use no html
102-
private parseReplacements(c: emailComponent, plainText: boolean = false): void {
102+
private parseReplacements(c: emailComponent, plainText: boolean = false): string {
103+
let tempText = c.text;
104+
103105
// for now only replaces the pnid for shoutouts. could easily be expanded to add more.
104106
if (c?.replacements) {
105107
Object.entries(c.replacements).forEach(([key, value]) => {
106108
if (key === 'pnid') {
107109
if (plainText) {
108-
c.text = c.text.replace(/{{pnid}}/g, value);
110+
tempText = tempText.replace(/{{pnid}}/g, value);
109111
} else {
110-
c.text = c.text.replace(/{{pnid}}/g, `<span class="shoutout" style="color:#cab1fb;">${value}</span>`);
112+
tempText = tempText.replace(/{{pnid}}/g, `<span class="shoutout" style="color:#cab1fb;">${value}</span>`);
111113
}
112114
}
113115
});
@@ -117,54 +119,54 @@ export class CreateEmail {
117119
const bRegex = /<b ?>.*?<\/b>|<strong ?>.*?<\/strong>/g;
118120

119121
if (!plainText) {
120-
c.text = c.text.replace(bRegex, el => `<span style="color:#fff;font-weight:bold;">${el}</span>`);
122+
tempText = tempText.replace(bRegex, el => `<span style="color:#fff;font-weight:bold;">${el}</span>`);
121123
}
122124

123125
// replace [links](https://example.com) with html anchor tags or a plaintext representation
124126
const linkRegex = /\[(?<linkText>.*?)\]\((?<linkAddress>.*?)\)/g;
125127

126128
if (plainText) {
127-
c.text = c.text.replace(linkRegex, '$<linkText> ($<linkAddress>)');
129+
tempText = tempText.replace(linkRegex, '$<linkText> ($<linkAddress>)');
128130
} else {
129-
c.text = c.text.replace(linkRegex, '<a href="$<linkAddress>" style="text-decoration:underline;font-weight:700;color:#fff;"><u>$<linkText></u></a>');
131+
tempText = tempText.replace(linkRegex, '<a href="$<linkAddress>" style="text-decoration:underline;font-weight:700;color:#fff;"><u>$<linkText></u></a>');
130132
}
133+
134+
return tempText;
131135
}
132136

133137
// generates the html version of the email
134138
public toHTML(): string {
135139
let innerHTML = '';
136140

137141
this.componentArray.map((c, i) => {
138-
let el = '&nbsp;';
142+
let el = '';
139143

140144
/* double padding causes issues, and the signature already has padding, so if the last element
141145
* is padding we just yeet it
142146
*/
143147
if (i === this.componentArray.length - 1 && c.type === 'padding') {
144148
return;
145149
}
146-
if (c.type !== 'padding') {
147-
el = this.addGmailDarkModeFix(c.text);
148-
}
150+
149151
switch (c.type) {
150152
case 'padding':
151-
innerHTML += `\n<tr><td width="100%" style="line-height:${c.size}em;">${el}</td></tr>`;
153+
innerHTML += `\n<tr><td width="100%" style="line-height:${c.size}em;">&nbsp;</td></tr>`;
152154
break;
153155
case 'header':
154-
this.parseReplacements(c);
155-
innerHTML += `\n<tr style="font-size:24px;font-weight:700;color:#fff"><td class="header">${el}</td></tr>`;
156+
el = this.parseReplacements(c);
157+
innerHTML += `\n<tr style="font-size:24px;font-weight:700;color:#fff"><td class="header">${this.addGmailDarkModeFix(el)}</td></tr>`;
156158
break;
157159
case 'paragraph':
158-
this.parseReplacements(c);
159-
innerHTML += `\n<tr><td>${el}</td></tr>`;
160+
el = this.parseReplacements(c);
161+
innerHTML += `\n<tr><td>${this.addGmailDarkModeFix(el)}</td></tr>`;
160162
break;
161163
case 'button':
162164
if (c.link) {
163165
el = `<a href="${c.link}" style="color:#fff;" width="100%">${el}</a>`;
164166
} else {
165167
el = `<span style="color:#fff;" width="100%">${el}</span>`;
166168
}
167-
innerHTML += `\n<tr><td ${c.primary ? 'class="primary button" bgcolor="#673db6"' : 'class="secondary button" bgcolor="#373C65"'} style="font-weight:700;border-radius:10px;padding:12px" align="center">${el}</td></tr>`;
169+
innerHTML += `\n<tr><td ${c.primary ? 'class="primary button" bgcolor="#673db6"' : 'class="secondary button" bgcolor="#373C65"'} style="font-weight:700;border-radius:10px;padding:12px" align="center">${this.addGmailDarkModeFix(el)}</td></tr>`;
168170
break;
169171
}
170172
});
@@ -179,16 +181,17 @@ export class CreateEmail {
179181
let plainText = '';
180182

181183
this.componentArray.forEach((c) => {
184+
let el = '';
182185
switch (c.type) {
183186
case 'padding':
184187
break;
185188
case 'header':
186-
this.parseReplacements(c, true);
187-
plainText += `\n${c.text}`;
189+
el = this.parseReplacements(c, true);
190+
plainText += `\n${el}`;
188191
break;
189192
case 'paragraph':
190-
this.parseReplacements(c, true);
191-
plainText += `\n${c.text}`;
193+
el = this.parseReplacements(c, true);
194+
plainText += `\n${el}`;
192195
break;
193196
case 'button':
194197
if (c.link) {

src/util.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,7 @@ export async function sendPNIDDeletedEmail(emailAddress: string, username: strin
281281
const email = new CreateEmail()
282282
.addHeader('Dear {{pnid}},', { pnid: username })
283283
.addParagraph('your PNID has successfully been deleted.')
284-
.addParagraph('If you had a tier subscription, a separate cancellation email will be sent.')
285-
.addParagraph('If you do not receive this cancellation email, or your subscription is still being charged, please contact @jonbarrow on our Discord server.')
286-
.addButton('Join the Discord', 'https://discord.pretendo.network/');
284+
.addParagraph('If you had a tier subscription, a separate cancellation email will be sent. If you do not receive this cancellation email, or you are still being charged for your subscription, please contact <b>@jonbarrow</b> on our [Discord server](https://discord.pretendo.network/).');
287285

288286
const options = {
289287
to: emailAddress,

0 commit comments

Comments
 (0)