[Home]WikiPatches/NoDuplicateTOCAnchors

UseModWiki | WikiPatches | RecentChanges | Preferences

1. Bug Description

Headings with duplicate names produce anchors and a table of contents with links to anchors with duplicate names. The duplicate links cause the page to scroll to the first anchor when clicked in most (probably all?) browsers. Example:

   == # Foo ==
   === # Baz ===
   == # Bar ===
   === # Baz ===
   == # Meep ===
   === # Baz ===

You will have three anchors in the document body named "Baz", and therefore three links to anchors named "Baz" in the table of contents.

2. Bug Fix

The fix simply appends underscores to the anchor name until it is unique. The "Baz" anchors above will be named "Baz", "Baz_", and "Baz__", in that order.

3. Bug Fix Patch

--- wiki.pl     2006-10-27 10:49:06.594459876 -0500
+++ wiki.pl.noduptocanchors     2006-10-27 10:48:49.547535191 -0500
@@ -61,8 +61,8 @@
   %LinkIndex $InterSiteInit $SaveUrlIndex $SaveNumUrlIndex $MainPage
   $OpenPageName @KeptList @IndexList $IndexInit $TableMode
   $q $Now $UserID $TimeZoneOffset $ScriptName $BrowseCode $OtherCode
-  $AnchoredLinkPattern @HeadingNumbers $TableOfContents $QuotedFullUrl
-  $ConfigError $UploadPattern );
+  $AnchoredLinkPattern @HeadingNumbers $TableOfContents %TableOfContentsAnchors
+  $QuotedFullUrl $ConfigError $UploadPattern );

 # == Configuration =====================================================
 $DataDir     = "/tmp/mywikidb"; # Main wiki directory
@@ -2124,6 +2124,9 @@
     $anchor =~ s/_$//;
     # Last ditch effort
     $anchor = '_' . (join '_', @HeadingNumbers) unless $anchor;
+    # Avoid duplicate anchors, which break the table of contents
+    $anchor .= '_' while ($TableOfContentsAnchors{$anchor});
+    $TableOfContentsAnchors{$anchor} = 1;
     $TableOfContents .= $number . &ScriptLink("$OpenPageName#$anchor",$text)
                         . "</dd>\n<dt> </dt><dd>";
     return &StoreHref(" name=\"$anchor\"") . $number;

UseModWiki | WikiPatches | RecentChanges | Preferences
Edit text of this page | View other revisions | Search MetaWiki
Last edited November 8, 2006 6:29 pm by lossage.worldspice.net (diff)
Search: