From ffcf7e9b66f86da5eb5caf99cfa6d7ea6270aef8 Mon Sep 17 00:00:00 2001 From: Cris Stringfellow <22254235+crislin2046@users.noreply.github.com> Date: Fri, 17 Dec 2021 22:07:10 +0800 Subject: [PATCH] "Added basic search result view" --- archivist.js | 3 +- libraryServer.js | 73 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 68 insertions(+), 8 deletions(-) diff --git a/archivist.js b/archivist.js index 5b16fdf..3a62dc4 100644 --- a/archivist.js +++ b/archivist.js @@ -729,8 +729,7 @@ export default Archivist; async function search(query) { //return await Flex.searchAsync(query, args.results_per_page); const results = NDX_FTSIndex.search(query); - console.log({query, results}); - return results; + return {query,results}; } async function saveFTS(path) { diff --git a/libraryServer.js b/libraryServer.js index 850399c..1f39de9 100644 --- a/libraryServer.js +++ b/libraryServer.js @@ -53,12 +53,16 @@ function addHandlers() { app.use("/library", express.static(args.library_path())) } - app.get('/search', async (req, res) => { - const resultUrls = await Archivist.search(req.query.query); - const results = resultUrls.map(({docId}) => Archivist.getDetails(docId)); - res.end(JSON.stringify({ - results - }, null, 2)); + app.get('/search(.json)?', async (req, res) => { + const {query, results:resultIds} = await Archivist.search(req.query.query); + const results = resultIds.map(({docId}) => Archivist.getDetails(docId)); + if ( req.path.endsWith('.json') ) { + res.end(JSON.stringify({ + results, query + }, null, 2)); + } else { + res.end(SearchResultView({results, query})); + } }); app.get('/mode', async (req, res) => { @@ -171,3 +175,60 @@ function IndexView(urls) { ` } +function SearchResultView({results, query}) { + return ` + + + Your HTML Library + +

22120

+

Internet Offline Library

+

Archive Results

+

+ Showing results for ${query} +

+
    + ${ + results.map(({url,title}) => ` +
  1. + ${title||url} +
  2. + `).join('\n') + } +
+ ` +} +