Skip to content

Feature Request: A way to add markup before and after children blocks #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
m4rrc0 opened this issue Sep 28, 2022 · 3 comments
Open

Comments

@m4rrc0
Copy link

m4rrc0 commented Sep 28, 2022

Hi.
It would be really useful to be able to add markup before and after children blocks. It would allow us to inject html tags more easily. The "outline structure" of Notion blocks could be used to specify children of a tag in a natural way. Since most (all?) markdown parsers accept html, it makes sense to me to easily allow replacing blocks with html tags.

One obvious candidate for such feature is the toggle block. I saw that you currently need to re-fetch children blocks inside the toggle transform function. Wouldn't it be more consistent if the parent toggle block only outputs <details><summary>${summary}</summary> as 'opening output' and </details> as 'closing output'? Then the children's outputs would be injected in between the opening and closing strings of the parent.

Of course, there would be some considerations to keep in mind. For example, children would need to be aware of all their parents' types because we shouldn't mistakenly create <p> elements inside <p> elements for example.
That is not really up to this library to make this check but simply allow one to implement her own logic with the setCustomTransformer function.

Example

n2m.setCustomTransformer("toggle", async (block, ancestors) => {
  const { has_children, toggle } = block;
  let toggle_rich_md = "";
  let toggle_plain_text = "";
  toggle.rich_text.forEach((rich_text) => {
    toggle_rich_md += n2m.annotatePlainText(
      rich_text.plain_text,
      rich_text.annotations
    );
    toggle_plain_text += rich_text.plain_text
  });

  // if a string is returned, it is the 'opening output' and there is no 'closing output'
  // (so it stays consistent with the current API)
  if (!has_children) return `<p>${toggle_rich_md}</p>`

  // if an object is returned, children of this block will be written between the 'open' string and the 'close' string
  return { open: `<details><summary>${toggle_plain_text}</summary>`, close: `</details>`};
});
@m4rrc0
Copy link
Author

m4rrc0 commented Sep 28, 2022

PS: it would also make it super easy to integrate with tools like Markdoc or even MDX which need opening and closing tags and can add a ton of functionality.

@souvikinator
Copy link
Owner

souvikinator commented Jan 5, 2025

@m4rrc0 thanks for sharing your usecase and the suggestion is valuable. The upcoming version v4 explores this and a lot more. It's in proposal phase so it's a perfect time to have your feedback on it and know if it solves for your use case
TLDR; We are expanding beyond MD ;)
Please checkout the discussion: #112

@m4rrc0
Copy link
Author

m4rrc0 commented Jan 9, 2025

Thanks for the reply.
Your plans seem ambitious. That's great.
I am not actively working on that codebase anymore so I can't provide a quick feedback unfortunately. I might get back to that project during 2025 but nothing is set yet.
Until then, good luck in this endeavor!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: Todo
Development

No branches or pull requests

2 participants