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.08k stars 90 forks source link

Suggestion: Add default return value in setCustomTransformer callback function #50

Closed m4rrc0 closed 1 year ago

m4rrc0 commented 1 year ago

Hi. When using setCustomTransformer, we may want to transform the output only in some situations. It would be great to be able to return the default n2m output when we need to.

Exemple of what it may look like:

n2m.setCustomTransformer("paragraph", async (block, defaultOutput) => {
  if (hasSomeProperty(block)) return someOutput(block);
  return defaultOutput(block);
});

Additionally, we may want to only slightly transform the default output and it may make things easier to generate that output string then transform it.

kungpaogao commented 1 year ago

I would like to try looking into this. For this I see two approaches:

  1. as you suggested, make the callback type have two parameters, one of which is the function for the original transformer
  2. return false/null to return default value, which is similar to the approach that marked takes

What are your thoughts on this?

m4rrc0 commented 1 year ago

I hope @souvikinator can jump in to give input.

Personally, I don't have a strong case for either one of these. My best guess is that option 2 will be enough for at least 90% of the use cases of this feature. The 10% remaining can always use blockToMarkdown() and modify the output as needed before further processing.

Option 1 seems more versatile and may avoid unnecessary data handling outside of n2m.

Note that if you go with option 1, option 2 shouldn't be too much more work to add on top so implementing both might be a good idea...

souvikinator commented 1 year ago

Looks like I'm late. @kungpaogao are you working on this? Like @m4rrc0 said, I don't have a strong case for either of them so yea we can go with the second approach. Further issues and requirements can be addressed in the future.

kungpaogao commented 1 year ago

I haven't started yet, but now that you've commented on it, I can start looking into it!

souvikinator commented 1 year ago

@kungpaogao any update on this?

kungpaogao commented 1 year ago

@souvikinator sorry for the late reply, i implemented it here, but haven't gotten around to testing it: https://github.com/kungpaogao/notion-to-md/tree/kungpaogao/custom-transformer-default

edit: going to take a look rn