Skip to content

Commit 7162708

Browse files
authored
Merge pull request #19 from aileo/wsl-path
Add option urlTransform
2 parents 4e48c73 + 050f117 commit 7162708

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ DEBUG=preview-email node app.js
108108
* `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)
109109
* `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 }`
110110
* `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**
111+
* `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._
111112

112113

113114
## Contributors

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)