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

here's how to handle column support #52

Open GorvGoyl opened 2 years ago

GorvGoyl commented 2 years ago

I needed to show 2 images in one row (2 columns), but it isn't directly possible. Currently, content in columns is rendered as individual rows. I figured out a way to show it in columns as how it is shown in Notion.

n2m.setCustomTransformer("column_list", async (block) => {
    const mdBlocks = await n2m.pageToMarkdown(block.id);
    const mdString = n2m.toMarkdownString(mdBlocks);
    return `
    <div style='display:"flex";column-gap:"10px"'>
   ${mdString}
    </div>
    `;
  });
n2m.setCustomTransformer("column", (block) => {
// avoid rendering it twice
    return "";
  });

I thought of sharing it in case others are looking for same.

souvikinator commented 1 year ago

Thanks for sharing, I marked it as information and won't close this issue for future reference.

emilemoureau commented 1 year ago

Improved version with responsive css and support of titles + paragraphs + images in each column (exactly like Notion)

n2m.setCustomTransformer("column_list", async (block) => {
  const mdBlocks_temp = await n2m.pageToMarkdown(block.id);
  let final_md_string = `<div className="column" style={{ display: "flex", columnGap: "25px" }}>`;

  for (const one_block of mdBlocks_temp) {
    const mdString_temp = n2m.toMarkdownString(one_block.children);
    final_md_string = final_md_string + `<div>${mdString_temp}</div>`
  }

  return final_md_string + "</div>"
});
.column div {
  display: flex;
  justify-content: center;
  flex-direction: column;
}

@media screen and (max-width: 630px) {
  .column {
    flex-direction: column;
  }
}