forked from craigary/nobelium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract component for Notion page rendering
- Loading branch information
1 parent
e683ec0
commit b3187d1
Showing
6 changed files
with
178 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import dynamic from 'next/dynamic' | ||
import { NotionRenderer as Renderer } from 'react-notion-x' | ||
|
||
// Lazy-load some heavy components | ||
const components = { | ||
// Code block | ||
Code: dynamic(() => { | ||
return import('react-notion-x/build/third-party/code').then(async module => { | ||
// Additional prismjs syntax | ||
await Promise.all([ | ||
import('prismjs/components/prism-markup-templating'), | ||
import('prismjs/components/prism-markup'), | ||
import('prismjs/components/prism-bash'), | ||
import('prismjs/components/prism-c'), | ||
import('prismjs/components/prism-cpp'), | ||
import('prismjs/components/prism-csharp'), | ||
import('prismjs/components/prism-docker'), | ||
import('prismjs/components/prism-java'), | ||
import('prismjs/components/prism-js-templates'), | ||
import('prismjs/components/prism-coffeescript'), | ||
import('prismjs/components/prism-diff'), | ||
import('prismjs/components/prism-git'), | ||
import('prismjs/components/prism-go'), | ||
import('prismjs/components/prism-graphql'), | ||
import('prismjs/components/prism-handlebars'), | ||
import('prismjs/components/prism-less'), | ||
import('prismjs/components/prism-makefile'), | ||
import('prismjs/components/prism-markdown'), | ||
import('prismjs/components/prism-objectivec'), | ||
import('prismjs/components/prism-ocaml'), | ||
import('prismjs/components/prism-python'), | ||
import('prismjs/components/prism-reason'), | ||
import('prismjs/components/prism-rust'), | ||
import('prismjs/components/prism-sass'), | ||
import('prismjs/components/prism-scss'), | ||
import('prismjs/components/prism-solidity'), | ||
import('prismjs/components/prism-sql'), | ||
import('prismjs/components/prism-stylus'), | ||
import('prismjs/components/prism-swift'), | ||
import('prismjs/components/prism-wasm'), | ||
import('prismjs/components/prism-yaml') | ||
]) | ||
return module.Code | ||
}) | ||
}), | ||
// Database block | ||
Collection: dynamic(() => { | ||
return import('react-notion-x/build/third-party/collection').then(module => module.Collection) | ||
}), | ||
// Equation block & inline variant | ||
Equation: dynamic(() => { | ||
return import('react-notion-x/build/third-party/equation').then(module => module.Equation) | ||
}), | ||
// PDF (Embed block) | ||
Pdf: dynamic(() => { | ||
return import('react-notion-x/build/third-party/pdf').then(module => module.Pdf) | ||
}, { ssr: false }), | ||
// Tweet block | ||
Tweet: dynamic(() => { | ||
return import('react-tweet-embed').then(module => { | ||
const { TweetEmbed } = module | ||
return function Tweet ({ id }) { | ||
return <TweetEmbed tweetId={id} options={{ theme: 'dark' }} /> | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
const mapPageUrl = id => `https://www.notion.so/${id.replace(/-/g, '')}` | ||
|
||
/** | ||
* Notion page renderer | ||
* | ||
* A wrapper of react-notion-x/NotionRenderer with predefined `components` and `mapPageUrl` | ||
* | ||
* @param props - Anything that react-notion-x/NotionRenderer supports | ||
*/ | ||
export default function NotionRenderer (props) { | ||
return ( | ||
<Renderer | ||
components={components} | ||
mapPageUrl={mapPageUrl} | ||
{...props} | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import PropTypes from 'prop-types' | ||
import Image from 'next/image' | ||
import BLOG from '@/blog.config' | ||
import useTheme from '@/lib/theme' | ||
import { BlockMapProvider } from '@/lib/blockMap' | ||
import formatDate from '@/lib/formatDate' | ||
import TagItem from '@/components/TagItem' | ||
import NotionRenderer from '@/components/NotionRenderer' | ||
import TableOfContents from '@/components/TableOfContents' | ||
|
||
/** | ||
* A post renderer | ||
* | ||
* @param {PostProps} props | ||
* | ||
* @typedef {object} PostProps | ||
* @prop {object} post - Post metadata | ||
* @prop {object} blockMap - Post block data | ||
* @prop {string} emailHash - Author email hash (for Gravatar) | ||
*/ | ||
export default function Post (props) { | ||
const { post, blockMap, emailHash } = props | ||
const { dark } = useTheme() | ||
|
||
return ( | ||
<BlockMapProvider blockMap={blockMap}> | ||
<article> | ||
<h1 className="font-bold text-3xl text-black dark:text-white"> | ||
{post.title} | ||
</h1> | ||
{post.type[0] !== 'Page' && ( | ||
<nav className="flex mt-7 items-start text-gray-500 dark:text-gray-400"> | ||
<div className="flex mb-4"> | ||
<a href={BLOG.socialLink || '#'} className="flex"> | ||
<Image | ||
alt={BLOG.author} | ||
width={24} | ||
height={24} | ||
src={`https://gravatar.com/avatar/${emailHash}`} | ||
className="rounded-full" | ||
/> | ||
<p className="ml-2 md:block">{BLOG.author}</p> | ||
</a> | ||
<span className="block"> / </span> | ||
</div> | ||
<div className="mr-2 mb-4 md:ml-0"> | ||
{formatDate(post.date?.start_date || post.createdTime, BLOG.lang)} | ||
</div> | ||
{post.tags && ( | ||
<div className="flex flex-nowrap max-w-full overflow-x-auto article-tags"> | ||
{post.tags.map(tag => ( | ||
<TagItem key={tag} tag={tag}/> | ||
))} | ||
</div> | ||
)} | ||
</nav> | ||
)} | ||
<div className="-mt-4 relative"> | ||
<NotionRenderer | ||
recordMap={blockMap} | ||
fullPage={false} | ||
darkMode={dark} | ||
/> | ||
<div className="absolute left-full inset-y-0"> | ||
{/* `65px` is the height of expanded nav */} | ||
{/* TODO: Remove the magic number */} | ||
<TableOfContents className="sticky" style={{ top: '65px' }}/> | ||
</div> | ||
</div> | ||
</article> | ||
</BlockMapProvider> | ||
) | ||
} | ||
|
||
Post.propTypes = { | ||
post: PropTypes.object.isRequired, | ||
blockMap: PropTypes.object.isRequired, | ||
emailHash: PropTypes.string.isRequired | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.