I wonder if it’s possible to have some kind of automatic headline list from my markdown text…
for example what i am trying to accomplish is, when writing a very long article with multiple h2, h3, h4 markdown tags to have an automated way to list some sort of index based on headlines so a viewer can jump right into the topic of choice.
so basically the script is reading the $page->text() field and identifies each
headline2
headline3
headline4
tag and generates a list with id hopping links towards each headline.
Yes, that is possible. The Kirby site uses this to display a table of contents above the docs pages.
The code that does this is the toc plugin. Every occurrence of (toc) inside your Kirbytext content will then be replaced dynamically. You can extend the plugin to work with h3 and h4.
i tried that. looks like what i am searching for. can this also work in form of implementing it somewhere within my template rather than using kirbytext (toc)?
The Kirbytext post filter needs access to the Kirbytext content. What you can do is to prepend the table of contents instead of replacing the placeholder inside the filter. Then you wouldn’t need the placeholder in the content.
If the table of contents needs to be somewhere else on the page, you can modify the function to be not a filter but a normal function and pass the rendered Kirbytext content as an argument manually.
That’s a good question that came to my mind too. I couldn’t figure it out although getkirby.com does have the id-attributes. They are not within the textfile, anyone an idea how they are generated? @sashtown?
Works perfectly for me, thank you @sashtown. @carstengrimm: Did you change the jquery selectors to match your html code? My headings are in .main-content not in .text so I changed the code. I added German Umlauts too:
And well, the field in the text file, that contains the headlines, in this case the text field. A .txt file usually contains several fields, like the title, a text field, maybe a date field etc.