Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/devGuide/writingPlugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,12 @@ removing such potential conflicts.
Note however, that variable interpolation syntax {% raw %}`{{ variable_name }}`{% endraw %} will act as per normal.
Meaning, the user would still be able to use variables in your special tags!
</box>

## Lifecycle hooks

You may also need to maintain some plugin state during site generation, then reset this when the site or pages are regenerated.

To do this, you may implement the `beforeSiteGenerate` method.

- `beforeSiteGenerate()`: Called during initial site generation and subsequent regenerations during live preview.
- No return value is required.
24 changes: 24 additions & 0 deletions packages/core/src/Site/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class Site {
this.baseUrlMap = new Set();
this.forceReload = forceReload;
this.plugins = {};
this.pluginsBeforeSiteGenerate = [];
/**
* @type {undefined | SiteConfig}
*/
Expand Down Expand Up @@ -623,6 +624,7 @@ class Site {
.then(() => this.collectBaseUrl())
.then(() => this.collectUserDefinedVariablesMap())
.then(() => this.collectPlugins())
.then(() => this.collectPluginSiteHooks())
.then(() => this.collectPluginSpecialTags())
.then(() => this.buildAssets())
.then(() => (this.onePagePath ? this.lazyBuildSourceFiles() : this.buildSourceFiles()))
Expand Down Expand Up @@ -654,6 +656,8 @@ class Site {
* Build all pages of the site
*/
buildSourceFiles() {
this.runBeforeSiteGenerateHooks();

return new Promise((resolve, reject) => {
logger.info('Generating pages...');
this.generatePages()
Expand Down Expand Up @@ -685,6 +689,8 @@ class Site {
* Only build landing page of the site, building more as the author goes to different links.
*/
lazyBuildSourceFiles() {
this.runBeforeSiteGenerateHooks();

return new Promise((resolve, reject) => {
logger.info('Generating landing page...');
this.generateLandingPage()
Expand All @@ -708,6 +714,7 @@ class Site {
_rebuildAffectedSourceFiles(filePaths) {
const filePathArray = Array.isArray(filePaths) ? filePaths : [filePaths];
const uniquePaths = _.uniq(filePathArray);
this.runBeforeSiteGenerateHooks();

return new Promise((resolve, reject) => {
this.regenerateAffectedPages(uniquePaths)
Expand All @@ -727,6 +734,7 @@ class Site {
const uniqueUrls = _.uniq(normalizedUrlArray);
uniqueUrls.forEach(normalizedUrl => logger.info(
`Building ${normalizedUrl} as some of its dependencies were changed since the last visit`));
this.runBeforeSiteGenerateHooks();

/*
Lazy loading only builds the page being viewed, but the user may be quick enough
Expand Down Expand Up @@ -988,6 +996,15 @@ class Site {
}));
}

/**
* Collect the before site generate hooks
*/
collectPluginSiteHooks() {
this.pluginsBeforeSiteGenerate = Object.values(this.plugins)
.filter(plugin => plugin.beforeSiteGenerate)
.map(plugin => plugin.beforeSiteGenerate);
}

/**
* Collects the special tags of the site's plugins, and injects them into the parsers.
*/
Expand Down Expand Up @@ -1016,6 +1033,13 @@ class Site {
};
}

/**
* Executes beforeSiteGenerate hooks from plugins
*/
runBeforeSiteGenerateHooks() {
this.pluginsBeforeSiteGenerate.forEach(cb => cb());
}

/**
* Creates the supplied pages' page generation promises at a throttled rate.
* This is done to avoid pushing too many callbacks into the event loop at once. (#1245)
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/plugins/default/markbind-plugin-plantuml.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ function generateDiagram(imageOutputPath, content) {
}

module.exports = {
preRender: (content, pluginContext, frontmatter, config) => {
beforeSiteGenerate: () => {
processedDiagrams.clear();

},
preRender: (content, pluginContext, frontmatter, config) => {
// Processes all <puml> tags
const $ = cheerio.load(content);
$('puml').each((i, tag) => {
Expand Down