Update kirbytext to add ids to headers automatically?

Does anybody have any idea on how I could easily update kirbytext to add id tags to headers automatically? This would allow linking to specific sections of a page without having to manually add the markdown extra syntax to every header tag.

Maybe kirbytext post filters http://getkirby.com/docs/advanced/kirbytext or a plugin function, I don’t think you want to use JS?

The problem with JS is that when you link to it, if the js hasn’t triggered yet it won’t work.

You could make your own heading Kirbytags.

Like: (h1: Text goes here align: right class: huge)

Here’s a gist for what I’m doing. If you already have a lot of content or don’t want custom tags for headers, post filters might be your best bet.

Generating id’s based on the text is pretty trivial, but just in case, here’s what I’m using, probably not the most efficient way. If there’s a built in function to do this, please do tell. :smile:

$id = str::slug(str::short(str::unhtml($tag->attr($tagName)), 35), '-');

Very cool solution, but I really need to stick with standard Markdown syntax for this.

Then Kirbytext post-filters are the best way. :slight_smile:

That does look like the way to go if I can figure how.

How to implement such filters is described in the docs. In your case, you would search for heading elements and generate and insert the ID in the opening tag.

Yeah, I found the docs, but they weren’t immediately making sense to me in regards to doing what I want to do. But I think it’s already clearing up in my head, so it shouldn’t be too hard.

If there are any questions left or you are getting stuck, feel free to ask. :smiley:

I’ve built it and it’s working. A couple of tweaks and I’ll share it.

For someone searching for such a plugin, @fitzage implemented it and wrote about it here: Plugin to make header tags linkable.