Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ DEBUG=preview-email node app.js
* `dir` (String) - a path to a directory for saving the generated email previews (defaults to `os.tmpdir()`, see [os docs](https://nodejs.org/api/os.html#os_os_tmpdir) for more insight)
* `open` (Object or Boolean) - an options object that is passed to [open][] (defaults to `{ wait: false }`) - if set to `false` then it will not open the email automaitcally in the browser using [open][], and if set to `true` then it will default to `{ wait: false }`
* `template` (String) - a file path to a `pug` template file (defaults to preview-email's [template.pug](template.pug) by default) - **this is where you can pass a custom template for rendering email previews, e.g. your own stylesheet**
* `urlTransform` (Function (path) => url) - a function to build preview url from file path (defaults to `(path) => 'file://[file path]'`) - _this is where you can customize the opened path to handle WSL to Windows tranformation or build a http url if `dir` is served._


## Contributors
Expand Down
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const previewEmail = async (message, options) => {
id: uuid.v4(),
open: { wait: false },
template: templateFilePath,
urlTransform: path => `file://${path}`,
...options
};
debug('message', message, 'options', options);
Expand All @@ -52,9 +53,10 @@ const previewEmail = async (message, options) => {
debug('filePath', filePath);
await writeFile(filePath, html);

if (options.open) await open(filePath, options.open);
const url = options.urlTransform(filePath);
if (options.open) await open(url, options.open);

return `file://${filePath}`;
return url;
};

module.exports = previewEmail;
11 changes: 11 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,14 @@ test('does not open in browser', async t => {
const url = await previewEmail({}, { open: false });
t.true(typeof url === 'string');
});

test('transform URL', async t => {
const url = await previewEmail(
{},
{
open: false,
urlTransform: path => `http://localhost:8000/${path}`
}
);
t.regex(url, /^http:\/\/localhost:8000\//);
});