3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-19 10:29:30 +01:00

misc/git-md-toc: handle sections that are link anchors

This commit is contained in:
Pragmatic Software 2020-01-21 13:11:15 -08:00
parent 6c255b613c
commit d6a77e9480

21
misc/git-md-toc vendored
View File

@ -216,19 +216,34 @@ foreach ( @ARGV ) {
$indent = " " x $depth; $indent = " " x $depth;
my $anchor = lc $title; my $anchor_url;
# handle link titles
if ($title =~ m/\[([^]]+)\]\(([^)]+)\)/) {
$title = $1;
$anchor_url = $2;
}
my $anchor;
if (defined $anchor_url) {
$anchor = $anchor_url;
} else {
$anchor = lc $title;
$anchor =~ s/\s/-/g; $anchor =~ s/\s/-/g;
$anchor =~ s/[^\w-]//g; $anchor =~ s/[^\w-]//g;
$anchor = '#' . $anchor;
$count{$anchor}++; $count{$anchor}++;
$anchor .= ( 1 - $count{$anchor} or "" ); $anchor .= ( 1 - $count{$anchor} or "" );
}
if ($depth >= $min_depth - 1 and $depth <= $max_depth - 1) { if ($depth >= $min_depth - 1 and $depth <= $max_depth - 1) {
if ($use_filename) { if ($use_filename) {
push (@toc, "$indent* [$title]($filename#$anchor)"); push (@toc, "$indent* [$title]($filename$anchor)");
} else { } else {
push (@toc, "$indent* [$title](#$anchor)"); push (@toc, "$indent* [$title]($anchor)");
} }
} }
} }