Skip to content
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

Adding a custom transformer for synced blocks produces double output #62

Closed
dionjwa opened this issue Apr 2, 2023 · 3 comments
Closed
Assignees
Labels
bug Something isn't working

Comments

@dionjwa
Copy link

dionjwa commented Apr 2, 2023

I'm using docu-notion which consumes this module.

I created a custom transformer for synced blocks since they were getting ignored.

However, the markdown output is doubled. Somehow the transform function is getting called twice.

The code for handling synced blocks is a bit unclear.

I suspect it's complicated by that for synced blocks, you need to dig into the children and then follow the synced blocks there. But even if my custom transformer just returns some hard-coded text string, that output is also doubled, so the doubling is not due to the custom transformer, it's seems the problem is upstream.

Relevant notion doc:

image

Then the relevant output is:

image

The transformer to test is:

{
  type: "synced_block",
  getStringFromBlock: async (
    context: IDocuNotionContext,
    block: NotionBlock
  ) => {
    return `test output ${Math.random()}`;
  }
}
@souvikinator
Copy link
Owner

souvikinator commented Apr 2, 2023

So I tried it out and the synced block is not getting ignored when not using setCustomTransformer.
Just the mermaid syntax is not being rendered which is why you are using a custom transformer.

Now custom transformer does produce duplicate output but only for synced_block. I'll into it and get back to you.

@souvikinator souvikinator self-assigned this Apr 2, 2023
@souvikinator souvikinator added the bug Something isn't working label Apr 2, 2023
@dionjwa
Copy link
Author

dionjwa commented Apr 3, 2023

Sorry, you're right, the content wasn't ignored per se.

Here is my function for getting the content of the synced blocks. I am only getting the first child each time, I only have a single block in the synced blocks, maybe that's an edge case, not sure:

/**
 * Replacing synced blocks is returning either the synced block, or
 * the first child of the synced block, and keep going until you reach
 * a non-synced block.
 */
const replaceSyncedBlockWithTarget = async (
  block: NotionBlock,
  client: Client
): Promise<NotionBlock> => {
  if (block.type !== "synced_block") {
    return block;
  }
  const synced_from = block?.synced_block?.synced_from?.block_id;
  if (synced_from) {
    const syncedBlock = await client.blocks.retrieve({ block_id: synced_from });
    return await replaceSyncedBlockWithTarget(
      syncedBlock as NotionBlock,
      client
    );
  }

  const children = await client.blocks.children.list({ block_id: block.id });
  if (children.results.length === 0) {
    return block;
  }
  return await replaceSyncedBlockWithTarget(
    children.results[0] as NotionBlock,
    client
  );
};

@souvikinator souvikinator moved this to Todo in notion-to-md Apr 14, 2023
@souvikinator souvikinator moved this from Todo to In Progress in notion-to-md Apr 14, 2023
@souvikinator souvikinator moved this from In Progress to Done in notion-to-md May 16, 2023
@souvikinator
Copy link
Owner

This issue is fixed and is live in v3.0.0
Thanks for your contribution. Closing the issue, feel free to reopen if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
No open projects
Status: Done
Development

No branches or pull requests

2 participants