Created
February 22, 2025 02:35
-
-
Save chand1012/5bab05af79eb6fc1e35f7fb3d000aeac to your computer and use it in GitHub Desktop.
Notion Block output from N8N Notion Node to Markdown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Helper function to extract plain text from a rich_text array */ | |
function parseRichText(richTextArray) { | |
if (!richTextArray || !Array.isArray(richTextArray)) return ""; | |
return richTextArray.map(rt => rt.plain_text).join(""); | |
} | |
/* Recursively convert a list of notion blocks into a Markdown string */ | |
function convertNotionBlocksToMarkdown(blocks, indentLevel = 0) { | |
const indent = " ".repeat(indentLevel); // two spaces per indent level | |
let md = ""; | |
blocks.forEach(block => { | |
let text = ""; | |
switch (block.type) { | |
case "paragraph": | |
text = block.paragraph?.rich_text ? parseRichText(block.paragraph.rich_text) : block.content; | |
break; | |
case "heading_1": | |
text = "# " + (block.heading_1?.rich_text ? parseRichText(block.heading_1.rich_text) : block.content); | |
break; | |
case "heading_2": | |
text = "## " + (block.heading_2?.rich_text ? parseRichText(block.heading_2.rich_text) : block.content); | |
break; | |
case "heading_3": | |
text = "### " + (block.heading_3?.rich_text ? parseRichText(block.heading_3.rich_text) : block.content); | |
break; | |
case "bulleted_list_item": | |
text = "- " + (block.bulleted_list_item?.rich_text ? parseRichText(block.bulleted_list_item.rich_text) : block.content); | |
break; | |
case "numbered_list_item": | |
text = "1. " + (block.numbered_list_item?.rich_text ? parseRichText(block.numbered_list_item.rich_text) : block.content); | |
break; | |
case "to_do": | |
text = "- [" + (block.to_do?.checked ? "x" : " ") + "] " + (block.to_do?.rich_text ? parseRichText(block.to_do.rich_text) : block.content); | |
break; | |
case "quote": | |
text = "> " + (block.quote?.rich_text ? parseRichText(block.quote.rich_text) : block.content); | |
break; | |
case "code": { | |
const codeLang = block.code?.language || ""; | |
const codeText = block.code?.rich_text ? parseRichText(block.code.rich_text) : block.content; | |
text = "```" + codeLang + "\n" + codeText + "\n```"; | |
break; | |
} | |
case "divider": | |
text = "---"; | |
break; | |
case "callout": { | |
const icon = block.callout?.icon?.emoji ? `${block.callout.icon.emoji} ` : ""; | |
text = "> " + icon + (block.callout?.rich_text ? parseRichText(block.callout.rich_text) : block.content); | |
break; | |
} | |
case "image": { | |
const image = block.image; | |
const url = image?.type === "external" ? image.external?.url : image.file?.url; | |
const caption = image?.caption ? parseRichText(image.caption) : ""; | |
text = url ? `` : ""; | |
break; | |
} | |
default: | |
text = block[block.type]?.rich_text ? parseRichText(block[block.type].rich_text) : block.content; | |
} | |
if (text !== "") { | |
md += indent + text + "\n\n"; | |
} | |
if (block.has_children && Array.isArray(block.children) && block.children.length > 0) { | |
md += convertNotionBlocksToMarkdown(block.children, indentLevel + 1); | |
} | |
}); | |
return md.trim(); | |
} | |
for (const item of $input.all()) { | |
item.json.markdown = convertNotionBlocksToMarkdown([item.json]); | |
} | |
return $input.all(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment