aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <l.leblansch@gmail.com>2021-04-02 16:17:15 +0200
committerlonkaars <l.leblansch@gmail.com>2021-04-02 16:17:15 +0200
commit6af043cae506bf5daad306a8dc3ccbb5ca3ab5cf (patch)
tree5a8de7ca3ebcaedc94a9601800fc11f9e77a3e8e
parent77943f248166c286bcb6dff826bc2b4e1752fd56 (diff)
nhentai-ish search working
-rw-r--r--pages/search.tsx49
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>