It would be great if we could implement our own normalizers for manipulating WordPress entities.
For example when we want to search and replace the hostname in URLs.
I was playing around with a simple PR where normalizers are passed via the options object then executed like so:
for (let i = 0; i < normalizers.length; i++) {
if (typeof normalizers[i] === 'function') {
entities = normalizers[i](entities)
continue
}
}
This is a very crude example ideally you would prepare the code to accept promises as well as functions.
The remark package has some similar extension functionality
Gatsby version: Latest
Node.js version: 8.2.1
Operating System: OSX El Capitan
gatsby-config.js: not changed
package.json: not changed
gatsby-node.js: not changed
gatsby-browser.js: not changed
gatsby-ssr.js: not changed
This sounds like a great idea! Feel free to implement this!
Yeah, plugins can have plugins (which could have plugins!!!!!!! — but don't do that :-)). So this sounds great.
Though for this particular example of replacing hostnames — that sounds like something the source plugin should handle directly?
Great! I'll take a closer look at implementing this properly and submitting a PR soon!
I think your right about the replacement of hostnames being handled in the plugin, I'll write up a normaliser to do this.
I had difficulty testing this package any tips?
Also hope the PR is ok Let me know if you require any amendments.
@moshie checkout https://www.gatsbyjs.org/docs/how-to-contribute/#contributing
Thought of passing custom normalizers as well today via gatsby-config.js and played around with an implementation before I saw this issue :)
Any progress on this? Do you need help with anything, @moshie?
Apologies, I have been short on time this week I have been working on bits here and there.
I will have something PR'd soon I promise() 👍
So I was thinking about how this might work and came up with this idea of setting the normalisers to an object.
Some people may want to run a normaliser before the "built in" ones so I specified a priority argument to define when it gets called, I was thinking the default priority could just be anything after.
This way also allows you to define multiple normalisers in one plugin.
// Plugin
//
exports.normalizer = function (normalizer) {
function modifyEntitys(entities, args) {
// Normalizer
return entities;
}
// returns normalizer
return normalizer.set(modifyEntitys, 20); // callback, priority
}
Giving priorities is a great idea! How about giving the built-in normalizers a priority as well, so you could modify entities between two of them?
Just a quick thought, could make things more complicated and might be a bit overengineered as well.
Glad you think so! I think that's a good idea about giving the built in normalisers the same priority argument, Ill write it up and report back soon :)
I am sort of stuck / out of my depth with this one.
Can't think of a clean way of integrating custom normalisers with the already coded ones.
I would appreciate a hand in making this happen.
Could you get by with a new normalize option that got called with each entity after all the built-in ones are finished? That wouldn't complicate the code much and would let you fix urls and other normal tweaks.
Due to the high volume of issues, we're closing out older ones without recent activity. Please open a new issue if you need help!