Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/neogeek/notion-to-json
Fetch Notion Pages as JSON
https://github.com/neogeek/notion-to-json
json notion
Last synced: 25 days ago
JSON representation
Fetch Notion Pages as JSON
- Host: GitHub
- URL: https://github.com/neogeek/notion-to-json
- Owner: neogeek
- License: mit
- Created: 2021-12-24T05:54:09.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-04-27T04:47:50.000Z (over 2 years ago)
- Last Synced: 2024-09-29T19:03:04.162Z (about 1 month ago)
- Topics: json, notion
- Language: TypeScript
- Homepage:
- Size: 145 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
![](cover.png)
> Fetch Notion Pages as JSON
[![NPM Version](http://img.shields.io/npm/v/notion-to-json.svg?style=flat)](https://www.npmjs.org/package/notion-to-json)
[![Tests](https://github.com/neogeek/notion-to-json/actions/workflows/test.workflow.yml/badge.svg)](https://github.com/neogeek/notion-to-json/actions/workflows/test.workflow.yml)## Install
```bash
$ npm install notion-to-json --save
```## Setup
Create integration at and use the **Internal Integration Token** when requesting pages.
## Usage
```typescript
import { getPageSimple } from 'notion-to-json';(async () => {
const page = await getPageSimple('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', {
api_key: 'secret_xxxxxxxxxxxx'
});console.log(page.title);
console.log(page.blocks);
})();
```### Next.js
```typescript
import type { NextPage } from 'next';
import Head from 'next/head';import sanitizeHtml from 'sanitize-html';
import { getPageSimple } from 'notion-to-json';
import { SupportedBlockTypes } from 'notion-to-json/dist/types';export async function getStaticProps() {
const page = await getPageSimple('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', {
api_key: 'secret_xxxxxxxxxxxx'
});return {
props: {
page
}
};
}const Block = ({
block
}: {
block: { type: string; contents: string | string[] };
}) => {
const SimpleBlockTags = Object.freeze({
[SupportedBlockTypes.paragraph]: 'p',
[SupportedBlockTypes.heading_1]: 'h1',
[SupportedBlockTypes.heading_2]: 'h2',
[SupportedBlockTypes.heading_3]: 'h3',
[SupportedBlockTypes.quote]: 'blockquote',
[SupportedBlockTypes.callout]: 'div'
});if (SimpleBlockTags[block.type]) {
const BlockTag = `${
SimpleBlockTags[block.type]
}` as keyof JSX.IntrinsicElements;return (
);
} else if (
block.type === SupportedBlockTypes.bulleted_list_item ||
block.type === SupportedBlockTypes.numbered_list_item
) {
const BlockTag = `${
block.type === SupportedBlockTypes.numbered_list_item ? 'ol' : 'ul'
}` as keyof JSX.IntrinsicElements;return (
{Array.isArray(block.contents) &&
block.contents.map((item, index) => (
))}
);
} else if (block.type === SupportedBlockTypes.image) {
return ;
}
return null;
};
const NotionPage: NextPage<{
page: {
title: string;
blocks: {
type: string;
contents: string | string[];
}[];
};
}> = ({ page }) => {
return (
{page.title}
{page.title}
<>
{page.blocks.map((block, index) => {
return ;
})}
>
);
};
export default NotionPage;
```