HeaderAnchor plugin

I've written a new plugin called Daizu::Plugin::HeaderAnchor which adds a elements to the headings in all your articles with id attributes, so that you can link to a particular section of a page.

I've put some headings in this blog article, so if you look at the source then you should see things like this, even though the original HTML I'm writing doesn't contain any anchors:

<h3><a id="sec-anchor-names"></a>Anchor names</h3>

name versus id

I decided, at least for now, to use id attributes instead of name ones, to make it easier to migrate to XHTML later without having to use deprecated features. Apparently id anchors do work in all modern browsers.

If necessary I can always add an option later to adjust exactly how the anchors are formatted.

Anchor names

The names for anchors are based on the title of a section (the textual content of the heading itself). I filter it and remove characters other than letters and digits, and put the prefix sec- on the start to make sure it doesn't conflict with identifiers used in the templates (which my plugin doesn't know about of course).

There's a limit of three words in the heading, so the identifiers shouldn't be too long. Also, if there aren't any bits of alphanumeric text which the plugin can find, then it will use an identifier like sec-unnamed.

Avoiding duplicates

If the author puts anchors in their own content then the plugin should handle that reasonably well. It will notice the anchors and avoid using the same ones itself. If that, or similar titles, would cause duplicate identifiers, it will just add numbers on the end to make them unique. It will also avoid adding anchors to headings which already have them, so you can use that to override its choice of identifier (perhaps if you've changed the heading text but don't want to break people's links to it).

< Output configuration | Daizu CMS version 0.1 >