Closed ertrzyiks closed 6 years ago
Looks like it's possible in the after_generate
filter.
var Promise = require('bluebird')
var streamToArray = require('stream-to-array')
var streamToArrayAsync = Promise.promisify(streamToArray)
function loadFileContent(stream) {
return streamToArrayAsync(stream).then(function (parts) {
const buffers = parts.map(function (part) {
return util.isBuffer(part) ? part : Buffer.from(part)
});
return Buffer.concat(buffers);
})
}
function processFile(buffer) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve(buffer.toString())
}, 100)
})
}
hexo.extend.filter.register('after_generate', function () {
var filePath = '/some/page.html'
return route.set(filePath, function () {
var stream = route.get(filePath)
return loadFileContent(stream).then(function (buffer) {
return processFile(buffer)
})
})
})
Environment Info
Node version(
node -v
): v8.9.3Hexo and Plugin version(
npm ls --depth 0
): 3.2.0For question
I want to apply some changes to the output HTML file, but the operation I want to do is asynchronous.
My initial thought was to use
after_render:html
hook, but it seems to work only synchronously in my case.What I really need is an asynchronous helper, but seems like EJS supports only synchronous helpers. What alternatives do I have with the current API?