Skip to content
Souvik Kar Mahapatra edited this page May 6, 2023 · 6 revisions

Note: Before getting started, create an integration and find the token. 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:

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);
})();

Output:

Save content to file

parent page content:

child page content:

Saving page to a file

Simply saving mdString from above code to a file does the work like so

  //writing to file
  fs.writeFile("test.md", mdString, (err) => {
    console.log(err);
  });

⚠️ Note: with this the child_page content is merged in the same file.

Output:

Saving child_page to separate file

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",
  config:{
	saveChildPage: true, //default: false
	saveToDir: "path/to/output/dir", //default: current directory
  }
});

// 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 parent page content to file
  // where the child pages will be saved
  // the child page will be linked to the parent page
  fs.writeFile("test.md", mdString, (err) => {
    console.log(err);
  });
})();

Output:

⚠️ Note: make sure to save parent page in the same directory as that of child page for the link from parent to child page to work.

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
Clone this wiki locally