Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
Topic closed
[request] Table of Contents generation
I’m not sure if this plugin already exists or needs to be written. I’d be happy to hack it up, but do let me know if somebody wants to work on it properly, or better yet, it already exists:
I need a way to automatically generate a table of contents for an article. The plugin would extract h1, h2, h3 etc. tags and automatically add internal anchors besides them. It would work just like table of contents on Wikipedia articles. Ideally, you could choose the top header level (so, e.g. h1’s don’t get included in the TOC because one uses h1 for site title). E.g. if article looked like this:
<h1>This is a h1 header</h1>
Some text
<h2>This is a h2 subheader</h2>
More text
<h2>Another subtitle</h2>
More text
It would generate a TOC like:
<ul>
<li>This is a h1 header</li>
<ul>
<li>This is a h2 subheader</li>
<li>Another subtitle</li>
</ul>
</ul>
So, does it exist and if not, is somebody willing to work on it?
Offline
Re: [request] Table of Contents generation
maybe cbs_article_index is what you’re looking for :-)
TXP Builders – finely-crafted code, design and txp
Offline
Re: [request] Table of Contents generation
That looks like it! It’s a bit old though, what state is it in, do you know?
Offline
Re: [request] Table of Contents generation
Oddly enough, I just finished — yesterday — making my own version of this. I haven’t posted it here or on textpattern.org (yet), but if you are interested You can see it here. (Edit: also available at Textpattern Resources, here.) (Edit: now has a proper support thread.)
It does allow heading-level filtering, but the reverse of what you describe (e.g., you can set it to ignore h4, h5, and h6 headings). There’s no need to filter for site title and article title, because the plugin only looks at article body fields.
It also makes nested list output, and offers a choice of relative vs. absolute URLs.
[Edit, 2008.1.29] — fixed bug whereby headings containing tags were not always handled correctly
Last edited by jsoo (2008-02-04 12:25:03)
Code is topiary
Offline
Re: [request] Table of Contents generation
Jsoo, right on! I will give it a try in a couple of days (I’d like to replace the index plugin, because it seemed unmaintained) and let you know if I run into any trouble :)
Offline
Re: [request] Table of Contents generation
Jsoo, I gave it a try and have a problem with nested lists. I described the problem in detail on the resources page
Nice job otherwise!
Offline
Re: [request] Table of Contents generation
Thanks Dejan — I have fixed this; please use version 0.1.2 (available here).
With the change, soo_toc
no longer necessarily generates valid HTML if the article uses a wacky heading structure (e.g., jump directly from h2
to h4
). I am not sure that I care about this enough to “fix” it.
[Edit] — Well, fixed it anyway. Now up to version 0.1.3.
Last edited by jsoo (2008-02-04 01:28:05)
Code is topiary
Offline
Re: [request] Table of Contents generation
Jsoo, great work, thanks! You can check it out in use, e.g. In this article
Offline
Re: [request] Table of Contents generation
cabrilo wrote:
Jsoo, great work, thanks! You can check it out in use, e.g. In this article
Nice. One thing I noticed, though, is that if I click a link in the TOC and then try to return to the top by using the browser “go back” command, nothing happens. I don’t know if this is particular to mootools or general to js navigation (or particular to my browser).
soo_toc now has an official support thread.
Code is topiary
Offline
Re: [request] Table of Contents generation
jsoo wrote:
cabrilo wrote:
Jsoo, great work, thanks! You can check it out in use, e.g. In this article
Nice. One thing I noticed, though, is that if I click a link in the TOC and then try to return to the top by using the browser “go back” command, nothing happens. I don’t know if this is particular to mootools or general to js navigation (or particular to my browser).
soo_toc now has an official support thread.
Thanks for that. I suspect it has something to do with mootools. I should be switching to jquery relatively soon though, and will see if that fixes the problem.
Offline
Pages: 1
Topic closed