--- freelinking.module.old	2007-12-27 23:03:30.000000000 +0100
+++ freelinking.module	2008-01-06 17:39:29.000000000 +0100
@@ -37,6 +37,8 @@ function freelinking_perm() {
 
 function freelinking_page($thetitle = NULL) {
   if ($thetitle) { // find the matching title
+    preg_match("/freelinking\/(.*)$/",$_GET['q'],$match);
+    $thetitle=$match[1];
     $freelink = _freelinking_make_link($thetitle);
     drupal_goto($freelink['path'], $freelink['args'] ? $freelink['args'] : '');
   }
@@ -51,10 +53,10 @@ function freelinking_page($thetitle = NU
         $rows[$i][] = '<a class="freelink" href="' . $freelink->path . '">' . $flpair->path . '</a>';
       }
       else { // a freelink
-        $fltargetnid = _freelinking_exists($freelink->phrase);
+        $fltargetnpath = _freelinking_exists($freelink->phrase);
         $freelink = _freelinking_make_link($freelink->phrase);
         if ($fltargetnid) {
-          $link = l(t('see this content'), drupal_get_path_alias('node/' . $fltargetnid));
+          $link = l(t('see this content'), $fltargetnpath);
         }
         else { // content not found, show link to create
           $link = '<a href=' . url($freelink['path'], $freelink['args']) . '>' . t('create this content') . '</a>';
@@ -280,7 +282,7 @@ function _freelinking_do_filtering($text
     }
 
     else if ($allowcamelcase) { // it's a CamelCase, expressions are a bit simpler
-      $pattern = '/\b' . $wikiword . '\b(?![^<]*>)/';
+      $pattern = '/(?<=[\s,:.])' . $wikiword . '(?=[\s,:.])(?![^<]*>)/';
       $phrase = $wikiword; // consistency for the db
       $freelink = $wikiword; // also for the db
       $replacement = l($wikiword, 'freelinking/' . urlencode($wikiword));
@@ -314,7 +316,7 @@ function _freelinking_store($phrase, $pa
 function _freelinking_exists($thetitle) { // helper function for freelinking_page
   // looks through the db for nodes matching $title. Returns the nid if such a node exists, otherwise, returns 0
   $title = urldecode($thetitle);
-  $query = "SELECT nid FROM {node} WHERE title = '%s'";
+  $query = "SELECT nid FROM {node} WHERE INSTR(title,'%s') ORDER BY LENGTH(title) ASC";
   $noderestrict = variable_get('freelinking_restriction', 'none');
   if ($noderestrict != 'none') { // need to add the where clause
     $query .= " AND type = '%s'";
@@ -323,20 +325,36 @@ function _freelinking_exists($thetitle) 
   else { // no restriction. query is fine but db_query doesn't need the extra argument
     $result = db_query($query, $title);
   }
-// FIXME ***
-  while ($node = db_fetch_object($result)) { // only one, I hope... what if there's more than one?
-    $nid = $node->nid;
+// Alwys take the first, shortest, most accurate match
+  $node = db_fetch_object($result);
+  $nid = $node->nid;
+  
+  if (!empty($nid)) {
+    return drupal_get_path_alias('node/' . $nid);
   }
-  return (empty($nid) ? 0 : $nid);
+  
+  $query = "SELECT dst FROM {url_alias} WHERE INSTR(dst,'%s') ORDER BY LENGTH(dst) ASC";
+  $noderestrict = variable_get('freelinking_restriction', 'none');
+//    if ($noderestrict != 'none') { // need to add the where clause
+//      $query .= " AND type = '%s'";
+//      $result = db_query($query, $title, $noderestrict);
+//    }
+//    else { // no restriction. query is fine but db_query doesn't need the extra argument
+  $result = db_query($query, $title);
+//    }
+// Take first (shortest - most accurate) result
+  $node = db_fetch_object($result);
+  $dst = $node->dst;
+  return (empty($dst) ? 0 : $dst);
 }
 
 
 function _freelinking_make_link($thetitle) { // helper function for freelinking_page
   global $user;
   // Returns a link to a node named $thetitle if found, or a link to new content otherwise.
-  $nid = _freelinking_exists($thetitle); 
-  if ($nid) { // the node exists, set the path to go there
-    $freelink['path'] = 'node/' . $nid;
+  $npath = _freelinking_exists($thetitle); 
+  if ($npath) { // the node exists, set the path to go there
+    $freelink['path'] = $npath;
   }
   else { // node doesn't exist, set path to create it
     switch (variable_get('freelinking_notfound', 'no access search')) {
