Root links

296 words, 2-minute read

By default, Publican assumes the build directory is the root path of your website. The page rendered to build/index.html is therefore the home page of the site.

publican.config.js excerpt

// root path
publican.config.root = '/';

However, you may want to build files to a sub-directory of your site, e.g. /blog/. Each post’s data.link would then be incorrect.

The site’s root path can be set by changing the config.root value:

publican.config.js excerpt

// root path
publican.config.root = '/blog/';

Links to the root page would then become /blog/ rather than / in next links, back links, pagination, tag lists, navigation menus, etc.

If you want to link to a specific page in content or templates, you can use a ${ tacs.root } expression to get the root path:

<a href="${ tacs.root }post/article-one/">link to Article one</a>

Unfortunately, ${ tacs.root } cannot be used in markdown because it confuses the HTML converter:

<!-- this will fail in markdown! -->
[link to Article one](${ tacs.root }post/article-one/)

To fix this problem, you could add a string replacement option to your configuration file which replaces all references to −−ROOT−− with the server’s root path:

publican.config.js excerpt

// root path
publican.config.root = '/blog/';

publican.config.replace.set(
  '−−ROOT−−',
  publican.config.root
);

This allows you to create links in markdown that use the root path, e.g.

* [Home](−−ROOT−−)
* [About us](−−ROOT−−about/)
* [Contact us](−−ROOT−−about/contact/)