Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2008-01-26 13:44:17

cabrilo
Member
Registered: 2006-06-18
Posts: 18
Website

[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

#2 2008-01-26 14:37:01

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,726
Website

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

#3 2008-01-26 15:10:38

cabrilo
Member
Registered: 2006-06-18
Posts: 18
Website

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

#4 2008-01-28 20:27:44

jsoo
Plugin Author
From: NC, USA
Registered: 2004-11-15
Posts: 1,793
Website

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

#5 2008-02-02 21:33:12

cabrilo
Member
Registered: 2006-06-18
Posts: 18
Website

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

#6 2008-02-03 10:15:01

cabrilo
Member
Registered: 2006-06-18
Posts: 18
Website

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

#7 2008-02-03 14:07:36

jsoo
Plugin Author
From: NC, USA
Registered: 2004-11-15
Posts: 1,793
Website

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

#8 2008-02-04 09:20:06

cabrilo
Member
Registered: 2006-06-18
Posts: 18
Website

Re: [request] Table of Contents generation

Jsoo, great work, thanks! You can check it out in use, e.g. In this article

Offline

#9 2008-02-04 12:31:46

jsoo
Plugin Author
From: NC, USA
Registered: 2004-11-15
Posts: 1,793
Website

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

#10 2008-02-04 12:53:44

cabrilo
Member
Registered: 2006-06-18
Posts: 18
Website

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

Board footer

Powered by FluxBB