--- freelinking.module.old	2005-07-19 09:05:54.000000000 -0400
+++ freelinking.module	2005-09-25 22:46:25.338154920 -0400
@@ -155,6 +155,7 @@
             $replacement = l($phrase, 'freelinking/' . rawurlencode(htmlspecialchars(rawurlencode($freelink))));
           }
         }
+
         else if ($allowcamelcase) { // it's a CamelCase, expressions are a bit simpler
           $pattern = '/\b' . $wikiword . '\b/';
           $phrase = $wikiword; // consistency for the db
@@ -162,12 +163,24 @@
           $replacement = l($wikiword, 'freelinking/' . urlencode($wikiword));
         }
         $text = preg_replace($pattern, $replacement, $text);
+        
+//FIX should only store on node save, not preview.
         $query = "SELECT phrase, target FROM {freelinking} WHERE phrase = '%s' AND target = '%s'";
         if ( !db_num_rows(db_query($query, $phrase, $freelink))) { // not in the db
           $query = "INSERT INTO {freelinking} (phrase, target) VALUES ('%s', '%s')";
           $result = db_query($query, $phrase, $freelink);
         } // endif row not found in table
+
       } // foreach wikiword
+
+//Now repair href inside href
+      $repair1 = '/<a href="?<a href=.*"? >/';
+      $repair2 = '/<\/a>"?>/';
+      $reparation1 = '<a href="';
+      $reparation2 = '">';
+      $text = preg_replace($repair1, $reparation1, $text);
+      $text = preg_replace($repair2, $reparation2, $text);
+
       return $text;
       break;
 
