-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotionPage.tsx
285 lines (245 loc) · 8.02 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
import * as React from 'react'
import Head from 'next/head'
import Link from 'next/link'
import dynamic from 'next/dynamic'
import cs from 'classnames'
import { useRouter } from 'next/router'
import { useSearchParam } from 'react-use'
import BodyClassName from 'react-body-classname'
import useDarkMode from 'use-dark-mode'
import { PageBlock } from 'notion-types'
import { Tweet, TwitterContextProvider } from 'react-static-tweets'
// core notion renderer
import { NotionRenderer, Code, Collection, CollectionRow } from 'react-notion-x'
// utils
import { getBlockTitle } from 'notion-utils'
import { mapPageUrl, getCanonicalPageUrl } from 'lib/map-page-url'
import { mapNotionImageUrl } from 'lib/map-image-url'
import { getPageDescription } from 'lib/get-page-description'
import { getPageTweet } from 'lib/get-page-tweet'
import { searchNotion } from 'lib/search-notion'
import * as types from 'lib/types'
import * as config from 'lib/config'
// components
import { CustomFont } from './CustomFont'
import { Loading } from './Loading'
import { Page404 } from './Page404'
import { PageHead } from './PageHead'
import { PageActions } from './PageActions'
import { Footer } from './Footer'
// import { PageSocial } from './PageSocial'
// import { GitHubShareButton } from './GitHubShareButton'
import { ReactUtterances } from './ReactUtterances'
import styles from './styles.module.css'
// import EyeDrawing from './eye-drawings'
// const Code = dynamic(() =>
// import('react-notion-x').then((notion) => notion.Code)
// )
//
// const Collection = dynamic(() =>
// import('react-notion-x').then((notion) => notion.Collection)
// )
//
// const CollectionRow = dynamic(
// () => import('react-notion-x').then((notion) => notion.CollectionRow),
// {
// ssr: false
// }
// )
// TODO: PDF support via "react-pdf" package has numerous troubles building
// with next.js
// const Pdf = dynamic(
// () => import('react-notion-x').then((notion) => notion.Pdf),
// { ssr: false }
// )
const Equation = dynamic(() =>
import('react-notion-x').then((notion) => notion.Equation)
)
// we're now using a much lighter-weight tweet renderer react-static-tweets
// instead of the official iframe-based embed widget from twitter
// const Tweet = dynamic(() => import('react-tweet-embed'))
const Modal = dynamic(
() => import('react-notion-x').then((notion) => notion.Modal),
{ ssr: false }
)
export const NotionPage: React.FC<types.PageProps> = ({
site,
recordMap,
error,
pageId
}) => {
const router = useRouter()
const lite = useSearchParam('lite')
const params: any = {}
if (lite) params.lite = lite
// lite mode is for oembed
const isLiteMode = lite === 'true'
const searchParams = new URLSearchParams(params)
const darkMode = useDarkMode(true, { classNameDark: 'dark-mode' })
if (router.isFallback) {
return <Loading />
}
const keys = Object.keys(recordMap?.block || {})
const block = recordMap?.block?.[keys[0]]?.value
if (error || !site || !keys.length || !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 siteMapPageUrl = mapPageUrl(site, recordMap, searchParams)
const canonicalPageUrl =
!config.isDev && getCanonicalPageUrl(site, recordMap)(pageId)
// const isRootPage =
// parsePageId(block.id) === parsePageId(site.rootNotionPageId)
const isBlogPost =
block.type === 'page' && block.parent_table === 'collection'
const showTableOfContents = isBlogPost
const minTableOfContentsItems = 3
const socialImage = mapNotionImageUrl(
(block as PageBlock).format?.page_cover || config.defaultPageCover,
block
)
const socialDescription =
getPageDescription(block, recordMap) ?? config.description
let comments: React.ReactNode = null
let pageAside: React.ReactChild = null
// 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={darkMode.value ? 'photon-dark' : 'github-light'}
/>
)
}
const tweet = getPageTweet(block, recordMap)
if (tweet) {
pageAside = <PageActions tweet={tweet} />
}
}
return (
<TwitterContextProvider
value={{
tweetAstMap: (recordMap as any).tweetAstMap || {},
swrOptions: {
fetcher: (id) =>
fetch(`/api/get-tweet-ast/${id}`).then((r) => r.json())
}
}}
>
<PageHead site={site} />
<Head>
<meta property='og:title' content={title} />
<meta property='og:site_name' content={site.name} />
<meta name='twitter:title' content={title} />
<meta property='twitter:domain' content={site.domain} />
{config.twitter && (
<meta name='twitter:creator' content={`@${config.twitter}`} />
)}
{socialDescription && (
<>
<meta name='description' content={socialDescription} />
<meta property='og:description' content={socialDescription} />
<meta name='twitter:description' content={socialDescription} />
</>
)}
{socialImage ? (
<>
<meta name='twitter:card' content='summary_large_image' />
<meta name='twitter:image' content={socialImage} />
<meta property='og:image' content={socialImage} />
</>
) : (
<meta name='twitter:card' content='summary' />
)}
{canonicalPageUrl && (
<>
<link rel='canonical' href={canonicalPageUrl} />
<meta property='og:url' content={canonicalPageUrl} />
<meta property='twitter:url' content={canonicalPageUrl} />
</>
)}
<title>{title}</title>
</Head>
<CustomFont site={site} />
{isLiteMode && <BodyClassName className='notion-lite' />}
<NotionRenderer
bodyClassName={cs(
styles.notion,
pageId === site.rootNotionPageId && 'index-page'
)}
components={{
pageLink: ({
href,
as,
passHref,
prefetch,
replace,
scroll,
shallow,
locale,
...props
}) => (
<Link
href={href}
as={as}
passHref={passHref}
prefetch={prefetch}
replace={replace}
scroll={scroll}
shallow={shallow}
locale={locale}
>
<a {...props} />
</Link>
),
code: Code,
collection: Collection,
collectionRow: CollectionRow,
tweet: Tweet,
modal: Modal,
equation: Equation
}}
recordMap={recordMap}
rootPageId={site.rootNotionPageId}
fullPage={!isLiteMode}
darkMode={darkMode.value}
previewImages={site.previewImages !== false}
showCollectionViewDropdown={false}
showTableOfContents={showTableOfContents}
minTableOfContentsItems={minTableOfContentsItems}
defaultPageIcon={config.defaultPageIcon}
defaultPageCover={config.defaultPageCover}
defaultPageCoverPosition={config.defaultPageCoverPosition}
mapPageUrl={siteMapPageUrl}
mapImageUrl={mapNotionImageUrl}
searchNotion={searchNotion}
pageFooter={comments}
pageAside={pageAside}
footer={
<Footer
isDarkMode={darkMode.value}
toggleDarkMode={darkMode.toggle}
/>
}
/>
{/* <GitHubShareButton /> */}
</TwitterContextProvider>
)
}