Skip to content

Commit 401a399

Browse files
author
Léo Bonnargent
committed
Add option urlTransform
- Allow to pass a function to tranform file path to url - can transform WSL path to windows path if needed - url provided to `open` instead of local path to allow usage of http (output files could be written to a served directory) should fix #18
1 parent 4e48c73 commit 401a399

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const previewEmail = async (message, options) => {
2828
id: uuid.v4(),
2929
open: { wait: false },
3030
template: templateFilePath,
31+
urlTransform: path => `file://${path}`,
3132
...options
3233
};
3334
debug('message', message, 'options', options);
@@ -52,9 +53,10 @@ const previewEmail = async (message, options) => {
5253
debug('filePath', filePath);
5354
await writeFile(filePath, html);
5455

55-
if (options.open) await open(filePath, options.open);
56+
const url = options.urlTransform(filePath);
57+
if (options.open) await open(url, options.open);
5658

57-
return `file://${filePath}`;
59+
return url;
5860
};
5961

6062
module.exports = previewEmail;

test/test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,14 @@ test('does not open in browser', async t => {
6262
const url = await previewEmail({}, { open: false });
6363
t.true(typeof url === 'string');
6464
});
65+
66+
test('transform URL', async t => {
67+
const url = await previewEmail(
68+
{},
69+
{
70+
open: false,
71+
urlTransform: path => `http://localhost:8000/${path}`
72+
}
73+
);
74+
t.regex(url, /^http:\/\/localhost:8000\//);
75+
});

0 commit comments

Comments
 (0)