1
1
import { defineCollection , getCollection , z , type CollectionEntry } from 'astro:content' ;
2
2
// import { glob } from 'astro/loaders'; // not available with legacy API
3
3
4
+ const schema = z . object ( {
5
+ title : z . string ( ) ,
6
+ date : z . coerce . date ( ) ,
7
+ description : z . string ( ) . optional ( ) ,
8
+ draft : z . boolean ( ) . optional ( ) ,
9
+ tags : z . array ( z . string ( ) ) . optional ( )
10
+ } ) ;
11
+
4
12
const quirks = defineCollection ( {
5
13
type : 'content' ,
6
14
// loader: glob({ pattern: '**\/[^_]*.md', base: './src/quirks' }),
7
- schema : z . object ( {
8
- title : z . string ( ) ,
9
- date : z . coerce . date ( ) ,
10
- description : z . string ( ) . optional ( ) ,
11
- draft : z . boolean ( ) . optional ( ) ,
12
- tags : z . array ( z . string ( ) ) . optional ( )
13
- } )
15
+ schema,
14
16
} ) ;
15
17
export const collections = { quirks } ;
16
18
19
+ type Post = CollectionEntry < "quirks" > ;
20
+
17
21
export async function getAllPosts ( ) {
18
- const posts = await getCollection ( "quirks" , ( { data } ) => {
22
+ const posts : Array < Post > = await getCollection ( "quirks" , ( { data } : { data : z . infer < typeof schema > } ) => {
19
23
if ( import . meta. env . PROD ) { // production, so hide drafts and scheduled posts
20
24
if ( data . draft === true ) {
21
25
console . log ( ' > skipping draft article, as env is production (title: %s)' , data . title ) ;
@@ -40,11 +44,11 @@ export async function getAllPosts() {
40
44
} )
41
45
}
42
46
43
- export async function getPostsGroupedByTags ( ) {
47
+ export async function getPostsGroupedByTags ( ) : Promise < Map < string , Array < Post > > > {
44
48
const posts = await getAllPosts ( ) ;
45
49
return posts . reduce (
46
- ( allTags : Map < string , Array < CollectionEntry < "quirks" > > > , post ) => {
47
- const postTags = post . data . tags || [ ] ;
50
+ ( allTags : Map < string , Array < Post > > , post : Post ) => {
51
+ const postTags : Array < string > = post . data . tags || [ ] ;
48
52
postTags . forEach ( ( tag ) => {
49
53
if ( ! allTags . has ( tag ) ) {
50
54
allTags . set ( tag , [ ] ) ;
0 commit comments