forked from transitive-bullshit/nextjs-notion-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfeed.tsx
101 lines (87 loc) · 2.72 KB
/
feed.tsx
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
import type { GetServerSideProps } from 'next'
import { ExtendedRecordMap } from 'notion-types'
import {
getBlockParentPage,
getBlockTitle,
getPageProperty,
idToUuid
} from 'notion-utils'
import RSS from 'rss'
import * as config from '@/lib/config'
import { getSiteMap } from '@/lib/get-site-map'
import { getSocialImageUrl } from '@/lib/get-social-image-url'
import { getCanonicalPageUrl } from '@/lib/map-page-url'
export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
if (req.method !== 'GET') {
res.statusCode = 405
res.setHeader('Content-Type', 'application/json')
res.write(JSON.stringify({ error: 'method not allowed' }))
res.end()
return { props: {} }
}
const siteMap = await getSiteMap()
const ttlMinutes = 24 * 60 // 24 hours
const ttlSeconds = ttlMinutes * 60
const feed = new RSS({
title: config.name,
site_url: config.host,
feed_url: `${config.host}/feed.xml`,
language: config.language,
ttl: ttlMinutes
})
for (const pagePath of Object.keys(siteMap.canonicalPageMap)) {
const pageId = siteMap.canonicalPageMap[pagePath]
const recordMap = siteMap.pageMap[pageId] as ExtendedRecordMap
if (!recordMap) continue
const keys = Object.keys(recordMap?.block || {})
const block = recordMap?.block?.[keys[0]]?.value
if (!block) continue
const parentPage = getBlockParentPage(block, recordMap)
const isBlogPost =
block.type === 'page' &&
block.parent_table === 'collection' &&
parentPage?.id === idToUuid(config.rootNotionPageId)
if (!isBlogPost) {
continue
}
const title = getBlockTitle(block, recordMap) || config.name
const description =
getPageProperty<string>('Description', block, recordMap) ||
config.description
const url = getCanonicalPageUrl(config.site, recordMap)(pageId)
const lastUpdatedTime = getPageProperty<number>(
'Last Updated',
block,
recordMap
)
const publishedTime = getPageProperty<number>('Published', block, recordMap)
const date = lastUpdatedTime
? new Date(lastUpdatedTime)
: publishedTime
? new Date(publishedTime)
: undefined
const socialImageUrl = getSocialImageUrl(pageId)
feed.item({
title,
url,
date,
description,
enclosure: socialImageUrl
? {
url: socialImageUrl,
type: 'image/jpeg'
}
: undefined
})
}
const feedText = feed.xml({ indent: true })
res.setHeader(
'Cache-Control',
`public, max-age=${ttlSeconds}, stale-while-revalidate=${ttlSeconds}`
)
res.setHeader('Content-Type', 'text/xml; charset=utf-8')
res.write(feedText)
res.end()
return { props: {} }
}
export default () => null