Can the page title be added to the TOC?

Hi,

I am testing out Zola for a blog but I have some difficulties making the page title and the TOC work together. So for each article I have a title in the front matter so that it can be displayed on the home page with some short summary / description. In the page template I added the page title as h1 before the page content. However, when using the page TOC the page title is not included because it is not a title in the content.

I tried to concat the page title with the TOC like this:

{% set toc = [page.title] %}
{% set toc = toc | concat(with=page.toc) %}

But this does not work because the page title does not have a permalink like the other headings in the TOC.

The other thing I could do is to include the title in the markdown file as a level 1 title. This would solve the TOC problem, however I didn’t see a way to get the first title from the content and treat it as the page title. So I would have to duplicate the page title in the front matter and then as an actual title

Is there a way to achieve this easily, maybe I missed something?

And if it is not, maybe we can add the possibility for Zola to extract the first title of the content and consider it the page title? Or an option to add the page title to the toc?


Also, I think I found a bug when trying to do this?

The snippet above works, but when you try to do the same on one line you get an error:

{% set toc = [page.title] | concat(with=page.toc) %}
Error: 
* Failed to parse "page.html"
  --> 17:51
   |
17 |                         {% set toc = [page.title] | concat(with=page.toc) %}␊
   |                                                   ^---
   |
   = expected `%}` or `-%}`

For the error, it’s probably just Tera not handling array assignements and filters in the grammar.

The reference for the ToC implementation was Wikipedia which doesn’t repeat the page title in its ToC so I don’t think there will be an easy way to add it. The way you have done it will work if you are careful and don’t try to access any property (eg you can do if header.permalink etc).
Extracting the first title could be done but I think it’s pretty niche, what’s the usecase there?

Thanks for the quick reply!

The way you have done it will work if you are careful and don’t try to access any property (eg you can do if header.permalink etc).

Indeed that’s a good solution, and I just realized that I actually don’t need to put the title in the TOC at all, I can just manually add it in the template before the for loop. I was just so focused on the first way I tried doing it that I forgot to take a step back to find the solution. Thanks!

Extracting the first title could be done but I think it’s pretty niche, what’s the usecase there?

The use case was that it would show up in the TOC and I could still use it in the templates by accessing the page.title attribute. It was mainly to avoid duplicating the title as the first heading in the markdown. But considering the solution above I don’t really need it anymore :slight_smile:

I guess it could be useful if you ever consider making the front matter optional.

For the error, it’s probably just Tera not handling array assignements and filters in the grammar.

Should I file a bug report?

Edit: Made a pull request :slight_smile: