react-notion-x
repository·master·Indexed 26 days ago
https://github.com/notionx/react-notion-xA fast and accurate React renderer for Notion pages. The ecosystem includes react-notion-x for rendering, notion-client for fetching data via an unofficial API, notion-x-to-md for converting pages to GitHub-Flavored Markdown (GFM), and utility packages like notion-types and notion-utils. It supports a wide range of Notion block types, including collections, equations, and embeds, with optional heavyweight components to minimize bundle size.
What's inside react-notion-x
- React Notion X is a fast and accurate React renderer designed to render Notion pages in React applications. It is used to transform Notion data into high-fidelity React components.
Fetch a database's content with getPage
masterWhen passing a database ID to the
getPagemethod, the response object containsblock,collection, andcollection_viewproperties.The
blockproperty is a map where keys are block IDs and values contain metadata (role, value). The order of IDs in the block map follows this pattern:- The collection view ID
- The parent page ID containing the database
- IDs of all child pages inside the database
- All child blocks of each page
Block types can include
header,text,list,media,page, orcollection_view.Optimize Image Loading and Caching
masterNotion-hosted images are often unoptimized and not cacheable. To improve performance, you can:
- Override
mapImageUrl: On theNotionRenderer, override themapImageUrlfunction to route images through a CDN (like Cloudflare Workers) for caching. - Lazy Loading:
NotionRenderersupports lazy image loading with optional low-quality image placeholder (LQIP) previews. - Next.js Integration: Use the
nextImagecomponent prop as described in the Next.js integration guide to leverage Next.js image optimization.
- Override
Optimize Performance with Next.js Components
masterIf you are using Next.js, you can improve performance and SEO by passing
next/image(ornext/legacy/image) andnext/linkto theNotionRenderervia thecomponentsprop. This wraps the Next.js components in a compatibility layer so they can be used in place of standard<img>and<a>tags.Note: Custom image components are currently only enabled with preview images or by setting
forceCustomImagestotrueon theNotionRenderer.import Image from 'next/image' // or import Image from 'next/legacy/image' if you use legacy Image import Link from 'next/link' export default ({ recordMap }) => ( <NotionRenderer recordMap={recordMap} components={{ nextImage: Image, // or nextLegacyImage: LegacyImage, nextLink: Link }} /> )Important notes for using notion-x-to-md
masterAuthentication
No Notion API keys or integrations are required. Ensure your Notion page is set to publicly accessible.
Image URLs
Images hosted by Notion are temporary and will eventually become inaccessible. To ensure permanent image URLs, you should post-process the
ExtendedRecordMapreturned byNotionAPI.getPageto upload images to a permanent storage and map the block IDs to the new URLs.Feature Support
- Markdown Format: Outputs GitHub-Flavored Markdown (GFM).
- Collections (Databases): Supported. All database views are rendered as Markdown tables. Includes support for number, date, expression, and formula formatting.
- Tweet Embeds: Uses
tweet-to-md. Polls are not shown. GIF and video tweets only show the poster image. - Embeds: Videos are shown as links. Iframe embeds (e.g., Maps, Figma, Drive) render as empty.
Install notion-utils
masterInstall the
notion-utilspackage via npm. This package is isomorphic, meaning it is compatible with both Node.js and client-side web environments.npm install notion-utilsInstall notion-x-to-md
masterInstall the
notion-x-to-mdpackage via npm to convert Notion pages to Markdown.npm install notion-x-to-mdInstall notion-types
masterInstall the
notion-typespackage to access TypeScript definitions for core Notion data structures. This package is compatible with both Node.js and browser environments.npm install notion-typesImport third-party modules separately to minimize bundle size
masterTo ensure a minimal bundle size, all modules located in thethird-partyfolder must be imported separately from the corereact-notion-xpackage. These modules contain large, optional dependencies that are not included in the main bundle by default.Use optional heavyweight components
masterTo keep the initial bundle small, heavyweight blocks like Code, Collection, Equation, Modal, and Pdf are not included in the default
NotionRendererexport.To use them:
- Import the component from
react-notion-x/third-party/*(ideally usingnext/dynamicfor lazy loading). - Pass the component to the
componentsprop ofNotionRenderer.
Available third-party components:
CodeCollectionEquationModalPdf
import dynamic from 'next/dynamic' const Code = dynamic(() => import('react-notion-x/third-party/code').then((m) => m.Code) ) const Collection = dynamic(() => import('react-notion-x/third-party/collection').then((m) => m.Collection) ) const Equation = dynamic(() => import('react-notion-x/third-party/equation').then((m) => m.Equation) ) const Pdf = dynamic( () => import('react-notion-x/third-party/pdf').then((m) => m.Pdf), { ssr: false } ) const Modal = dynamic(() => import('react-notion-x/third-party/modal').then((m) => m.Modal), { ssr: false } ) export default ({ recordMap }) => ( <NotionRenderer recordMap={recordMap} components={{ Code, Collection, Equation, Modal, Pdf }} /> )- Import the component from
Install notion-client
masterInstall the
notion-clientpackage via npm. This package is compatible with server-side V8 contexts including Node.js, Deno, and Cloudflare Workers.npm install notion-clientImport required CSS styles
masterTo ensure correct rendering, you must import the core
react-notion-xstyles. Depending on your features, you may also need to import styles for code syntax highlighting (Prism) or equations (KaTeX).Required:
react-notion-x/styles.css
Optional:
prismjs/themes/prism-tomorrow.css(for Code blocks)katex/dist/katex.min.css(for Equations)
// core styles shared by all of react-notion-x (required) import 'react-notion-x/styles.css' // used for code syntax highlighting (optional) import 'prismjs/themes/prism-tomorrow.css' // used for rendering equations (optional) import 'katex/dist/katex.min.css'