-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetPosts.ts
73 lines (64 loc) · 1.84 KB
/
getPosts.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
import { DateTime } from 'luxon';
import { Octokit } from 'octokit';
import markdownToHtml from '@/lib/markdownToHtml';
export type Post = {
slug: string,
date: DateTime,
title: string,
summary: string,
reading_time: string,
tags: string[],
content: string,
author: {
name?: string,
avatar?: string,
},
};
export async function getPosts(): Promise<Post[]> {
const posts = await get();
return posts.filter(p => !p.tags.includes('docs'));
}
export async function getPost(slug: string): Promise<null | Post> {
const posts = await get();
return posts.find(post => post.slug === slug) || null;
}
export async function getPostsByTag(tag: string): Promise<Post[]> {
const posts = await get(tag);
return posts;
}
export async function getDocs(): Promise<Post[]> {
const posts = await get('docs');
return posts;
}
async function get(labels?: string): Promise<Post[]> {
const octokit = new Octokit();
const issues = (await octokit.rest.issues.listForRepo({
owner: 'maffin-io',
repo: 'maffin-blog',
creator: 'argaen',
labels,
headers: {
accept: 'application/vnd.github+json',
},
})).data.filter(issue => !issue.pull_request && !issue.title.includes('[draft]'));
return Promise.all(issues.map(async (issue) => {
if (!issue.body) {
throw new Error(`Missing body in issue ${issue.title}`);
}
const { content: htmlContent, metadata } = await markdownToHtml(issue.body);
return {
slug: metadata.slug,
date: DateTime.fromISO(issue.created_at),
title: issue.title,
summary: metadata.summary,
reading_time: metadata.reading_time,
// @ts-ignore
tags: issue.labels.map(label => label.name),
content: htmlContent,
author: {
name: issue.user?.name || issue.user?.login,
avatar: issue.user?.avatar_url,
},
};
}));
}