aboutsummaryrefslogtreecommitdiff
path: root/pages/search.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'pages/search.tsx')
-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>