GithubHelp home page GithubHelp logo

scopsy / notion-to-md Goto Github PK

View Code? Open in Web Editor NEW

This project forked from souvikinator/notion-to-md

1.0 1.0 0.0 92 KB

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

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

License: MIT License

TypeScript 100.00%

notion-to-md's Introduction

๐Ÿ’ก For better readability and detailed instructions headover to the wiki

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)
  • Simple tables
  • divider
  • equation block (converted to code blocks)
  • convert returned markdown object to string (toMarkdownString())
  • typescript support
  • add tests

Install

$ npm install notion-to-md

Usage

Note: Details on methods can be found in API section

converting markdown objects to markdown string

This is how the notion page looks for this example:

Imgur

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:

output

converting page to markdown object

Example notion page:

Imgur

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:

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

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
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:

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

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

notion-to-md's People

Contributors

souvikinator avatar dharshatharan avatar dyllan-to-you avatar dantehemerson avatar scopsy avatar souvikns avatar miaogaolin avatar

Stargazers

Gajanan Rajput avatar

Watchers

 avatar

notion-to-md's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.