From d9225fc5ee88e95e4b89af7bf2674355ab3f2204 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Wed, 10 Aug 2022 12:26:03 +0200 Subject: [PATCH] fix(#109): fix README redirection in case of multi-matches --- public/script/app.js | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/public/script/app.js b/public/script/app.js index ff89c08..904aff3 100644 --- a/public/script/app.js +++ b/public/script/app.js @@ -1279,18 +1279,39 @@ angular (res) => { $scope.files = res.data; if ($scope.paths.length == 0 || $scope.paths[0] == "") { - for (let file in $scope.files) { - // redirect to readme + // redirect to readme + const readmeCandidates = {}; + for (const file in $scope.files) { if (file.toLowerCase().indexOf("readme") > -1) { - let uri = $location.url(); - if (uri[uri.length - 1] != "/") { - uri += "/"; - } - - // redirect to readme - $location.url(uri + file); + readmeCandidates[file.toLowerCase()] = file; } } + + const readmePriority = [ + "readme.md", + "readme.txt", + "readme.org", + "readme.1st", + "readme", + ]; + let best_match = null; + for (const p of readmePriority) { + if (readmeCandidates[p]) { + best_match = p; + break; + } + } + if (!best_match && Object.keys(readmeCandidates).length > 0) + best_match = Object.keys(readmeCandidates)[0]; + if (best_match) { + let uri = $location.url(); + if (uri[uri.length - 1] != "/") { + uri += "/"; + } + + // redirect to readme + $location.url(uri + readmeCandidates[best_match]); + } } if (callback) { return callback();