-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.astro
55 lines (52 loc) · 1.55 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
48
49
50
51
52
53
54
55
---
import type { CollectionEntry } from 'astro:content'
import PhotoTeaser from '@/components/PhotoTeaser.astro'
import PostTeaser from '@/components/PostTeaser/index.astro'
import { getAllPostsForSearch } from '@/lib/astro'
import Fuse from 'fuse.js'
import styles from './index.module.css'
type Props = {
post: CollectionEntry<'articles' | 'photos' | 'links'>
isPhotos?: boolean
}
const { post, isPhotos } = Astro.props as Props
const allPosts = await getAllPostsForSearch()
const allPostsWithoutCurrent = allPosts?.filter(
(post) => post.slug !== Astro.props.post.slug
) as CollectionEntry<'articles' | 'photos' | 'links'>[]
// 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
})
const relatedPosts = fuse
// https://www.fusejs.io/examples.html#extended-search
.search(
`${post?.data?.tags?.join(' | ')} | "${post?.data?.lead}" | "${
post?.data?.title
}"`
)
.map((result) => result.item)
.slice(0, 6)
---
<section class={`${styles.relatedPosts} ${isPhotos ? styles.photos : ''}`}>
<h1 class={styles.title}>Related</h1>
<ul>
{
relatedPosts?.map((post) => (
<li>
{post.collection === 'photos' ? (
<PhotoTeaser post={post as CollectionEntry<'photos'>} />
) : (
<PostTeaser
post={post as CollectionEntry<'articles' | 'links'>}
hideDate
/>
)}
</li>
))
}
</ul>
</section>