-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Injector does not bind function to hexo object #4446
Comments
Tags are not bound either. Most of them use a function as return value, e.g. hexo/lib/plugins/tag/asset_img.js Lines 13 to 16 in add6c00
Lines 83 to 92 in 1489074
|
probably most tags don't need to access the config; meanwhile injector inserts to all pages by default and may need to inserts some config value, especially for inserting to |
It is not binding. Filter is using Reflect.apply to force the code run in hexo context. |
The original proposal #4047 mentioned the injector is designed as a convenient wrapper around
If Injector is suitable for that use case, a dev might replace existing filter with injector without
why not run Injector in hexo context?
config is static. |
In short, |
Here is a possible workaround. // index.js
hexo.extend.injector.register('head_end', require('./lib/inject')(hexo))
// lib/inject.js
module.exports = (hexo) => () => {
const mathCfg = hexo.config.math
return `<lorem>${mathCfg.css}</lorem>`
}
/*
module.exports = function(ctx) {
return function() {
// Stuff that can access ctx
}
}
It is an idea from functional programing. A function being "exports" will return another function to be called.
*/ |
This is exactly how Hexo handles tags. Can be traced back to this commit in 2014 I can't tell which one is better - bind this or lambda calculus & currying. |
Noticed in hexojs/hexo-math#130
In
filter
, the function is automatically binded tohexo
,However, that style doesn't work in injector,
I need to manually bind it,
The text was updated successfully, but these errors were encountered: