Working search if you provide your own file list

This commit is contained in:
Alexandra
2024-10-16 00:52:22 -06:00
parent 80620ebe9e
commit 69757f3ff7
7 changed files with 68 additions and 11 deletions

3
.env
View File

@@ -1 +1,2 @@
PORT=8062
PORT=8062
FUZZY_LENIENCY=0.2

6
lib/loadfiles.js Normal file
View File

@@ -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
}

15
lib/search.js Normal file
View File

@@ -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)
}

15
package-lock.json generated
View File

@@ -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",

View File

@@ -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"

View File

@@ -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}.`)
console.log(`Listening on ${process.env.PORT}.`)
console.log(`Loaded ${fileList.length} known files.`)

View File

@@ -10,7 +10,7 @@ _ / / / _ /_/ /_ / _ / / __/ / / / /_
/_/ /_/ _\__, / /_/ /_/ \___//_/ /_/\__/
/____/
</pre>
<input type="text" class="w-50 form-control bg-dark text-white" name="q">
<input type="text" class="w-50 form-control bg-dark text-white" name="q" value="<%= query %>"">
<button type="submit" class="btn btn-secondary">Search</button>
</div>
</div>
@@ -23,7 +23,29 @@ _ / / / _ /_/ /_ / _ / / __/ / / / /_
<th>Category</th>
<th>Size</th>
<th>Date</th>
<th>Match %</th>
</tr>
<% for (let x = 0; x < results.length; x++) { %>
<tr>
<td>
<a href="<%= results[x].item.path %>">
<%= results[x].item.name %>
</a>
</td>
<td>
</td>
<td>
<%= results[x].item.size %>
</td>
<td>
<%= results[x].item.date %>
</td>
<td>
<%= 1 - results[x].score %>%
</td>
</tr>
<% } %>
</table>
</div>
</div>