zola icon indicating copy to clipboard operation
zola copied to clipboard

Sorting subsections and pages together

Open vigiraud opened this issue 2 years ago • 6 comments

First, thank you very much for Zola, it's a great project and it's incredibly useful.

Feature request

I'd like to sort the content of a section according to weights. Instead of sorting the subsections on one side, and the pages on another, I'd like to sort them together. It would allow me to make a table of content like this for example :

Section
|-- Page (weight: 1)
|-- Page (weight: 2)
|-- Section (weight: 3)
|-- Page (weight: 4)
|-- Section (weight: 5)
|-- Page (weight: 6)

However, there are two separate arrays in section's variables :

// Pages directly in this section. By default, the pages are not sorted. Please set the "sorted_by"
// variable in the _index.md file of the corresponding section to "date" or "weight" for sorting by
// date and weight, respectively.
pages: Array<Page>;
// Direct subsections to this section, sorted by subsections weight
// This only contains the path to use in the `get_section` built-in function to get
// the actual section object if you need it
subsections: Array<String>;

I could find some solution if the weight of the pages and the subsections were accessible as variables, but it does not seem to be the case. Am I missing something ?

Proposed solutions

  • Adding a section variable which would be an array sorting subsections and pages together
  • Or making weights available as variable for both sections and pages

vigiraud avatar Sep 08 '22 22:09 vigiraud

You can already make a table of content without that: https://github.com/getzola/book In practice what you're asking is not going to happen though, sections are not pages

Keats avatar Sep 09 '22 11:09 Keats

I thought there was a way to access values placed in the frontmatter of pages/sections, similar to how you can access values placed in the extra section of the config.toml. Is that incorrect?

c-git avatar Sep 09 '22 18:09 c-git

@Keats Thank you for your quick answer. I know sections are not pages, but rather folders with a _index.md inside. What I have on my website are nested sections. I have 2 or more levels of recursion. In this example : image I'd like to have «Créer du contenu» classed between «Mettre en place le DNS» and «Publier le contenu», however this weight sorting is, apparently, completely impossible as of today.

@c-git If that's the case, it would be great for me.

vigiraud avatar Sep 09 '22 23:09 vigiraud

I thought there was a way to access values placed in the frontmatter of pages/sections,

Most of the values of the front-matter are sent to the template but not all. For example https://github.com/getzola/zola/blob/ad6c83412391dd748d4a9d8f3a0568687e73a447/components/content/src/ser.rs#L138-L158 you can see that we are not passing weight since it's only used internally and there never was a need to expose it.

Keats avatar Sep 10 '22 20:09 Keats

Thanks @Keats, yes I see, you can access them if you put them in extra. I was so sure there was a way I was surprised when I tried it and couldn't figure out how to do it. Must have known you needed to use extra when I tried it the last time. Tested and I can access the values if I put them in extra.

@2lafru I'm not sure if you could put your sorting values in extra. Would that work for you?

eg.

+++
title='My Page'
[extra]
# Put all your custom variables here
my_sort_value=10
+++

Then you'll be able to access my_sort_value to do what you needed.

You can see all things available using {{ __tera_context }}.

c-git avatar Sep 13 '22 23:09 c-git

Yeah, I think it'll probably do the trick. I'll try it out, thank you very much !

vigiraud avatar Sep 14 '22 19:09 vigiraud

@Keats @c-git Just so you know, I implemented this workaround. You can see it live here (notice the menu on the left). Source code is here.

vigiraud avatar Dec 21 '22 12:12 vigiraud

Hey that's great, I'll take a look when I get a chance

c-git avatar Dec 21 '22 13:12 c-git