diff --git a/.env b/.env index fac3b2f..0e114c9 100644 --- a/.env +++ b/.env @@ -1 +1,2 @@ -PORT=8062 \ No newline at end of file +PORT=8062 +FUZZY_LENIENCY=0.2 \ No newline at end of file diff --git a/lib/loadfiles.js b/lib/loadfiles.js new file mode 100644 index 0000000..f715bf5 --- /dev/null +++ b/lib/loadfiles.js @@ -0,0 +1,6 @@ +import { readFile } from 'fs/promises'; + +export default async function parseJsonFile(filePath) { + let data = JSON.parse(await readFile(filePath, "utf8")); + return data +} \ No newline at end of file diff --git a/lib/search.js b/lib/search.js new file mode 100644 index 0000000..cda3ac7 --- /dev/null +++ b/lib/search.js @@ -0,0 +1,15 @@ +import Fuse from 'fuse.js' + +export default function fuzzySearch(fileArr, query){ + const leniency = parseInt(process.env.FUZZY_LENIENCY) + const options = { + findAllMatches: true, + threshold: leniency, + ignoreLocation: true, + includeScore: true, + ignoreFieldNorm: true, + keys: ['name'] + } + const fuse = new Fuse(fileArr, options) + return fuse.search(query) +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 955ba43..a388dc6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,7 +8,7 @@ "dotenv": "^16.4.5", "ejs": "^3.1.10", "express": "^4.21.1", - "fuzzystring": "^1.0.2", + "fuse.js": "^7.0.0", "jsdom": "^25.0.1", "node-cron": "^3.0.3", "node-fetch": "^3.3.2" @@ -601,11 +601,14 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/fuzzystring": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/fuzzystring/-/fuzzystring-1.0.2.tgz", - "integrity": "sha512-5L1DG0o2aHPXXeOfs+MBPp/+oZdt8t6GuvXYvfhxdY9n1xnwnY/nHzvUWqsrd8NSiiMaPaiseEQJDg8pwMfLBA==", - "license": "MIT" + "node_modules/fuse.js": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/fuse.js/-/fuse.js-7.0.0.tgz", + "integrity": "sha512-14F4hBIxqKvD4Zz/XjDc3y94mNZN6pRv3U13Udo0lNLCWRBUsrMv2xwcF/y/Z5sV6+FQW+/ow68cHpm4sunt8Q==", + "license": "Apache-2.0", + "engines": { + "node": ">=10" + } }, "node_modules/get-intrinsic": { "version": "1.2.4", diff --git a/package.json b/package.json index 705b64b..9125775 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "dotenv": "^16.4.5", "ejs": "^3.1.10", "express": "^4.21.1", - "fuzzystring": "^1.0.2", + "fuse.js": "^7.0.0", "jsdom": "^25.0.1", "node-cron": "^3.0.3", "node-fetch": "^3.3.2" diff --git a/server.js b/server.js index f53a39e..3b7eaa6 100644 --- a/server.js +++ b/server.js @@ -1,6 +1,11 @@ import getAllFiles from './lib/dirwalk.js' +import parseJsonFile from './lib/loadfiles.js' +import fuzzySearch from './lib/search.js' import 'dotenv/config' import express from 'express' + +var fileList = await parseJsonFile('./filelist.json') + var app = express(); app.set('view engine', 'ejs') @@ -11,10 +16,15 @@ app.get('/', function(req, res) { }) app.get('/search', function(req, res) { + let results = fuzzySearch(fileList, req.query.q) + console.log(results) res.render('pages/index', { - page: 'results' + page: 'results', + query: req.query.q, + results: results }) }) app.listen(process.env.PORT) -console.log(`Listening on ${process.env.PORT}.`) \ No newline at end of file +console.log(`Listening on ${process.env.PORT}.`) +console.log(`Loaded ${fileList.length} known files.`) \ No newline at end of file diff --git a/views/pages/results.ejs b/views/pages/results.ejs index de058fa..90aaeaa 100644 --- a/views/pages/results.ejs +++ b/views/pages/results.ejs @@ -10,7 +10,7 @@ _ / / / _ /_/ /_ / _ / / __/ / / / /_ /_/ /_/ _\__, / /_/ /_/ \___//_/ /_/\__/ /____/ - + @@ -23,7 +23,29 @@ _ / / / _ /_/ /_ / _ / / __/ / / / /_