-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.astro
47 lines (41 loc) · 1.24 KB
/
index.astro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
---
import PostTeaser from '@/components/PostTeaser/index.astro'
import { getAllPostsForSearch } from '@/lib/astro'
import type { BlogEntry } from '@config/content.schema'
import Fuse from 'fuse.js'
import styles from './index.module.css'
type Props = {
post: BlogEntry
}
const { post } = Astro.props as Props
const allPosts = await getAllPostsForSearch()
const allPostsWithoutCurrent = allPosts?.filter((p) => p.slug !== post.slug)
// Configure fuse.js
// https://fusejs.io/api/options.html
const fuse = new Fuse(allPostsWithoutCurrent, {
keys: ['data.tags', 'data.title', 'data.lead'],
useExtendedSearch: true,
threshold: 0.6
})
// Get lead text from leadRaw property, checking first if lead exists (only on ArticleEntry)
const leadText = ('leadRaw' in post ? post.leadRaw : undefined) || ''
const relatedPosts = fuse
// https://www.fusejs.io/examples.html#extended-search
.search(
`${post?.data?.tags?.join(' | ')} | "${leadText}" | "${post?.data?.title}"`
)
.map((result) => result.item)
.slice(0, 6)
---
<section class={styles.relatedPosts}>
<h1 class={styles.title}>Related</h1>
<ul>
{
relatedPosts?.map((post) => (
<li>
<PostTeaser post={post} hideDate />
</li>
))
}
</ul>
</section>