souvikinator / notion-to-md

Convert notion pages, block and list of blocks to markdown (supports nesting and custom parsing)
https://www.npmjs.com/package/notion-to-md
MIT License
1.11k stars 91 forks source link

feat: add setCustomTransformer. Customize parsing! #39

Closed froger closed 2 years ago

froger commented 2 years ago
froger commented 2 years ago

Hello @souvikinator ! Thanks for this repo, it's really helpful.

While developing scripts to transform notion pages to Docusaurus markdown (mdx), I've stumbled on a specific need that may help other: I want to change some parsing in a very way, like handling the embed betters.

So here what I've done:

n2m.setCustomTransformer('embed', async (block) => {
  const {embed} = block as any;
  if (!embed?.url) return '';
  return `<figure>
<iframe style={{border: "none"}} width="800" height="450" src="${
    embed?.url
  }"></iframe>
<figcaption>${await n2m.blockToMarkdown(embed?.caption)}</figcaption>
</figure>`;
});

This way, when the parser have a "embed" type, it executes this function and not the default one. It can fits any other type.

Tradeoffs: The types for ListBlockChildrenResponseResult have not all the notion types, so it may be needed to do a any type-casting.

souvikinator commented 2 years ago

Hello there, Thanks for the contribution. That definitely is an useful feature.

I'll test it locally and create a new release shortly.

souvikinator commented 2 years ago

Wow that's a pretty awesome feature. Just love it 💯. It's live in v2.5.0.