Repository navigation
Open
Description
Plugins can add helpers ($utils), partials, loaders, and a $hooks.transform that changes finished HTML. None of them can add data to a page before it renders. So anything that a site computes per page, such as its language, its translations, or its previous and next page, ends up as long expressions in every template, and often as a filter over $pages on every page, whose work grows with pages × pages.
This issue adds that step to core: $hooks.page. It needs a full core benchmark before and after (see the end). #58 builds @bongo/i18n on it.
The hook
// index.d.ts
export namespace Build {
/**
* Return the params of one page, before any page renders. bongo calls it once for each page.
* @example const page: Build.PageHook = page => page.path.startsWith('/fr/') ? { lang: 'fr', ...page.params } : page.params;
*/
type PageHook = (page: Template.PageInfo, ctx: PageHook.Context) => Promisable<Template.Params>;
namespace PageHook {
type Context = {
/** Every page, as the hooks before this one left them. */
pages: Template.PageInfo[];
/** The config, with its plugins merged. */
config: Required<Config>;
};
}
type Hooks = {
transform?: Transform;
page?: PageHook;
};
}When it runs
1. read content/ frontmatter and loaders → one entry per page
2. $hooks.page ← new: once for each page
3. build $pages, $section, $tags from the new params
4. render pages, then output files, then data modules
5. $hooks.transform on each finished page and output file
Rules
pageis the page as$pagesshows it:path,url,title,summary,section,tags,headings, andparams(its frontmatter).ctx.pagesholds every page, so a hook can compare pages.- The hook returns the whole new
params. It decides if frontmatter wins:{ lang: 'fr', ...page.params }is a default,{ ...page.params, lang: 'fr' }always wins. - bongo derives
title,summary, andtagsagain from the new params, so a hook can set a title or add a tag, and$tagsholds that tag. path,url, andsectioncannot change: they decide the output file and folder, which are fixed by then. A hook that changesparams.urlfails the build and names the page.- Hooks run in turn, the plugins' in order, then the config's own. Each runs over every page, and sees in
ctx.pageswhat the hooks before it returned. - The new params are the same everywhere:
$page.params, the entries of$pages,$section, and$tags, output files, data modules, andctx.paramsof$hooks.transform. - A hook that throws fails the build and names the page.
- One call per page per build. Dev calls it on every rebuild, since a rebuild reads all frontmatter anyway. A hook that compares pages should index
ctx.pagesonce per build, such as in aWeakMapkeyed by the array.
Example: folder defaults
export default {
$hooks: {
page: page => page.path.startsWith('/fr/') ? { lang: 'fr', ...page.params } : page.params,
},
};Proposed additions (decide before building)
page.file: the source path of the page incontent/, such asblog/post.md. Edit-on-GitHub links and<lastmod>need it, andindex.d.tsalready has// TODO: file.abs pathand// TODO: file.rel path. Recommended: yes.- Return
nullto drop a page: no output file, and not in$pages,$section, or$tags. This gives drafts and scheduled posts:page => page.params.draft && !dev ? null : page.params. It changes which files a build writes, so it needs its own tests and part of the benchmark.
Uses
- Translations: a plugin sets
lang,key, andversionson each page once, in place of long expressions in each template. See @bongo/i18n, and config partials with page values #58. - Folder defaults: one hook sets defaults for every page in a folder, such as
langorgroup, in place of the same frontmatter in each page (see "Example: folder defaults" above). - Docs site sidebar and pager:
$utils.navand$utils.pagerrebuild the page list on each page render (docs/layouts/single.hbs). The hook can setparams.prevandparams.nextonce. - Edit-on-GitHub links from the real source file (with
page.file), in place of a guess from the URL. <lastmod>in sitemaps:params.updatedfrom git or the file time (withpage.file).- Dates parsed once, and
params.yearfor archives. - Related posts and series, from one index per build.
- Drafts and scheduled posts (with
nullto drop a page).
Benchmark (required)
This changes the order of a build for every site, so it needs a full core benchmark before and after. The same benchmark serves #56.
- Add a benchmark that generates a large site: for example 10,000 Markdown pages in nested folders, with tags, layouts and partials that read
$pages, output files, and data modules. - On
mainbefore the change, record the time and the peak memory of:bongo build, the first build ofbongo dev, and a narrow dev rebuild (one page edited). - Record the same with the change, with no hook, so that the cost of the new step alone shows. @bongo/i18n, and config partials with page values #58 measures it again with
@bongo/i18n. - Put the numbers in the PR. With no hook, the time and memory must not get worse.
Work
- Benchmark on
main -
$hooks.pagein core, with tests - The proposed additions, if accepted, with tests
- Docs: the Hooks reference, and a guide section on folder defaults
- Benchmark after, numbers in the PR
Metadata
Metadata
Assignees
Labels
No labels
Activity
[-]core: $hooks.page, config partials with page values, and @bongo/i18n[/-][+]core: $hooks.page[/+]