Publican.lib event hooks
533 words, 3-minute read
The hook event functions append supplemental data and change some aspects of static site rendering. The libInit() function adds all hook functionality:
publican.config.js excerpt
// Publican configuration
import { Publican, tacs } from 'publican';
import { libInit } from 'publican.lib';
// ...set Publican defaults...
// initialize publican.lib
libInit(publican, tacs);
// build
await publican.build();
Or you can add event hooks individually:
publican.config.js excerpt
// Publican configuration
import { Publican, tacs } from 'publican';
import * as hook from 'publican.lib/hook';
// ...set Publican defaults...
// add hooks
publican.config.processPostRender.add( hook.postrenderMeta );
// build
await publican.build();
processFileDate() #
A processContent hook that sets the content date and modified properties for any content file using a file name pattern YYYY-MM-DD_something.md.
It does not alter the rendered file slug, but you can remove the date with:
publican.config.js excerpt
// slug replacement strings - remove YYYY-MM-DD
publican.config.slugReplace.set(/\d{4}-\d{2}-\d{2}_/g, '');
contentFilename() #
A processContent hook that parses a {{ filename }} above a code block in a markdown file. You can style the resulting HTML to show a tab or similar:
<p class="filename language-js">
<dfn>myfile.js</dfn>
</p>
<pre class="language-js">...
htmlBlocks() #
A processContent hook that replaces markdown lines starting with three colons into HTML tags. For example:
content markdown
::: div id="mydiv"
Some content
::: /div
results in the HTML:
output HTML
<div id="mydiv">
<p>Some content<p>
</div>
renderstartData() #
A processRenderStart hook that modifies the title and description of all tag index files to create friendlier text such as:
- “JavaScript” posts
- List of 23 posts using the tag “JavaScript”.
renderstartInlineScripts() #
A processRenderStart hook that creates a global tacs.script JavaScript Map that defines inline scripts using cspScript():
tacs.script.get('theme'): an inline script that appends adata-themeattribute to the root<html>element using a localStorage valuetacs.script.get('speculation'): an inline speculation rules definition
prerenderInlineScripts() #
A processPreRender hook that creates a data.script JavaScript Map for every page that defines an inline script using cspScript():
data.script.get('schema'): an inline schema.org structured data block.
renderstartTagScore() #
A processRenderStart hook that creates a global tacs.tagScore JavaScript Map object that calculates a score for each tag (lesser-used tags have a higher score). This helps determine related articles.
prerenderRelated() #
A processPreRender hook that appends a data.related array to every page with a list of related articles in order of relevancy. To output the three most-relevant related articles:
template
${ data?.related?.length ? `
<aside>
<h2>Related posts</h2>
<nav>
${ data.related
.slice(0,3)
.map(p => `<p><a href="{ p.link }">${ p.title }</a></p>`)
.join('') }
</nav>
</aside>
` : ''}
postrenderMeta() #
A processPostRender hook that inserts a Publican <meta name="generator"> tag into the head of every HTML page.