use heading identifiers in TOC links

reasoning: if headings have identifiers, they are probably more useful
anchors than the automatically generated anchors we build in the toc
plugin. this can happen if, for example, you use the `multimarkdown`
plugin, which inserts `id` tags for every header it encounters. this
also leverages the `headinganchors` plugin nicely.

keeps backwards-compatibility with old toc-generated #indexXhY
anchors.
master
Antoine Beaupré 2017-04-12 15:42:42 -04:00 committed by Simon McVittie
parent 55ae3c7368
commit b5b48d5bb5
1 changed files with 7 additions and 4 deletions

View File

@ -59,13 +59,16 @@ sub format (@) {
my $liststarted=0; my $liststarted=0;
my $indent=sub { "\t" x $curlevel }; my $indent=sub { "\t" x $curlevel };
$p->handler(start => sub { $p->handler(start => sub {
my $tagname=shift; my ($tagname, $text, $attr) = @_;
my $text=shift;
if ($tagname =~ /^h(\d+)$/i) { if ($tagname =~ /^h(\d+)$/i) {
my $level=$1; my $level=$1;
my $anchor="index".++$anchors{$level}."h$level"; my $anchor="index".++$anchors{$level}."h$level";
$page.="$text<a name=\"$anchor\"></a>"; $page.="$text<a name=\"$anchor\"></a>";
# if the heading already has a unique ID, use that instead in TOC
if ($attr->{id}) {
$anchor = $attr->{id};
}
# Unless we're given startlevel as a parameter, # Unless we're given startlevel as a parameter,
# take the first header level seen as the topmost level, # take the first header level seen as the topmost level,
# even if there are higher levels seen later on. # even if there are higher levels seen later on.
@ -124,7 +127,7 @@ sub format (@) {
else { else {
$page.=$text; $page.=$text;
} }
}, "tagname, text"); }, "tagname, text, attr");
$p->handler(default => sub { $page.=join("", @_) }, "text"); $p->handler(default => sub { $page.=join("", @_) }, "text");
$p->parse($content); $p->parse($content);
$p->eof; $p->eof;