diff --git a/src/data.js b/src/data.js index e9d0903..8665f31 100644 --- a/src/data.js +++ b/src/data.js @@ -8,7 +8,7 @@ const fs = require("fs") export function searchdata(term, type) { // Searches users and posts for any matches let search_results = {"posts": [], "users": []}; // Search users - if (type == 'post' || type == 'any') { + if (type.includes('post')) { let list = getdata('posts'); list.forEach((element,index) => { if (typeof element.deleted == 'undefined' || element.deleted == false) { @@ -24,18 +24,18 @@ export function searchdata(term, type) { // Searches users and posts for any mat }; }); } - if (type == 'user' || type == 'any') { + if (type.includes('user')) { let list = getdata('users'); list.forEach((element,index) => { if (typeof element.deleted == 'undefined' || element.deleted == false) { if (element.username.includes(term)) { - search_results.posts.push(element) + search_results.users.push(element) } else if (element.prettyname.includes(term)) { - search_results.posts.push(element) + search_results.users.push(element) } else if (element.description.includes(term)) { - search_results.posts.push(element) + search_results.users.push(element) }; }; }); diff --git a/src/routes/form_actions.js b/src/routes/form_actions.js index 46967e9..4984a42 100644 --- a/src/routes/form_actions.js +++ b/src/routes/form_actions.js @@ -202,9 +202,12 @@ router.post("/submit_edit_post", (req,res) => { router.get('/search', (req, res) => { const search_term = req.query.q; // 'q' is the parameter name - let search_type = req.query.type; // eg 'any', 'post', 'user' etc... - if (typeof search_type == 'undefined') { // Default to 'any' - search_type = 'any'; + let search_type = req.query.type; // eg 'post', 'user' + if (typeof search_type == 'string') { // Make the search_term an array + search_type = [ search_type ] + } + if (typeof search_type == 'undefined') { // Default to all of the types + search_type = ['user', 'post']; } console.log('searching for: ', search_term); const search_results = data.searchdata(search_term, search_type); // data.searchdata returns an array of search results @@ -213,6 +216,8 @@ router.get('/search', (req, res) => { config, locale, search_results, + search_term, + search_type, }) }); // /search diff --git a/views/headers/site_wide.ejs b/views/headers/site_wide.ejs index 400d8b4..b6abf11 100644 --- a/views/headers/site_wide.ejs +++ b/views/headers/site_wide.ejs @@ -1,5 +1,6 @@ <%= locale.home_page %> <%= locale.site_index %> <%= locale.new_post %> +

<%- config.seperator %> diff --git a/views/partials/search.ejs b/views/partials/search.ejs index 7997dd8..17f2959 100644 --- a/views/partials/search.ejs +++ b/views/partials/search.ejs @@ -1,5 +1,5 @@ - + @@ -7,6 +7,23 @@ + + + + <%- config.seperator %>
<% search_results.posts.forEach((result, index) => { %>