Allow generator functions to be replaced programmatically
As a spinoff from #826, we first need to implement the facilities to allow generator functions to be replaceable. Then, we'll start with allowing them to be replaced programmatically in an extension.
A generator function is defined as any of the discrete steps in the generator, as well as any key utility functions those steps use, such as loadAsciiDoc. This definition may expand in the future. For now, the functions are as follows:
- aggregateContent
- buildNavigation
- classifyContent
- convertDocument
- convertDocuments
- createPageComposer
- extractAsciiDocMetadata
- loadAsciiDoc
- loadUi
- mapSite
- produceRedirects
- publishSite
- resolveAsciiDocConfig
Like with the context variables, the generator should retrieve the functions from the generator context. In doing so, it allows the functions to be replaced. Helper method should then be added to the generator context to retrieve and replace the functions. The signature of those methods are as follows:
- getFunctions()
- replaceFunctions(Object)
The replaceFunctions method accepts a map of functions, where the keys are the function names and the values the functions. By accepting an Object, it simplifies the method call.
As part of this change, the functions should be bound to the generator context when loaded. This allows the functions to access the generator context without having to change their signature. Initially, this will only be used to access the utility functions, such as loadAsciiDoc. However, it opens up the possibility in the future that the generator functions can raise events and expose context variables around them, just as the default site generator does today.
Here's an example of an extension that replaces a generator function:
module.exports.register = function () {
this.replaceFunctions({
async publishSite () {
console.log('Not publishing today')
return []
}
})
}