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
parent
55ae3c7368
commit
b5b48d5bb5
|
@ -59,12 +59,15 @@ sub format (@) {
|
|||
my $liststarted=0;
|
||||
my $indent=sub { "\t" x $curlevel };
|
||||
$p->handler(start => sub {
|
||||
my $tagname=shift;
|
||||
my $text=shift;
|
||||
my ($tagname, $text, $attr) = @_;
|
||||
if ($tagname =~ /^h(\d+)$/i) {
|
||||
my $level=$1;
|
||||
my $anchor="index".++$anchors{$level}."h$level";
|
||||
$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,
|
||||
# take the first header level seen as the topmost level,
|
||||
|
@ -124,7 +127,7 @@ sub format (@) {
|
|||
else {
|
||||
$page.=$text;
|
||||
}
|
||||
}, "tagname, text");
|
||||
}, "tagname, text, attr");
|
||||
$p->handler(default => sub { $page.=join("", @_) }, "text");
|
||||
$p->parse($content);
|
||||
$p->eof;
|
||||
|
|
Loading…
Reference in New Issue