Publican tag indexes

918 words, 5-minute read

Front matter can specify tags – key words associated with the content:

tags: HTML, CSS, JavaScript

Publican generates paginated tag index pages linking to pages that use those tags. These can be configured and presented in different ways.

Generated index pages #

Publican automatically generates paginated index pages for all tags, e.g. the files (slug) at:

Each of these index pages has a data.pagination object which can be used to display links to other index pages.

Linking to tag indexes #

The post’s data.tags property provides an array of objects with the following properties:

A page can list the tags it uses with links to the associated index using code such as:

${ data?.tags?.map(t => `<p><a href="${ t.link }">${ t.tag }</a></p>`) }

Tag index configuration #

The publican.config.tagPages configuration object controls how tags are indexed. The defaults are:

publican.config.js excerpt

// root tag directory
publican.config.tagPages.root = 'tag';

// number of items per paginated page
publican.config.tagPages.size = 24;

// sort by date order
publican.config.tagPages.sortBy = 'date';

// sort from newest to oldest
publican.config.tagPages.sortOrder = -1;

// use this template unless specified otherwise
publican.config.tagPages.template = 'default.html';

// do not show tag indexes on the menu
publican.config.tagPages.menu = false;

// index pages in sitemaps
publican.config.tagPages.index = 'monthly';

Disabling index pages #

Set publican.config.tagPages to false (or any falsy value) to disable directory index pages.

Index generation #

The publican.config.tagPages.size value is set to 24 by default. Therefore, if 51 posts had been tagged with JavaScript, there will be three index pages:

Changing the value to 12 results in five index pages:

The posts are ordered by date from newest to oldest, but you can change this to any content property, e.g. by priority:

// sort by priority from highest to lowest
publican.config.tagPages.sortBy = 'priority';
publican.config.tagPages.sortOrder = -1;

data.pagination #

Publican provides all index pages with a data.pagination object that has the following properties.

namedescription
.pagean array of page data objects available in the current index page
.pageTotaltotal number of pages
.pageCurrentcurrent page number (zero based)
.pageCurrent1current page number (one based)
.subpageFrom1post number from
.subpageTo1post number to
.hrefBacklink to previous paginated page
.hrefNextlink to next paginated page
.hrefarray of links to all paginated pages

Showing page lists #

The following template code example lists all pages when the data.pagination object is available (it can therefore be added to any template):

${ data?.pagination?.page?.length && `
  <nav class="pagelist">
    ${ data.pagination.page.map(p => `
      <article>
        <a href="${ p.link }">
          <h2>${ p.title }</h2>
        </a>
      </article>
    `) }
  </nav>
`}

Showing index navigation #

The following template code example shows index back and next buttons when the data.pagination object is available:

${ data?.pagination?.hrefBack && `
    <a href="${ data.pagination.hrefBack }">back</a>
`}

${ data?.pagination?.hrefNext && `
    <a href="${ data.pagination.hrefNext }">next</a>
`}

The following template code example shows a list of links to all index pages (the currently active page is not a link):

${ pagination?.href.forEach((page, idx) => {
  return idx == data.pagination.pageCurrent ?
    `<strong>${ idx + 1 }</strong>` :
    `<a href="${ page }">${ idx + 1 }</a>`
}) }

tacs.tag #

tacs.tag is a global Map object of all published posts indexed by the normalized tag reference. Each item returns an ordered array of posts. For example, show links to all posts with the html tag:

${ tacs.tag.get('html').map(
  p => `<p><a href="${ p.link }">${ p.title }</a></p>`
) }

tacs.tagList #

tacs.tagList is a global array of tag objects ordered by highest to lowest usage. Each object has the following properties:

You can use tacs.tagList to create a list of links to all tags. The following content HTML file is rendered to build/tag/index.html and shows a list of links to all tag indexes:

src/content/tag.html

---
title: Post tags
menu: false
description: The following tags have been used on posts across this site.
---

<p>${ data.description }</p>

<ul>
${ tacs?.tagList?.map(t => `
  <li>
    <a href="${ t.link }">
      ${ t.tag } (${ t.count })
    </a>
  </li>
`) }
</ul>