Souvikns / notion-to-md

Convert notion pages, block and list of blocks to markdown (supports nesting)

Home Page:https://www.npmjs.com/package/notion-to-md

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Notion to Markdown

Convert notion pages, block and list of blocks to markdown (supports nesting) using notion-sdk-js

Note: Before getting started, create an integration and find the token.

Todo

  • heading
  • images
  • quotes
  • links
  • bullets
  • todo
  • inline code
  • code block
  • strikethrough, underline, bold, italic
  • nested blocks
  • pages inside pages/child page
  • embeds, bookmarks, videos, files (converted to links)
  • tables
  • divider
  • equation block (converted to code blocks)
  • convert returned markdown object to string (toMarkdownString())
  • add tests

Install

$ npm install notion-to-md

Usage

converting markdown objects to markdown string

This is how the notion page looks for this example:

Imgur

const { Client } = require("@notionhq/client");
const notion2md = require("notion-to-md");

const notion = new Client({
  auth: "your integration token",
});

// passing notion client to the option
const n2m = new notion2md({ 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:

output

converting page to markdown object

This is how the notion page looks for this example:

Imgur

const { Client } = require("@notionhq/client");
const notion2md = require("notion-to-md");

const notion = new Client({
  auth: "your integration token",
});

// passing notion client to the option
const n2m = new notion2md({ notionClient: notion });

(async () => {
  const x = await n2m.pageToMarkdown("target_page_id");
  console.log(x);
})();

Output:

[
  {
    "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

This example blocks from a single page is used. One construct array of blocks from different notion pages and pass in to the blocksToMarkdown()

same notion page as before

const { Client } = require("@notionhq/client");
const notion2md = require("notion-to-md");

const notion = new Client({
  auth: "your integration token",
});

// passing notion client to the option
const n2m = new notion2md({ 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
  • not dependent on @notionhq/client
const notion2md = require("notion-to-md");

// notion client not required
const n2m = new notion2md();

const result = n2m.blockToMarkdown(block);
console.log(result);

result:

![image](https://media.giphy.com/media/Ju7l5y9osyymQ/giphy.gif)

API

toMarkdownString(markdownObjects)

  • takes output of pageToMarkdown or blocksToMarkdown as argument
  • convert to markdown string.

pageToMarkdown(page_id)

  • Takes page_id as input and converts all the blocks in the page to corresponding markdown
  • Uses blocksToMarkdown internally.

blockToMarkdown(block)

  • Takes one notion block and converts to markdown
  • does not deal with nested notion blocks
  • This method doesn't require the notion-sdk-js.
  • Refer docs to know more about notion blocks

blocksToMarkdown(blocks)

Note: requires notion-sdk-js unlike blockToMarkdown

  • blocks: array of notion blocks
  • deals with nested blocks
  • uses blockToMarkdown internally.

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

About

Convert notion pages, block and list of blocks to markdown (supports nesting)

https://www.npmjs.com/package/notion-to-md

License:MIT License


Languages

Language:JavaScript 100.0%