-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadAndFormatCollection.ts
51 lines (42 loc) · 1.55 KB
/
loadAndFormatCollection.ts
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
import { getCollection, type CollectionEntry } from 'astro:content'
import { readOutExif } from '@lib/exif'
import path from 'path'
import config from '@config/blog.config'
import { sortPosts } from './sortPosts'
import { getSlug } from './getSlug'
export async function loadAndFormatCollection(
name: 'articles' | 'links' | 'photos'
): Promise<CollectionEntry<'articles' | 'links' | 'photos'>[]> {
let postsCollection = await getCollection(name)
if (import.meta.env.PROD) {
postsCollection = postsCollection.filter(({ data }) => data.draft !== true)
}
for await (const post of postsCollection) {
const date = post.data.date
? post.data.date
: new Date(post.id.split('/')[0].substring(0, 10))
const slug = getSlug(`${post.collection}/${post.id}`)
const githubLink = `${config.repoContentPath}/${post.collection}/${post.id}`
post.slug = slug as CollectionEntry<'articles' | 'links' | 'photos'>['slug']
post.data.date = date
post.data.githubLink = githubLink
//
// Extracts exif & iptc data from photos.
//
if (post.collection === 'photos') {
const isProd = import.meta.env.PROD
const imagePath = isProd
? path.join(
'content',
'photos',
post.id.split('/')[0],
post.data.image.src.split('/')[2].split('.')[0].concat('.jpg')
)
: post.data.image.src.split('?')[0].split('/@fs')[1]
const exif = await readOutExif(imagePath)
post.data.exif = exif
}
}
const posts = sortPosts(postsCollection)
return posts
}