forked from transitive-bullshit/nextjs-notion-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNotionPage.tsx
349 lines (308 loc) · 10.1 KB
/
NotionPage.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import * as React from 'react'
import dynamic from 'next/dynamic'
import Image from 'next/image'
import Link from 'next/link'
import { useRouter } from 'next/router'
import cs from 'classnames'
import { PageBlock } from 'notion-types'
import { formatDate, getBlockTitle, getPageProperty } from 'notion-utils'
import BodyClassName from 'react-body-classname'
import { NotionRenderer } from 'react-notion-x'
// import TweetEmbed from 'react-tweet-embed'
import { useSearchParam } from 'react-use'
import * as config from '@/lib/config'
import * as types from '@/lib/types'
import { mapImageUrl } from '@/lib/map-image-url'
import { getCanonicalPageUrl, mapPageUrl } from '@/lib/map-page-url'
import { searchNotion } from '@/lib/search-notion'
import { useDarkMode } from '@/lib/use-dark-mode'
import { Footer } from './Footer'
import { GitHubShareButton } from './GitHubShareButton'
import { ReactUtterances } from './ReactUtterances'
// import { ReactCusdis } from 'react-cusdis'
import { Loading } from './Loading'
import { NotionPageHeader } from './NotionPageHeader'
import { Page404 } from './Page404'
import { PageAside } from './PageAside'
import { PageHead } from './PageHead'
import styles from './styles.module.css'
// -----------------------------------------------------------------------------
// dynamic imports for optional components
// -----------------------------------------------------------------------------
const Code = dynamic(() =>
import('react-notion-x/build/third-party/code').then(async (m) => {
// add / remove any prism syntaxes here
await Promise.all([
import('prismjs/components/prism-markup-templating.js'),
import('prismjs/components/prism-markup.js'),
import('prismjs/components/prism-bash.js'),
import('prismjs/components/prism-c.js'),
import('prismjs/components/prism-cpp.js'),
import('prismjs/components/prism-csharp.js'),
import('prismjs/components/prism-docker.js'),
import('prismjs/components/prism-java.js'),
import('prismjs/components/prism-js-templates.js'),
import('prismjs/components/prism-coffeescript.js'),
import('prismjs/components/prism-diff.js'),
import('prismjs/components/prism-git.js'),
import('prismjs/components/prism-go.js'),
import('prismjs/components/prism-graphql.js'),
import('prismjs/components/prism-handlebars.js'),
import('prismjs/components/prism-less.js'),
import('prismjs/components/prism-makefile.js'),
import('prismjs/components/prism-markdown.js'),
import('prismjs/components/prism-objectivec.js'),
import('prismjs/components/prism-ocaml.js'),
import('prismjs/components/prism-python.js'),
import('prismjs/components/prism-reason.js'),
import('prismjs/components/prism-rust.js'),
import('prismjs/components/prism-sass.js'),
import('prismjs/components/prism-scss.js'),
import('prismjs/components/prism-solidity.js'),
import('prismjs/components/prism-sql.js'),
import('prismjs/components/prism-stylus.js'),
import('prismjs/components/prism-swift.js'),
import('prismjs/components/prism-wasm.js'),
import('prismjs/components/prism-yaml.js')
])
return m.Code
})
)
const Collection = dynamic(() =>
import('react-notion-x/build/third-party/collection').then(
(m) => m.Collection
)
)
const Equation = dynamic(() =>
import('react-notion-x/build/third-party/equation').then((m) => m.Equation)
)
const Pdf = dynamic(
() => import('react-notion-x/build/third-party/pdf').then((m) => m.Pdf),
{
ssr: false
}
)
const Modal = dynamic(
() =>
import('react-notion-x/build/third-party/modal').then((m) => {
m.Modal.setAppElement('.notion-viewport')
return m.Modal
}),
{
ssr: false
}
)
// const Tweet = ({ id }: { id: string }) => {
// return <TweetEmbed tweetId={id} />
// }
const propertyLastEditedTimeValue = (
{ block, pageHeader },
defaultFn: () => React.ReactNode
) => {
if (pageHeader && block?.last_edited_time) {
return `Last updated ${formatDate(block?.last_edited_time, {
month: 'long'
})}`
}
return defaultFn()
}
const propertyDateValue = (
{ data, schema, pageHeader },
defaultFn: () => React.ReactNode
) => {
if (pageHeader && schema?.name?.toLowerCase() === 'published') {
const publishDate = data?.[0]?.[1]?.[0]?.[1]?.start_date
if (publishDate) {
return `${formatDate(publishDate, {
month: 'long'
})}`
}
}
return defaultFn()
}
const propertyTextValue = (
{ schema, pageHeader },
defaultFn: () => React.ReactNode
) => {
if (pageHeader && schema?.name?.toLowerCase() === 'author') {
return <b>{defaultFn()}</b>
}
return defaultFn()
}
export const NotionPage: React.FC<types.PageProps> = ({
site,
recordMap,
error,
pageId
}) => {
const router = useRouter()
const lite = useSearchParam('lite')
const components = React.useMemo(
() => ({
nextImage: Image,
nextLink: Link,
Code,
Collection,
Equation,
Pdf,
Modal,
Header: NotionPageHeader,
propertyLastEditedTimeValue,
propertyTextValue,
propertyDateValue
}),
[]
)
// lite mode is for oembed
const isLiteMode = lite === 'true'
const { isDarkMode } = useDarkMode()
const siteMapPageUrl = React.useMemo(() => {
const params: any = {}
if (lite) params.lite = lite
const searchParams = new URLSearchParams(params)
return mapPageUrl(site, recordMap, searchParams)
}, [site, recordMap, lite])
const keys = Object.keys(recordMap?.block || {})
const block = recordMap?.block?.[keys[0]]?.value
// const isRootPage =
// parsePageId(block?.id) === parsePageId(site?.rootNotionPageId)
const isBlogPost =
block?.type === 'page' && block?.parent_table === 'collection'
const showTableOfContents = !!isBlogPost
const minTableOfContentsItems = 3
const pageAside = React.useMemo(
() => (
<PageAside block={block} recordMap={recordMap} isBlogPost={isBlogPost} />
),
[block, recordMap, isBlogPost]
)
const footer = React.useMemo(() => <Footer />, [])
if (router.isFallback) {
return <Loading />
}
if (error || !site || !block) {
return <Page404 site={site} pageId={pageId} error={error} />
}
const title = getBlockTitle(block, recordMap) || site.name
console.log('notion page', {
isDev: config.isDev,
title,
pageId,
rootNotionPageId: site.rootNotionPageId,
recordMap
})
if (!config.isServer) {
// add important objects to the window global for easy debugging
const g = window as any
g.pageId = pageId
g.recordMap = recordMap
g.block = block
}
const canonicalPageUrl =
!config.isDev && getCanonicalPageUrl(site, recordMap)(pageId)
const socialImage = mapImageUrl(
getPageProperty<string>('Social Image', block, recordMap) ||
(block as PageBlock).format?.page_cover ||
config.defaultPageCover,
block
)
// const forhit = `https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=${canonicalPageUrl}&count_bg=%2379C83D&title_bg=%23555555&icon=&icon_color=%23E7E7E7&title=hits&edge_flat=true`
const socialDescription =
getPageProperty<string>('Description', block, recordMap) ||
config.description
let comments: React.ReactNode = null
// let pageAside: React.ReactChild = (
// <div>
// <br></br>
// <img src={forhit} />
// </div>
// )
// only display comments and page actions on blog post pages
if (isBlogPost) {
if (config.utterancesGitHubRepo) {
comments = (
<ReactUtterances
repo={config.utterancesGitHubRepo}
issueMap='issue-term'
issueTerm='title'
theme={isDarkMode ? 'photon-dark' : 'github-light'}
/>
)
// } else if (config.cusdis) {
// if (!config.cusdis.appId) {
// console.warn('[cusdis]', 'appId is required')
// }
// comments = darkMode.value ? (
// <ReactCusdis
// style={{
// width: '100%',
// marginTop: '30px'
// }}
// attrs={{
// host: config.cusdis.host || 'https://cusdis.com',
// appId: config.cusdis.appId,
// pageId: pageId,
// pageTitle: title,
// pageUrl: canonicalPageUrl,
// theme: 'dark'
// }}
// ></ReactCusdis>
// ) : (
// <ReactCusdis
// style={{
// width: '100%',
// marginTop: '30px'
// }}
// attrs={{
// host: config.cusdis.host || 'https://cusdis.com',
// appId: config.cusdis.appId,
// pageId: pageId,
// pageTitle: title,
// pageUrl: canonicalPageUrl,
// theme: 'light'
// }}
// ></ReactCusdis>
// )
}
}
return (
<>
<PageHead
pageId={pageId}
site={site}
title={title}
description={socialDescription}
image={socialImage}
url={canonicalPageUrl}
/>
{isLiteMode && <BodyClassName className='notion-lite' />}
{isDarkMode && <BodyClassName className='dark-mode' />}
<NotionRenderer
bodyClassName={cs(
styles.notion,
pageId === site.rootNotionPageId && 'index-page'
)}
darkMode={isDarkMode}
components={components}
recordMap={recordMap}
rootPageId={site.rootNotionPageId}
rootDomain={site.domain}
fullPage={!isLiteMode}
previewImages={!!recordMap.preview_images}
showCollectionViewDropdown={false}
showTableOfContents={showTableOfContents}
minTableOfContentsItems={minTableOfContentsItems}
defaultPageIcon={config.defaultPageIcon}
defaultPageCover={config.defaultPageCover}
defaultPageCoverPosition={config.defaultPageCoverPosition}
mapPageUrl={siteMapPageUrl}
mapImageUrl={mapImageUrl}
pageFooter={comments}
searchNotion={config.isSearchEnabled ? searchNotion : null}
pageAside={pageAside}
footer={footer}
/>
<GitHubShareButton />
</>
)
}