hexojs / hexo

A fast, simple & powerful blog framework, powered by Node.js.
https://hexo.io
MIT License
39.22k stars 4.82k forks source link

Tag plugin async callback #441

Closed visioncan closed 10 years ago

visioncan commented 10 years ago

I'm tried to extend Tag plugin for <img> tag from Filckr, I use async and request to fetch FlickrAPI json. but callback can't waiting for output. I think that is because of async.

How can make this work? thank you and your awesome Hexo! :smile:

tommy351 commented 10 years ago

Tag plugins don't support for async callback because Hexo uses Swig as template engine.

You can try the filter, it's more flexible and supports async callback. For example, this is how titlecase works:

var titlecase = hexo.util.titlecase;

hexo.extend.filter.register('pre', function(data, callback){
  if (!hexo.config.titlecase || !data.title) return callback();

  data.title = titlecase(data.title);

  callback(null, data);
});
visioncan commented 10 years ago

OK, got it! I will try. Thank you ~

visioncan commented 10 years ago

I made it. https://github.com/visioncan/hexo-tag-flickr :stuck_out_tongue_closed_eyes:

tommy351 commented 10 years ago

Brilliant combination of filter and tag!

visioncan commented 10 years ago

hahaha! actually, I'm not sure whether this is optimal or another solution to do it?

akfish commented 9 years ago

@tommy351 And now filter plugins don't have async method. Am I missing something?

tommy351 commented 9 years ago

@akfish Filter plugins still support async method. You need to use promise instead.

akfish commented 9 years ago

@tommy351 Thanks for the information.