• Stars
    star
    1,077
  • Rank 42,945 (Top 0.9 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 3 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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


notion-to-md banner
Notion-to-MD
(Notion to Markdown)

Notion-to-MD is a Node.js package that allows you to convert Notion pages to Markdown format.

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

notion-to-md - Programmatically convert notion pages to markdown | Product Hunt

Install

npm install notion-to-md

Usage

⚠️ Note: Before getting started, create an integration and find the token. Details on methods can be found in API section

⚠️ Note: Starting from v2.7.0, toMarkdownString no longer automatically saves child pages. Now it provides an object containing the markdown content of child pages.

converting markdown objects to markdown string

This is how the notion page looks for this example:

const { Client } = require("@notionhq/client");
const { NotionToMarkdown } = require("notion-to-md");
const fs = require('fs');
// 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);
  console.log(mdString.parent);
})();

Separate child page content

parent page content:

child page content:

NotionToMarkdown takes second argument, config

const { Client } = require("@notionhq/client");
const { NotionToMarkdown } = require("notion-to-md");
const fs = require('fs');
// 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,
    config:{
     separateChildPage:true, // default: false
  }
 });

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

Output:

toMarkdownString returns an object with target page content corresponding to parent property and if any child page exists then it's included in the same object.

User gets to save the content separately.

Disable child page parsing

...

const n2m = new NotionToMarkdown({ 
  notionClient: notion,
    config:{
     parseChildPages:false, // default: parseChildPages
  }
 });

...

converting page to markdown object

Example notion page:

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

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)

Custom Transformers

You can define your own custom transformer for a notion type, to parse and return your own string. setCustomTransformer(type, func) will overload the parsing for the giving type.

const { NotionToMarkdown } = require("notion-to-md");
const n2m = new NotionToMarkdown({ notionClient: notion });
n2m.setCustomTransformer("embed", async (block) => {
  const { embed } = block as any;
  if (!embed?.url) return "";
  return `<figure>
  <iframe src="${embed?.url}"></iframe>
  <figcaption>${await n2m.blockToMarkdown(embed?.caption)}</figcaption>
</figure>`;
});
const result = n2m.blockToMarkdown(block);
// Result will now parse the `embed` type with your custom function.

Note Be aware that setCustomTransformer will take only the last function for the given type. You can't set two different transforms for the same type.

You can also use the default parsing by returning false in your custom transformer.

// ...
n2m.setCustomTransformer("embed", async (block) => {
  const { embed } = block as any;
  if (embed?.url?.includes("myspecialurl.com")) {
    return `...`; // some special rendering
  }
  return false; // use default behavior
});
const result = n2m.blockToMarkdown(block);
// Result will now only use custom parser if the embed url matches a specific url

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.

Contributers

License

MIT

More Repositories

1

lsx

ls Xtended : A command line utility that lets you navigate through terminal like a pro 😎.
Go
154
star
2

gofuzz

Fast as Flash Web URL Fuzzing tool written in golang.
Go
36
star
3

asker

A collection of common interactive command line user interfaces for C++
C++
23
star
4

unwee

A simple URL un-shortener in golang
Go
12
star
5

synko

[abandoned] 🔁 Sync application configurations and settings across multiple multi-platform devices 🖥️💻 (linux and macos)
Python
5
star
6

resume

TeX
4
star
7

stegano

Simple Steganography Tool for hiding and retrieving message from an image.
JavaScript
4
star
8

gh-retrieve

Simple Nodejs module to download any specific directory or sub-directory in a public GitHub repository
JavaScript
3
star
9

spawn-ignore

simple bash script to generate .gitignore file of multiple languages
Shell
3
star
10

node-eyeson

minimal file watcher cli written in node.
JavaScript
3
star
11

errfmt

package to format error
JavaScript
2
star
12

web-SS

Screenshots of the websites in bulk, automating the screenshot process in single click!
JavaScript
2
star
13

termparse

Minimal node js cli maker
JavaScript
2
star
14

live-expression-devloper-tool

Chrome Live Expression Developer tool for Mozilla FireFox
JavaScript
2
star
15

opoll

Python
2
star
16

my_configs

backup of configuration files for various application
2
star
17

get-node-app

Setup basic NodeJS templates in few steps
JavaScript
2
star
18

giscus_comments

1
star
19

logto

minimal NodeJs module to make log files on the go
JavaScript
1
star
20

dump

one off scripts and stuff that don't require a new repo
1
star
21

eyes-on

1
star
22

souvikinator

1
star
23

super-noob

Assembly
1
star
24

notes

JavaScript
1
star
25

neko-neko

JavaScript
1
star
26

PeC

A super simple terminal based text editor made with cpp
C++
1
star
27

pad-even

pad strings evenly
JavaScript
1
star
28

Tools

Few scripts and tools under one roof
Python
1
star
29

weird-qr

Convert Current URL into or any text into QR code on the go right from your browser
JavaScript
1
star
30

test-repo

This repo is a subject to all my experiments
1
star
31

choose-your-adventure

1
star
32

console-print

JavaScript
1
star