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

NotionToMarkdown(notionClient,config)

config contains options for extended functionalities.

config:{
   separateChildPage: boolean, // default: false
}

toMarkdownString(mdBlock[])

  • takes the output of pageToMarkdown or blocksToMarkdown as an argument
  • converts to markdown string of object
  • parent property contains the target page markdown string.
  • if separateChildPage is enabled in config then all the child pages content are added corresponding to their title in the same object.

for (separateChildPage=false):

image

output:

image

for (separateChildPage=true):

image

image

output:

image


pageToMarkdown(id,totalPage)

  • Uses blocksToMarkdown internally.
  • id(pageid) as input and converts all the blocks in the page to corresponding markdown object
  • totalPage is the retrieve block children request number i.e page_size Maximum = totalPage * 100.

totalPage

Notion API allows to fetch up to 100 children on a page. Total page stands for the number of pages required to get all the child blocks on a page where each page contains <= 100 blocks.

The default value is null which means all the blocks of the page will be converted to markdown, so one can choose to not pass any value as totalPage.

How to use totalPage arg ?

  • if the number of blocks in a notion page is indefinite or the requirement is to convert the whole notion page to markdown, then no need to pass totalPage as argument.

  • if the requirement is to convert the first 100 blocks of the page to markdown then use totalPage as 1.

  • if the notion page contains 150 blocks then totalPage argument should be greater than or equal to 2 leading to pageSize = 2 * 100 and rendering all 150 blocks.

Example:

// converting whole page to markdown
const x = await n2m.pageToMarkdown("target_page_id");

//or
const y = await n2m.pageToMarkdown("target_page_id",null);

// converting first 100 blocks to markdown.
const z = await n2m.pageToMarkdown("target_page_id",1);

blocksToMarkdown(blocks,totalPage)

  • blocks: array of notion blocks
  • totalPage: the retrieve block children request number i.e page_size Maximum = totalPage * 100.
  • deals with nested blocks
  • uses blockToMarkdown internally.

blockToMarkdown(block)

  • Takes single notion block and converts to markdown string
  • does not deal with nested notion blocks
  • Refer docs to know more about notion blocks

setCustomTransformer(type, func)

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.

Clone this wiki locally