@@ -2,11 +2,13 @@ import { Client } from '@notionhq/client';
22import { config } from '@/config/notion' ;
33import { Post } from '@/models/post.model' ;
44import { QueryDatabaseResponse } from '@notionhq/client/build/src/api-endpoints' ;
5+ import { NotionToMarkdown } from 'notion-to-md' ;
56
67const 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) =>
1517const notionClient = new Client ( {
1618 auth : config . token ,
1719} ) ;
20+ const n2m = new NotionToMarkdown ( { notionClient } ) ;
1821
1922interface 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