Skip to content

Commit

Permalink
Merge pull request #157 from acelaya/feature/search-improvements
Browse files Browse the repository at this point in the history
Replace lunr with fuse.js for posts search
  • Loading branch information
acelaya authored Oct 17, 2023
2 parents 909eb43 + 3c5984e commit 1f8608b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 103 deletions.
82 changes: 9 additions & 73 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
"bootstrap": "^3.4.1",
"date-fns": "^2.30.0",
"disqus-react": "^1.1.5",
"feed": "^4.2.2",
"fuse.js": "^6.6.2",
"html-entities": "^2.4.0",
"lunr": "^2.3.9",
"markdown-it": "^13.0.2",
"photoswipe": "^5.4.2",
"react": "^18.2.0",
Expand All @@ -33,11 +32,9 @@
"react-tweet-embed": "^2.0.0"
},
"devDependencies": {
"@types/lunr": "^2.3.5",
"@types/markdown-it": "^13.0.2",
"@types/node": "^20.5.8",
"@types/react": "18.2.28",
"react-lunr": "^1.1.0",
"typescript": "^5.2.2"
}
}
15 changes: 8 additions & 7 deletions src/components/Search.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type { FC } from 'react';
import type { Post } from '../utils/posts.ts';
import { useCallback, useState } from 'react';
import { useLunr } from 'react-lunr';
import { useCallback, useMemo, useState } from 'react';
import { PostHint } from './post/PostHint.tsx';
import Fuse from 'fuse.js';

export interface SearchProps {
index: object;
posts: Record<string, Post>;
posts: Post[];
}

const loadSearchFromQueryParams = () => {
Expand All @@ -23,13 +22,15 @@ const updateSearchInQueryParams = (newSearchValue: string) => {
window.history.replaceState(null, '', `?${params.toString()}`);
};

export const Search: FC<SearchProps> = ({ index, posts }) => {
export const Search: FC<SearchProps> = ({ posts }) => {
const [searchValue, setSearchValue] = useState<string>(loadSearchFromQueryParams);
const updateSearch = useCallback((newSearchValue: string) => {
setSearchValue(newSearchValue);
updateSearchInQueryParams(newSearchValue);
}, []);
const results = useLunr(searchValue, index, posts);
const fuse = useMemo(() => new Fuse(posts, { keys: ['body', 'title'],
}), [posts])
const results = useMemo(() => fuse.search(searchValue), [fuse, searchValue]);

return (
<>
Expand All @@ -54,7 +55,7 @@ export const Search: FC<SearchProps> = ({ index, posts }) => {
{searchValue !== '' && <p className="text-center">No results found</p>}
</>
)}
{results.map((post, index) => <PostHint key={index} post={post as Post} />)}
{results.map((post, index) => <PostHint key={index} post={post.item} />)}
</div>
</section>
</>
Expand Down
21 changes: 2 additions & 19 deletions src/pages/search.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---
import lunr from 'lunr';
import { Container } from '../components/Container';
import { SectionTitle } from '../components/SectionTitle';
import { Search } from '../components/Search';
Expand All @@ -8,30 +7,14 @@ import { getAllPosts } from '../utils/posts';
const posts = await getAllPosts().then((posts) =>
// Get rid of the `render` function, which cannot be serialized
posts.map(({ render, ...post }) => ({
title: post.data.title,
...post,
}))
(posts as any[]).map(({ render, ...post }) => post)
);
const index = lunr(function () {
// Set which of the post fields have meaningful content to index
this.field('title');
this.field('body');
// Lunr requires an `id` field, which posts already include
posts.forEach(post => this.add(post));
})
const serializedIndex = index.toJSON();
const postMap = posts.reduce((acc, post) => {
acc[post.id] = post;
return acc;
}, {});
---

<Layout url="/search/">
<Container className="search-section">
<SectionTitle>Search</SectionTitle>

<Search index={serializedIndex} posts={postMap} client:only="react" />
<Search posts={posts} client:only="react" />
</Container>
</Layout>

0 comments on commit 1f8608b

Please sign in to comment.