@designsystemsinternational/email 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/commands/index.js +5 -0
- package/bin/commands/mailchimp.js +57 -0
- package/bin/commands/s3.js +57 -0
- package/bin/commands/scaffold.js +22 -0
- package/bin/commands/send.js +69 -0
- package/bin/commands/zip.js +39 -0
- package/bin/defaultConfig.js +39 -0
- package/dist/cjs/browser.cjs +368 -0
- package/dist/cjs/index.cjs +631 -0
- package/dist/esm/browser.js +361 -0
- package/dist/esm/index.js +620 -0
- package/dist/index.cjs +526 -0
- package/dist/index.js +361 -0
- package/package.json +23 -5
package/dist/index.js
ADDED
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
import posthtml from 'posthtml';
|
|
2
|
+
import removeTags from 'posthtml-remove-tags';
|
|
3
|
+
import removeAttributes from 'posthtml-remove-attributes';
|
|
4
|
+
import matchHelper from 'posthtml-match-helper';
|
|
5
|
+
import colorConvert from 'color-convert';
|
|
6
|
+
import unescape from 'lodash.unescape';
|
|
7
|
+
import JSZip from 'jszip';
|
|
8
|
+
import { saveAs } from 'file-saver';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Generates a deterministic random string based on the input string.
|
|
12
|
+
*
|
|
13
|
+
* @param {string} str Input string
|
|
14
|
+
* @return {string} hash of str
|
|
15
|
+
*/
|
|
16
|
+
const hashFromString = (str) => {
|
|
17
|
+
let hash = 0;
|
|
18
|
+
for (let i = 0; i < str.length; i++) {
|
|
19
|
+
hash = str.charCodeAt(i) + ((hash << 5) - hash);
|
|
20
|
+
}
|
|
21
|
+
return hash.toString();
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Formats a string to a filename by turning it into kebab case
|
|
26
|
+
*
|
|
27
|
+
* @param {string} text
|
|
28
|
+
* @return {string} formatted filename
|
|
29
|
+
*/
|
|
30
|
+
const formatFilename = (text) => {
|
|
31
|
+
return text
|
|
32
|
+
.toLowerCase()
|
|
33
|
+
.replace(/[^a-z0-9-]+/g, ' ')
|
|
34
|
+
.trim()
|
|
35
|
+
.replace(/\s+/g, '-');
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Converts all rgb defined colors with their hex equivalent
|
|
40
|
+
* as some email clients only support hex color format.
|
|
41
|
+
*/
|
|
42
|
+
const convertRgbToHex = () => {
|
|
43
|
+
return (tree) => {
|
|
44
|
+
tree.match(matchHelper('[style]'), (node) => {
|
|
45
|
+
let styleBlock = node.attrs.style;
|
|
46
|
+
styleBlock = styleBlock.replace(
|
|
47
|
+
/rgb\(\s*\d{1,3},\s*\d{1,3},\s*\d{1,3}\)/g,
|
|
48
|
+
(match) => {
|
|
49
|
+
const values = match
|
|
50
|
+
.replace(/[rgb() ]/g, '')
|
|
51
|
+
.split(',')
|
|
52
|
+
.map((n) => parseInt(n));
|
|
53
|
+
return '#' + colorConvert.rgb.hex(values);
|
|
54
|
+
},
|
|
55
|
+
);
|
|
56
|
+
node.attrs.style = styleBlock;
|
|
57
|
+
return node;
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
return tree;
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Prepares HTML by only returning the body tag, to make sure
|
|
66
|
+
*/
|
|
67
|
+
const onlyReturnBody = () => {
|
|
68
|
+
return (tree) => {
|
|
69
|
+
tree.match({ tag: 'body' }, (node) => {
|
|
70
|
+
return node.content;
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
return tree;
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Rewrites the markup by turning all conditional comments returned from the
|
|
79
|
+
* <ConditionalComment> component into true conditional comments that can be
|
|
80
|
+
* picked up by Outlook.
|
|
81
|
+
*/
|
|
82
|
+
const injectConditionalComments = () => {
|
|
83
|
+
return (tree) => {
|
|
84
|
+
tree.match(matchHelper('[data-conditional-comment]'), (node) => {
|
|
85
|
+
const comment = node.attrs['data-conditional-comment'];
|
|
86
|
+
|
|
87
|
+
// Remove HTML comment delimiters
|
|
88
|
+
const cleanedComment = unescape(comment).replace(/<!--|-->/g, '');
|
|
89
|
+
|
|
90
|
+
// Remove Line breaks from comment
|
|
91
|
+
const cleanedCommentLines = cleanedComment.split('\n').join('');
|
|
92
|
+
|
|
93
|
+
node = `<!--${cleanedCommentLines}-->`;
|
|
94
|
+
return node;
|
|
95
|
+
});
|
|
96
|
+
return tree;
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Rewrites the markup by adding all styles defined in the data-mso-style
|
|
102
|
+
* attribute to the <style> tag. This is needed because the mso styles are
|
|
103
|
+
* non-standard CSS, so they cannot be added to React's style tag.
|
|
104
|
+
*/
|
|
105
|
+
const injectMsoStyles = () => {
|
|
106
|
+
return (tree) => {
|
|
107
|
+
tree.match(matchHelper('[data-mso-style]'), (node) => {
|
|
108
|
+
if (node.attrs?.style == null) node.attrs.style = '';
|
|
109
|
+
node.attrs.style += node.attrs['data-mso-style'];
|
|
110
|
+
node.attrs['data-mso-style'] = null;
|
|
111
|
+
return node;
|
|
112
|
+
});
|
|
113
|
+
return tree;
|
|
114
|
+
};
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Collects all inline styles defined in the components, de-dedupes them and
|
|
119
|
+
* moves them to a single unified style tag in the head of the document.
|
|
120
|
+
*/
|
|
121
|
+
const moveInlineStylesToHead = () => {
|
|
122
|
+
let styles = {};
|
|
123
|
+
|
|
124
|
+
return (tree) => {
|
|
125
|
+
tree.match({ tag: 'style' }, (node) => {
|
|
126
|
+
if (!node.content) return '';
|
|
127
|
+
const styleBlock = node.content[0].trim();
|
|
128
|
+
const hash = hashFromString(styleBlock);
|
|
129
|
+
|
|
130
|
+
if (styleBlock) {
|
|
131
|
+
styles[hash] = styleBlock;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Remove the inline style node
|
|
135
|
+
return '';
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
tree.match({ tag: 'head' }, (node) => {
|
|
139
|
+
node.content.push({
|
|
140
|
+
tag: 'style',
|
|
141
|
+
content: Object.values(styles).join(''),
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
return node;
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
return tree;
|
|
148
|
+
};
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const prepareHtmlFactory = (plugins) => {
|
|
152
|
+
const htmlProcessor = posthtml();
|
|
153
|
+
|
|
154
|
+
plugins.forEach((plugin) => {
|
|
155
|
+
htmlProcessor.use(plugin);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
return async (body, template = (str) => str) => {
|
|
159
|
+
const content = template(body);
|
|
160
|
+
|
|
161
|
+
const processed = await htmlProcessor.process(content);
|
|
162
|
+
|
|
163
|
+
return processed.html;
|
|
164
|
+
};
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* This default pipeline removes all script and link tags from the HTML,
|
|
169
|
+
* moves all inline styles to the head and wraps everything in the email template.
|
|
170
|
+
*
|
|
171
|
+
* @param {string} body - the html body
|
|
172
|
+
* @param {function} template - the template function, defaults to no template
|
|
173
|
+
*/
|
|
174
|
+
const defaultPipeline = prepareHtmlFactory([
|
|
175
|
+
onlyReturnBody(),
|
|
176
|
+
removeTags({ tags: ['script', 'link'] }),
|
|
177
|
+
convertRgbToHex(),
|
|
178
|
+
moveInlineStylesToHead(),
|
|
179
|
+
injectMsoStyles(),
|
|
180
|
+
injectConditionalComments(),
|
|
181
|
+
removeAttributes(['data-reactid', 'data-reactroot']),
|
|
182
|
+
]);
|
|
183
|
+
|
|
184
|
+
const BASE64_REGEX = /data:image\/([a-zA-Z]*);base64,(.*)/;
|
|
185
|
+
|
|
186
|
+
// Browser packacking factory
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Turns base64 inlined images into image buffers and updates their src
|
|
190
|
+
* attribute accordingly. Returns an object containing the updated html and
|
|
191
|
+
* an array of image buffers. Different adapters can then hook into this to
|
|
192
|
+
* process the email into a ZIP archive or pushing it to an API.
|
|
193
|
+
*
|
|
194
|
+
* @param {string} body - the prepared html body
|
|
195
|
+
* @return {object} the processed html and base64 strings for all images
|
|
196
|
+
*/
|
|
197
|
+
const preparePackageFactory =
|
|
198
|
+
({ urlPrefix = '', handlePackage }) =>
|
|
199
|
+
async (body, opts = {}) => {
|
|
200
|
+
let counter = 0;
|
|
201
|
+
const images = [];
|
|
202
|
+
|
|
203
|
+
const processed = await posthtml()
|
|
204
|
+
.use((tree) => {
|
|
205
|
+
tree.match({ tag: 'img' }, (node) => {
|
|
206
|
+
const { src, alt } = node.attrs;
|
|
207
|
+
|
|
208
|
+
if (BASE64_REGEX.test(src)) {
|
|
209
|
+
const [_, ext, data] = src.match(
|
|
210
|
+
/data:image\/([a-zA-Z]*);base64,(.*)/,
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
const hash = Math.random().toString(36).substring(2, 7);
|
|
214
|
+
|
|
215
|
+
const filename = alt
|
|
216
|
+
? `${formatFilename(alt)}-text-${hash}.${ext}`
|
|
217
|
+
: `image-${counter}.${ext}`;
|
|
218
|
+
|
|
219
|
+
node.attrs.src = `${urlPrefix}${filename}`;
|
|
220
|
+
|
|
221
|
+
images.push({
|
|
222
|
+
filename,
|
|
223
|
+
buffer: data,
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
counter++;
|
|
227
|
+
}
|
|
228
|
+
return node;
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
return tree;
|
|
232
|
+
})
|
|
233
|
+
.process(body);
|
|
234
|
+
|
|
235
|
+
return await handlePackage(
|
|
236
|
+
{
|
|
237
|
+
html: processed.html,
|
|
238
|
+
images,
|
|
239
|
+
},
|
|
240
|
+
opts,
|
|
241
|
+
);
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
const makeDefaultTemplate =
|
|
245
|
+
({ lang = 'en-US', dir = 'ltr' } = {}) =>
|
|
246
|
+
(body) =>
|
|
247
|
+
`<!doctype html>
|
|
248
|
+
<html
|
|
249
|
+
lang="${lang}"
|
|
250
|
+
xml:lang="${lang}"
|
|
251
|
+
dir="${dir}"
|
|
252
|
+
xmlns="http://www.w3.org/1999/xhtml"
|
|
253
|
+
xmlns:v="urn:schemas-microsoft-com:vml"
|
|
254
|
+
xmlns:o="urn:schemas-microsoft-com:office:office"
|
|
255
|
+
>
|
|
256
|
+
<head>
|
|
257
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
258
|
+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
|
259
|
+
<title>Chief</title>
|
|
260
|
+
<!--[if mso]>
|
|
261
|
+
<style type="text/css">
|
|
262
|
+
table {
|
|
263
|
+
border-collapse: collapse;
|
|
264
|
+
border: 0;
|
|
265
|
+
border-spacing: 0;
|
|
266
|
+
margin: 0;
|
|
267
|
+
mso-table-lspace: 0pt !important;
|
|
268
|
+
mso-table-rspace: 0pt !important;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
img {
|
|
272
|
+
display: block !important;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
div, td {padding:0;}
|
|
276
|
+
|
|
277
|
+
div {margin:0 !important;}
|
|
278
|
+
|
|
279
|
+
.notMso {
|
|
280
|
+
display: none;
|
|
281
|
+
mso-hide: all;
|
|
282
|
+
}
|
|
283
|
+
</style>
|
|
284
|
+
<noscript>
|
|
285
|
+
<xml>
|
|
286
|
+
<o:OfficeDocumentSettings>
|
|
287
|
+
<o:PixelsPerInch>96</o:PixelsPerInch>
|
|
288
|
+
</o:OfficeDocumentSettings>
|
|
289
|
+
</xml>
|
|
290
|
+
</noscript>
|
|
291
|
+
<![endif]-->
|
|
292
|
+
<meta name="x-apple-disable-message-reformatting" />
|
|
293
|
+
<meta httpEquiv="Content-Type" content="text/html; charset=utf-8" />
|
|
294
|
+
<meta httpEquiv="X-UA-Compatible" content="IE=edge" />
|
|
295
|
+
<meta name="viewport" content="width=device-width" />
|
|
296
|
+
<meta name="color-scheme" content="light" />
|
|
297
|
+
<meta name="supported-color-schemes" content="light" />
|
|
298
|
+
|
|
299
|
+
<meta name="format-detection" content="telephone=no">
|
|
300
|
+
<meta name="format-detection" content="date=no">
|
|
301
|
+
<meta name="format-detection" content="address=no">
|
|
302
|
+
<meta name="format-detection" content="email=no">
|
|
303
|
+
|
|
304
|
+
<style>
|
|
305
|
+
a[x-apple-data-detectors] {
|
|
306
|
+
color: inherit !important;
|
|
307
|
+
|
|
308
|
+
font-size: inherit !important;
|
|
309
|
+
font-family: inherit !important;
|
|
310
|
+
font-weight: inherit !important;
|
|
311
|
+
line-height: inherit !important;
|
|
312
|
+
text-decoration: inherit !important;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
#MessageViewBody a:not([style]) {
|
|
316
|
+
color: inherit !important;
|
|
317
|
+
font-size: inherit !important;
|
|
318
|
+
font-family: inherit !important;
|
|
319
|
+
font-weight: inherit !important;
|
|
320
|
+
line-height: inherit !important;
|
|
321
|
+
text-decoration: inherit !important;
|
|
322
|
+
}
|
|
323
|
+
</style>
|
|
324
|
+
</head>
|
|
325
|
+
<body class="body" style="padding:0;">
|
|
326
|
+
${body}
|
|
327
|
+
</body>
|
|
328
|
+
</html>`;
|
|
329
|
+
|
|
330
|
+
const defaultTemplate = makeDefaultTemplate();
|
|
331
|
+
|
|
332
|
+
const defaultConfig = {
|
|
333
|
+
name: 'email',
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* @param {string} body - the prepared html body
|
|
338
|
+
* @return {function} A function that can be called to download the email as a ZIP archive
|
|
339
|
+
*/
|
|
340
|
+
const packageToZip = preparePackageFactory({
|
|
341
|
+
urlPrefix: '',
|
|
342
|
+
handlePackage: ({ html, images }, opts) => {
|
|
343
|
+
const config = { ...defaultConfig, ...opts };
|
|
344
|
+
|
|
345
|
+
const zip = new JSZip();
|
|
346
|
+
const files = zip.folder(config.name);
|
|
347
|
+
|
|
348
|
+
files.file('index.html', html);
|
|
349
|
+
images.forEach(({ filename, buffer }) => {
|
|
350
|
+
files.file(filename, buffer, { base64: true });
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
return async () => {
|
|
354
|
+
files.generateAsync({ type: 'blob' }).then((content) => {
|
|
355
|
+
saveAs(content, `${formatFilename(config.name)}.zip`);
|
|
356
|
+
});
|
|
357
|
+
};
|
|
358
|
+
},
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
export { defaultTemplate, makeDefaultTemplate, packageToZip, defaultPipeline as prepareHtml, prepareHtmlFactory, preparePackageFactory };
|
package/package.json
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@designsystemsinternational/email",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "Make working with email more enjoyable (if possible)",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./dist/esm/index.js",
|
|
10
|
+
"require": "./dist/cjs/index.cjs"
|
|
11
|
+
},
|
|
12
|
+
"./browser": {
|
|
13
|
+
"import": "./dist/esm/browser.js",
|
|
14
|
+
"require": "./dist/cjs/browser.cjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
7
17
|
"files": [
|
|
8
|
-
"src/**"
|
|
18
|
+
"src/**",
|
|
19
|
+
"dist/**",
|
|
20
|
+
"bin/**"
|
|
9
21
|
],
|
|
10
22
|
"repository": {
|
|
11
23
|
"type": "git",
|
|
@@ -15,20 +27,26 @@
|
|
|
15
27
|
"email-cli": "bin/cli.js"
|
|
16
28
|
},
|
|
17
29
|
"scripts": {
|
|
30
|
+
"build": "rollup -c",
|
|
18
31
|
"cli": "node bin/cli.js",
|
|
19
|
-
"test": "vitest run",
|
|
20
32
|
"lint": "eslint .",
|
|
21
|
-
"
|
|
22
|
-
"
|
|
33
|
+
"prepublish": "npm run build && npm run lint",
|
|
34
|
+
"prepare": "npm run build",
|
|
35
|
+
"prettier:fix": "prettier --write .",
|
|
36
|
+
"test": "vitest run",
|
|
37
|
+
"test:watch": "vitest"
|
|
23
38
|
},
|
|
24
39
|
"author": "Design Systems International <i@designsystems.international>",
|
|
25
40
|
"license": "ISC",
|
|
26
41
|
"devDependencies": {
|
|
42
|
+
"@rollup/plugin-commonjs": "^23.0.5",
|
|
43
|
+
"@rollup/plugin-json": "^5.0.2",
|
|
27
44
|
"@vitest/coverage-c8": "^0.22.0",
|
|
28
45
|
"@vitest/ui": "^0.22.0",
|
|
29
46
|
"eslint": "^8.22.0",
|
|
30
47
|
"eslint-config-prettier": "^8.5.0",
|
|
31
48
|
"prettier": "2.7.1",
|
|
49
|
+
"rollup": "^3.7.4",
|
|
32
50
|
"vite": "^4.0.0",
|
|
33
51
|
"vitest": "^0.22.0"
|
|
34
52
|
},
|