forked from vercel/platforms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchers.ts
116 lines (104 loc) · 2.17 KB
/
fetchers.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { cache } from "react";
import type { _SiteData } from "@/types";
import prisma from "@/lib/prisma";
import remarkMdx from "remark-mdx";
import { remark } from "remark";
import { serialize } from "next-mdx-remote/serialize";
import { replaceExamples, replaceTweets } from "@/lib/remark-plugins";
export const getSiteData = cache(async (site: string): Promise<_SiteData> => {
let filter: {
subdomain?: string;
customDomain?: string;
} = {
subdomain: site,
};
if (site.includes(".")) {
filter = {
customDomain: site,
};
}
const data = (await prisma.site.findUnique({
where: filter,
include: {
user: true,
posts: {
where: {
published: true,
},
orderBy: [
{
createdAt: "desc",
},
],
},
},
})) as _SiteData;
return data;
});
export const getPostData = cache(async (site: string, slug: string) => {
let filter: {
subdomain?: string;
customDomain?: string;
} = {
subdomain: site,
};
if (site.includes(".")) {
filter = {
customDomain: site,
};
}
const data = await prisma.post.findFirst({
where: {
site: {
...filter,
},
slug,
},
include: {
site: {
include: {
user: true,
},
},
},
});
if (!data) return { notFound: true, revalidate: 10 };
const [mdxSource, adjacentPosts] = await Promise.all([
getMdxSource(data.content!),
prisma.post.findMany({
where: {
site: {
...filter,
},
published: true,
NOT: {
id: data.id,
},
},
select: {
slug: true,
title: true,
createdAt: true,
description: true,
image: true,
imageBlurhash: true,
},
}),
]);
return {
data: {
...data,
mdxSource,
},
adjacentPosts,
};
});
async function getMdxSource(postContents: string) {
// Serialize the content string into MDX
const mdxSource = await serialize(postContents, {
mdxOptions: {
remarkPlugins: [replaceTweets, () => replaceExamples(prisma)],
},
});
return mdxSource;
}