-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathloadAndFormatCollection.ts
95 lines (83 loc) · 2.69 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import path from 'node:path'
import { getCollection } from 'astro:content'
import { readOutExif } from '@/lib/exif'
import config from '@config/blog.config'
import type {
ArticleEntry,
BlogEntry,
LinkEntry,
PhotoEntry
} from '@config/content.schema'
import { getSlug } from './getSlug'
import { sortPosts } from './sortPosts'
//
// Main loader for all collections content.
// ---
// Astro's `getCollection()` is never called
// from components, but this helper method instead.
//
export async function loadAndFormatCollection(
name: 'articles'
): Promise<ArticleEntry[]>
export async function loadAndFormatCollection(
name: 'links'
): Promise<LinkEntry[]>
export async function loadAndFormatCollection(
name: 'photos'
): Promise<PhotoEntry[]>
export async function loadAndFormatCollection(
name: 'articles' | 'links' | 'photos'
): Promise<ArticleEntry[] | LinkEntry[] | PhotoEntry[]> {
let postsCollection = (await getCollection(name)) as BlogEntry[]
// filter out drafts, but only in production
if (import.meta.env.PROD) {
postsCollection = postsCollection.filter(({ data }) => data.draft !== true)
}
for await (const post of postsCollection) {
//
// use date from frontmatter, or grab from folder path
//
const date = post.data.date
? post.data.date
: new Date(post.id.split('/')[0].substring(0, 10))
//
// construct slug from folder or file name
//
const slug = getSlug(`${post.collection}/${post.id}`)
post.slug = slug
const githubLink = `${config.repoContentPath}/${post.collection}/${post.id}`
post.data.githubLink = githubLink
post.data.date = date
//
// extract exif & iptc data from photos
//
if (post.collection === 'photos') {
const isProd = import.meta.env.PROD
// Get the absolute image path from post.data.image
// to read exif from
//
// production image.src:
// `/_astro/filename.hash.jpg`
// development image.src:
// `/@fs/absolute/system/path/project/src/content/photos/postSlug/filename.jpg?origWidth=3873&origHeight=2796&origFormat=jpg`
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 sortedPosts = sortPosts(postsCollection as unknown as BlogEntry[])
if (name === 'articles') {
return sortedPosts as ArticleEntry[]
}
if (name === 'photos') {
return sortedPosts as PhotoEntry[]
}
return sortedPosts as LinkEntry[]
}