https://github.com/victornpb/notion-2-markdown
(FORK) Convert notion pages, block and list of blocks to markdown (supports nesting)
https://github.com/victornpb/notion-2-markdown
Last synced: 8 months ago
JSON representation
(FORK) Convert notion pages, block and list of blocks to markdown (supports nesting)
- Host: GitHub
- URL: https://github.com/victornpb/notion-2-markdown
- Owner: victornpb
- License: mit
- Fork: true (souvikinator/notion-to-md)
- Created: 2022-05-17T04:02:10.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-06-10T13:52:50.000Z (over 3 years ago)
- Last Synced: 2024-04-29T23:24:06.452Z (over 1 year ago)
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/notion-to-md
- Size: 171 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
> 💡 For better readability and detailed instructions headover to the [wiki](https://github.com/souvikinator/notion-to-md/wiki)
# Notion to Markdown
Convert notion pages, block and list of blocks to markdown (supports nesting) using **[notion-sdk-js](https://github.com/makenotion/notion-sdk-js)**
> **Note:** Before getting started, create [an integration and find the token](https://www.notion.so/my-integrations).
[](https://ko-fi.com/O5O1AFCJR)
## Todo
- [x] heading
- [x] images
- [x] quotes
- [x] links
- [x] bullets
- [x] todo
- [x] inline code
- [x] code block
- [x] strikethrough, underline, bold, italic
- [x] nested blocks
- [x] embeds, bookmarks, videos, files (converted to links)
- [x] Simple tables
- [x] divider
- [x] equation block (converted to code blocks)
- [x] convert returned markdown object to string (`toMarkdownString()`)
- [x] typescript support
- [ ] add tests## Install
```Bash
$ npm install notion-to-md
```## Usage
> **Note:** Details on methods can be found in [API section](https://github.com/souvikinator/notion-to-md#api)
### converting markdown objects to markdown string
This is how the notion page looks for this example:

```js
const { Client } = require("@notionhq/client");
const { NotionToMarkdown } = require("notion-to-md");
// or
// import {NotionToMarkdown} from "notion-to-md";const notion = new Client({
auth: "your integration token",
});// passing notion client to the option
const n2m = new NotionToMarkdown({ notionClient: notion });(async () => {
const mdblocks = await n2m.pageToMarkdown("target_page_id");
const mdString = n2m.toMarkdownString(mdblocks);//writing to file
fs.writeFile("test.md", mdString, (err) => {
console.log(err);
});
})();
```**Output:**

### converting page to markdown object
Example notion page:

```js
const { Client } = require("@notionhq/client");
const { NotionToMarkdown } = require("notion-to-md");const notion = new Client({
auth: "your integration token",
});// passing notion client to the option
const n2m = new NotionToMarkdown({ notionClient: notion });(async () => {
// notice second argument, totalPage.
const x = await n2m.pageToMarkdown("target_page_id", 2);
console.log(x);
})();
```**Output:**
```json
[
{
"parent": "# heading 1",
"children": []
},
{
"parent": "- bullet 1",
"children": [
{
"parent": "- bullet 1.1",
"children": []
},
{
"parent": "- bullet 1.2",
"children": []
}
]
},
{
"parent": "- bullet 2",
"children": []
},
{
"parent": "- [ ] check box 1",
"children": [
{
"parent": "- [x] check box 1.2",
"children": []
},
{
"parent": "- [ ] check box 1.3",
"children": []
}
]
},
{
"parent": "- [ ] checkbox 2",
"children": []
}
]
```### converting list of blocks to markdown object
same notion page as before
```js
const { Client } = require("@notionhq/client");
const { NotionToMarkdown } = require("notion-to-md");const notion = new Client({
auth: "your integration token",
});// passing notion client to the option
const n2m = new NotionToMarkdown({ notionClient: notion });(async () => {
// get all blocks in the page
const { results } = await notion.blocks.children.list({
block_id,
});//convert to markdown
const x = await n2m.blocksToMarkdown(results);
console.log(x);
})();
```**Output**: same as before
### Converting a single block to markdown string
- only takes a single notion block and returns corresponding markdown string
- nesting is ignored
- depends on @notionhq/client```js
const { NotionToMarkdown } = require("notion-to-md");// passing notion client to the option
const n2m = new NotionToMarkdown({ notionClient: notion });const result = n2m.blockToMarkdown(block);
console.log(result);
```**result**:
```

```## Contribution
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.## License
[MIT](https://choosealicense.com/licenses/mit/)