Basic search functionality on the frontpage, I want to add support for

more advanced searches like using boolean operators, but right now it's
pretty basic.
This commit is contained in:
2025-10-01 10:40:36 +01:00
parent 8ad8f01043
commit 521dbccc7e
4 changed files with 32 additions and 9 deletions

View File

@@ -8,7 +8,7 @@ const fs = require("fs")
export function searchdata(term, type) { // Searches users and posts for any matches export function searchdata(term, type) { // Searches users and posts for any matches
let search_results = {"posts": [], "users": []}; let search_results = {"posts": [], "users": []};
// Search users // Search users
if (type == 'post' || type == 'any') { if (type.includes('post')) {
let list = getdata('posts'); let list = getdata('posts');
list.forEach((element,index) => { list.forEach((element,index) => {
if (typeof element.deleted == 'undefined' || element.deleted == false) { 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'); let list = getdata('users');
list.forEach((element,index) => { list.forEach((element,index) => {
if (typeof element.deleted == 'undefined' || element.deleted == false) { if (typeof element.deleted == 'undefined' || element.deleted == false) {
if (element.username.includes(term)) { if (element.username.includes(term)) {
search_results.posts.push(element) search_results.users.push(element)
} }
else if (element.prettyname.includes(term)) { else if (element.prettyname.includes(term)) {
search_results.posts.push(element) search_results.users.push(element)
} }
else if (element.description.includes(term)) { else if (element.description.includes(term)) {
search_results.posts.push(element) search_results.users.push(element)
}; };
}; };
}); });

View File

@@ -202,9 +202,12 @@ router.post("/submit_edit_post", (req,res) => {
router.get('/search', (req, res) => { router.get('/search', (req, res) => {
const search_term = req.query.q; // 'q' is the parameter name const search_term = req.query.q; // 'q' is the parameter name
let search_type = req.query.type; // eg 'any', 'post', 'user' etc... let search_type = req.query.type; // eg 'post', 'user'
if (typeof search_type == 'undefined') { // Default to 'any' if (typeof search_type == 'string') { // Make the search_term an array
search_type = 'any'; 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); console.log('searching for: ', search_term);
const search_results = data.searchdata(search_term, search_type); // data.searchdata returns an array of search results 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, config,
locale, locale,
search_results, search_results,
search_term,
search_type,
}) })
}); // /search }); // /search

View File

@@ -1,5 +1,6 @@
<a href="/"><%= locale.home_page %></a> <a href="/"><%= locale.home_page %></a>
<a href="/index/pages"><%= locale.site_index %></a> <a href="/index/pages"><%= locale.site_index %></a>
<a href="<%= config.new_post_url %>"><%= locale.new_post %></a> <a href="<%= config.new_post_url %>"><%= locale.new_post %></a>
<form method="GET" action="/search" style="display: inline"><label>Search: </label><input type="text" placeholder="🔍" name="q"><input type="submit" value="Submit"></form>
<br/> <br/>
<%- config.seperator %> <%- config.seperator %>

View File

@@ -1,5 +1,5 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html lang='<%- config.locale %>>
<head> <head>
<%- include('../partials/head'); %> <%- include('../partials/head'); %>
</head> </head>
@@ -8,6 +8,23 @@
<%- include('../headers/site_wide'); %> <%- include('../headers/site_wide'); %>
</div> </div>
<div id='advanced-search'>
<form method="GET" action="/search">
<label>Search Term:</label>
<input type='text' placeholder='🔍' name='q' value='<%- search_term %>'><br/>
<label>Search for:</label><br/>
<label>Post:</label>
<input type="checkbox" name="type" value="post" <% if (search_type.includes('post')) {%>checked<% } %>><br/>
<label>User:</label>
<input type="checkbox" name="type" value="user" <% if (search_type.includes('user')) {%>checked<% } %>><br/>
<input type="submit" value="Submit">
</form>
</div>
<%- config.seperator %>
<div id='results'> <div id='results'>
<% search_results.posts.forEach((result, index) => { %> <% search_results.posts.forEach((result, index) => { %>
<a href="/post/<%- result.id %>"><%- result.title %></a> <a href="/post/<%- result.id %>"><%- result.title %></a>