From ead24d57321f965db99178336e9a72c03c966bd1 Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Tue, 16 Oct 2018 18:59:36 +0200 Subject: [PATCH] search: prepend fuzzy filename matches Prefer similar url over contents. --- search.php | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/search.php b/search.php index e1da846..5bd03bb 100644 --- a/search.php +++ b/search.php @@ -1,10 +1,31 @@ $limit) { + shuffle($results); + array_splice($results, $limit); +} + +$cmd = "git ls-files -- $path"; +exec($cmd, $ls); +if ($ls) { + # order files by similarity to query + $ls = array_combine($ls, array_map(function ($row) use ($Page) { + $row = preg_replace('{(?:^|/)index\.html$}', '', $row); + return similar_text($row, $Page) - strlen($row) / 8; + }, $ls)); + arsort($ls); + + # prepend best match, replace unless duplicate + array_unshift($results, key($ls)); + $results = array_unique($results); + array_splice($results, $limit); +} -if (!$results) { +elseif (!$results) { $results = ['index.html']; } -- 2.30.0