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

[Question] Capture file buffer post render / pre save #940

Open
DigitalKrony opened this issue Sep 25, 2017 · 10 comments
Open

[Question] Capture file buffer post render / pre save #940

DigitalKrony opened this issue Sep 25, 2017 · 10 comments

Comments

@DigitalKrony
Copy link

Good Morning/Afternoon/Evening,

I'm wondering if there's currently a step that might be unpublished, unseen, or simply unused where I can acquire the file buffer of the rendered file BEFORE it is saved into the file system?

For instance, say I was pairing Assemble.io with an Express server and wanted to render a specific URL on request, rather than serve a static from a pre-rendered dist folder.

@assemblebot
Copy link

@DigitalKrony Thanks for the issue! If you're reporting a bug, please be sure to include:

  • The version of assemble you are using.
  • Your assemblefile.js (This can be in a gist)
  • The commandline output. (Screenshot or gist is fine)
  • What you expected to happen instead.

If your issue is related to one of the following, please open an issue there:

  • grunt-assemble Issues with using assemble in grunt or the grunt-assemble library.
  • handlebars-helpers Issues with using handlebars helpers from the handlebars-helpers library.

@jonschlinkert
Copy link
Member

Indeed you can! try using a .postRender middleware:

app.postRender(/\.hbs$/, function(file, next) {
  // do stuff with file
  next(null, file);
});

Or, if you only want to do this in a single task or pipeline, use a plugin:

app.task('default', function() {
  return app.src('*.hbs')
    .pipe(app.renderFile())
    .pipe(through.obj(function(file, enc, next) {
      // do stuff with file
      next(null, file);
    }))
    .pipe(app.dest('some_folder'));
});

@jonschlinkert
Copy link
Member

you can also use .preWrite, if you have other .postRender middleware and you want to ensure it's called afterward:

app.preWrite(/\.hbs$/, function(file, next) {
  // do stuff with file
  next(null, file);
});

@DigitalKrony
Copy link
Author

Awesome, thanks so much. Also, the express ref links at the bottom of the page below is broken.

https://github.com/assemble/assemble/blob/b65fb6670f0b6da3a21c46f6184bc93c91d99dbd/support/docs/src/content/api/router.md

@DigitalKrony
Copy link
Author

DigitalKrony commented Sep 26, 2017

Ok, diving deeper.

I've built out my class to utilize postRender() and snag the view object that it passes to me, however, I'm not sure that's what I'm looking for.

While that looks like I have access to the file(s) buffers for the page to render, its/their layout(s), and partial(s) used, I seem to be missing the end rendered HTML. Am I missing something? Is there a different function?

@jonschlinkert
Copy link
Member

Hmm, try changing the regex to \.(html|hbs)$. Then to get the rendered string, do file.contents.toString().

Is that what you're looking for?

@DigitalKrony
Copy link
Author

Fantastic! that got me 99% of the way there.

Adding a .HTML to the end of it returned the file content.

Thanks so much for the help!

End request looks like:

Renderer Class

app.postRender(/\.(html|hbs)$/, function(view, next) {
    _this.renderedContent = view.contents.toString();
    next();
});

Server responce

let render = REDACTED.render(config);
let renderPage = () => {
    if (res._header === null) {
        res.send(render.HTML);
        res.end();
        render.removeListener('complete', renderPage);
    }
}

render.on('complete', renderPage);

@jonschlinkert
Copy link
Member

jonschlinkert commented Sep 26, 2017

great! the fwiw, view object has other properties that are useful as well, like view.path, view.data (which is created from front-matter), etc.

@DigitalKrony
Copy link
Author

Very cool.

Now... where should I head when {{md file.md }} isn't rendering?

@jonschlinkert
Copy link
Member

where should I head when {{md file.md }} isn't rendering?

stackoverflow would be a good place to start. we generally direct all implementation questions there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants