Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting async-only jstransformers; fixes #166 #167

Merged
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
28 changes: 12 additions & 16 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,25 @@ function getLayout({ file, settings }) {
*/

function render({ filename, files, metadata, settings, metalsmith }) {
return new Promise(resolve => {
const file = files[filename];
const layout = getLayout({ file, settings });
const extension = layout.split('.').pop();

debug(`rendering ${filename} with layout ${layout}`);
const file = files[filename];
const layout = getLayout({ file, settings });
const extension = layout.split('.').pop();

// Stringify file contents
let contents = file.contents.toString();
debug(`rendering ${filename} with layout ${layout}`);

const transform = getTransformer(extension);
const locals = Object.assign({}, metadata, file, { contents });
const layoutPath = path.join(metalsmith.path(settings.directory), layout);
// Stringify file contents
const contents = file.contents.toString();

// Transform the contents
contents = transform.renderFile(layoutPath, settings.engineOptions, locals).body;
const transform = getTransformer(extension);
const locals = Object.assign({}, metadata, file, { contents });
const layoutPath = path.join(metalsmith.path(settings.directory), layout);

// Transform the contents
return transform.renderFileAsync(layoutPath, settings.engineOptions, locals).then(rendered => {
// Update file with results
// eslint-disable-next-line no-param-reassign
file.contents = Buffer.from(contents);

file.contents = Buffer.from(rendered.body);
debug(`done rendering ${filename}`);
return resolve();
});
}

Expand Down
17 changes: 17 additions & 0 deletions lib/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ describe('metalsmith-layouts', () => {
});
});

it('should apply a single layout to a single file with an async jstransformer', done => {
const base = path.join(process.cwd(), 'test', 'fixtures', 'single-file-async');
const actual = path.join(base, 'build');
const expected = path.join(base, 'expected');
const metalsmith = new Metalsmith(base);

rimraf.sync(actual);

return metalsmith.use(plugin()).build(err => {
if (err) {
return done(err);
}
expect(() => equal(actual, expected)).not.toThrow();
return done();
});
});

it('should apply a single layout to multiple files', done => {
const base = path.join(process.cwd(), 'test', 'fixtures', 'multiple-files');
const actual = path.join(base, 'build');
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"husky": "^0.14.3",
"jest": "^23.5.0",
"jstransformer-handlebars": "^1.0.0",
"jstransformer-qejs": "^0.2.0",
"lint-staged": "^7.0.0",
"metalsmith": "^2.3.0",
"prettier": "^1.9.2",
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/single-file-async/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<html>
<head></head>
<body>
<h1>The title</h1>

</body>
</html>
7 changes: 7 additions & 0 deletions test/fixtures/single-file-async/layouts/standard.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!DOCTYPE html>
<html>
<head></head>
<body>
<%- contents %>
</body>
</html>
4 changes: 4 additions & 0 deletions test/fixtures/single-file-async/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
layout: standard.ejs
---
<h1>The title</h1>