Publican public methods

426 words, 3-minute read

The Publican object provides the following public methods that can be used in the publican.config.js configuration files.

.addContent() #

Content can also be passed to Publican as a string in the configuration file. This may be useful if you are retrieving data from a Content Management System API rather than using the file system.

To add virtual content, call:

publican.addContent( <filename>, <content> );

prior to running the build process (await publican.build();).

The filename follows the directory structure rules and determines the type of content. For example, to add a markdown page:

publican.config.js

publican.addContent(
  'article/virtual-post.md', `
---
title: Virtual post
---
This is a virtual post!
`);

.addTemplate() #

Templates can be passed to Publican as a string in the configuration file. This may be useful if you want to create custom templates or partials using conditions or other logic.

To add a virtual template, call:

publican.addTemplate( <filename>, <content> );

The filename is relative to the template location. The following example adds a template which shows an HTML <blockquote> section when a quote value is set in front matter.

publican.config.js excerpt

publican.addTemplate(
  '_partials/blockquote.html',
  '${ data.quote ? `<blockquote>${ data.quote }</blockquote>` : "" }'
);

It can be used in any other template with an include() expression:

default.html excerpt

<main>
  ${ include('_partials/blockquote.html') }
  ${ data.content }
</main>

.clean() #

An asynchronous function that removes all files from the build directory. The .clean() method should be called prior to running .build() and ensures old files will not be present.

publican.config.js

// import Publican
import { Publican } from 'publican';

// create Publican object
const publican = new Publican();

// clear build directory
await publican.clean();

// build site
await publican.build();

The .clean() method works cross-platform so you do not require Node.js directory handling modules or alternative npm scripts.

.build() #

The main asynchronous function to start a build. It should be called after all options are set at the end of the configuration file.

publican.config.js excerpt

// build site
await publican.build();

When watch mode is enabled, Publican will monitor content and template files and automatically rebuild again.