diff options
author | lonkaars <l.leblansch@gmail.com> | 2021-04-02 16:17:15 +0200 |
---|---|---|
committer | lonkaars <l.leblansch@gmail.com> | 2021-04-02 16:17:15 +0200 |
commit | 6af043cae506bf5daad306a8dc3ccbb5ca3ab5cf (patch) | |
tree | 5a8de7ca3ebcaedc94a9601800fc11f9e77a3e8e /pages | |
parent | 77943f248166c286bcb6dff826bc2b4e1752fd56 (diff) |
nhentai-ish search working
Diffstat (limited to 'pages')
-rw-r--r-- | pages/search.tsx | 49 |
1 files changed, 46 insertions, 3 deletions
diff --git a/pages/search.tsx b/pages/search.tsx index 3eb46ab..c9b27a5 100644 --- a/pages/search.tsx +++ b/pages/search.tsx @@ -52,6 +52,37 @@ function Posts(props: { posts: Array<Post> }) { </div>; } +function searchFilter(query: string, tags: Array<string>) { + var output = { + query: "", + tags: [] + } + + // remove string literals from tag matching + var queryWithoutLiterals = query.replace(/\".+?\"/g, ""); + + // find tags and remove them from the query + tags.forEach(tag => { + var index = queryWithoutLiterals.indexOf(tag); + if (index == -1) return; + + // remove tag from query + queryWithoutLiterals = + queryWithoutLiterals.substr(0, index) + + queryWithoutLiterals.substr(index + tag.length); + + output.tags.push(tag); + }); + + // add back in the string literals (janky just gets pasted on end) + output.query = + queryWithoutLiterals + " " + + (query.match(/\".+?\"/g) + ?.map(r => r.substr(1, r.length - 2)) + .join(" ") || ""); + return output; +} + export default function SearchPage() { var [posts, setPosts] = useState<PostsInfo>({ posts: [], valid_tags: [] }); var [query, setQuery] = useState("-"); @@ -82,11 +113,23 @@ export default function SearchPage() { })()}, []); useEffect(() => { - if(query.length == 0) { - setVisiblePosts(posts.posts) + var search = searchFilter(query, posts.valid_tags); + + if(search.query.length == 0) { + var results = posts.posts; } else { - setVisiblePosts(fuse.search(query).map(res => res.item)) + var fuseSearch = fuse.search(search.query); + var results = fuseSearch.map(res => res.item); } + + results = results.filter(result => { + for(var i in search.tags) { + if (!result.tags.includes(search.tags[i])) + return false; + } + return true; + }); + setVisiblePosts(results) }, [query]); return <div> |