Skip to content

Commit e3d9304

Browse files
committed
working kinda
1 parent bbb5055 commit e3d9304

4 files changed

Lines changed: 169 additions & 10 deletions

File tree

frontend/main/package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/main/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"next-seo": "^4.28.1",
4040
"next-themes": "^0.0.15",
4141
"nightwind": "^1.1.12",
42+
"notion-to-md": "^2.3.3",
4243
"prism-react-renderer": "^1.2.1",
4344
"react": "^17.0.2",
4445
"react-calendly": "^2.2.2",

frontend/main/src/pages/[...permalink].tsx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ import {
2121
} from '@/services/sanity.server';
2222
import { GetStaticPaths, GetStaticProps, InferGetStaticPropsType } from 'next';
2323
import { StripeProduct } from '@/models/stripe.model';
24+
import {
25+
getPurrfectStreamBlocksBySlug,
26+
getPurrfectStreamPageMarkdown,
27+
queryPurrfectStreamByReleased,
28+
queryPurrfectStreamBySlug,
29+
} from '@/services/notion.server';
2430

2531
interface StaticPropsResult {
2632
site: Site;
@@ -39,7 +45,6 @@ export const getStaticPaths: GetStaticPaths = async () => {
3945
for (const postType of [
4046
PostType.post,
4147
PostType.tutorial,
42-
PostType.podcast,
4348
PostType.page,
4449
PostType.course,
4550
]) {
@@ -54,6 +59,19 @@ export const getStaticPaths: GetStaticPaths = async () => {
5459
});
5560
}
5661
}
62+
63+
// Moved podcasts to notion
64+
let notionPosts = await queryPurrfectStreamByReleased(10000);
65+
for (const post of notionPosts.results) {
66+
if (post.slug) {
67+
paths.push({
68+
params: {
69+
permalink: ['podcast', post.slug],
70+
},
71+
});
72+
}
73+
}
74+
5775
return {
5876
paths,
5977
fallback: true,
@@ -148,7 +166,12 @@ export const getStaticProps: GetStaticProps<StaticPropsResult> = async ({
148166
slug,
149167
});
150168
} else {
151-
post = await getPostBySlugService({ preview, type, slug });
169+
// Moved to Notion June 2022
170+
if (type == PostType.podcast) {
171+
post = await getPurrfectStreamPageMarkdown(slug);
172+
} else {
173+
post = await getPostBySlugService({ preview, type, slug });
174+
}
152175
}
153176
}
154177

frontend/main/src/services/notion.server.ts

Lines changed: 129 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import { Client } from '@notionhq/client';
22
import { config } from '@/config/notion';
33
import { Post } from '@/models/post.model';
44
import { QueryDatabaseResponse } from '@notionhq/client/build/src/api-endpoints';
5+
import { NotionToMarkdown } from 'notion-to-md';
56

67
const slugify = (str: string) =>
78
str
89
.toLowerCase()
910
.trim()
11+
.replace('.', '-')
1012
.replace(/[^\w\s-]/g, '')
1113
.replace(/[\s_-]+/g, '-')
1214
.replace(/^-+|-+$/g, '');
@@ -15,6 +17,7 @@ const slugify = (str: string) =>
1517
const notionClient = new Client({
1618
auth: config.token,
1719
});
20+
const n2m = new NotionToMarkdown({ notionClient });
1821

1922
interface NotionPosts extends Omit<QueryDatabaseResponse, 'results'> {
2023
results: Post[];
@@ -55,7 +58,6 @@ export const queryPurrfectStreamByReleased = async (
5558
},
5659
],
5760
});
58-
5961
return {
6062
...raw,
6163
results: raw.results.map((q: any) => {
@@ -72,14 +74,133 @@ export const queryPurrfectStreamByReleased = async (
7274
: null,
7375
},
7476
_type: 'podcast',
75-
slug: slugify(
76-
`${q.properties.Season.number}.${
77-
q.properties.Episode.number
78-
} - ${q?.properties?.Name?.title
79-
.map((t: any) => t.plain_text)
80-
.join('')}`
81-
),
77+
slug: q?.properties?.slug?.rich_text
78+
.map((t: any) => t.plain_text)
79+
.join(''),
80+
excerpt: q?.properties?.excerpt?.rich_text
81+
.map((t: any) => t.plain_text)
82+
.join(''),
8283
};
8384
}),
8485
} as unknown as NotionPosts;
8586
};
87+
88+
export const queryPurrfectStreamBySlug = async (slug: string) => {
89+
let raw = await notionClient.databases.query({
90+
database_id: config.purrfectStreamsDb,
91+
filter: {
92+
and: [
93+
{
94+
property: 'slug',
95+
rich_text: {
96+
contains: slug,
97+
},
98+
},
99+
{
100+
property: 'Status',
101+
select: {
102+
equals: 'Released',
103+
},
104+
},
105+
{
106+
property: 'Episode',
107+
number: {
108+
is_not_empty: true,
109+
},
110+
},
111+
],
112+
},
113+
sorts: [
114+
{
115+
property: 'Season',
116+
direction: 'descending',
117+
},
118+
{
119+
property: 'Episode',
120+
direction: 'descending',
121+
},
122+
],
123+
});
124+
return {
125+
...raw,
126+
results: raw.results.map((q: any) => {
127+
return {
128+
...q,
129+
title: `${q.properties.Season.number}.${
130+
q.properties.Episode.number
131+
} - ${q?.properties?.Name?.title
132+
.map((t: any) => t.plain_text)
133+
.join('')}`,
134+
coverPhoto: {
135+
public_id: q?.cover?.external?.url
136+
? q?.cover?.external?.url.split('upload/').at(1)
137+
: null,
138+
},
139+
_type: 'podcast',
140+
slug: q?.properties?.slug?.rich_text
141+
.map((t: any) => t.plain_text)
142+
.join(''),
143+
excerpt: q?.properties?.excerpt?.rich_text
144+
.map((t: any) => t.plain_text)
145+
.join(''),
146+
};
147+
}),
148+
};
149+
};
150+
151+
export const getPurrfectStreamPageById = async (page_id: string) => {
152+
let raw = await notionClient.pages.retrieve({
153+
page_id,
154+
});
155+
return {
156+
...raw,
157+
} as unknown as Post;
158+
};
159+
160+
export const getPurrfectStreamBlocksBySlug = async (slug: string) => {
161+
const dbObject = await queryPurrfectStreamBySlug(slug);
162+
if (!dbObject.results.length) {
163+
return null;
164+
}
165+
let raw = await notionClient.blocks.children.list({
166+
block_id: dbObject.results[0].id,
167+
});
168+
console.log(raw);
169+
return {
170+
...raw,
171+
} as unknown as Post;
172+
};
173+
174+
export const getPurrfectStreamPageMarkdown = async (slug: string) => {
175+
let raw = await queryPurrfectStreamBySlug(slug);
176+
if (!raw.results.length) {
177+
return null;
178+
}
179+
raw.results.map((q: any) => {
180+
return {
181+
...q,
182+
title: `${q.properties.Season.number}.${
183+
q.properties.Episode.number
184+
} - ${q?.properties?.Name?.title.map((t: any) => t.plain_text).join('')}`,
185+
coverPhoto: {
186+
public_id: q?.cover?.external?.url
187+
? q?.cover?.external?.url.split('upload/').at(1)
188+
: null,
189+
},
190+
_type: 'podcast',
191+
slug: q?.properties?.slug?.rich_text
192+
.map((t: any) => t.plain_text)
193+
.join(''),
194+
excerpt: q?.properties?.excerpt?.rich_text
195+
.map((t: any) => t.plain_text)
196+
.join(''),
197+
};
198+
});
199+
200+
const mdblocks = await n2m.pageToMarkdown(raw.results[0].id);
201+
const content = n2m.toMarkdownString(mdblocks);
202+
return {
203+
...raw.results[0],
204+
content,
205+
};
206+
};

0 commit comments

Comments
 (0)