|
| 1 | +const { blockToString } = require("../block-to-string") |
| 2 | + |
| 3 | +const EOL_MD = "\n\n" |
| 4 | + |
| 5 | +exports.getNotionPageMD = (page) => |
| 6 | + page.children.reduce((acc, block) => { |
| 7 | + let childBlocksString = "" |
| 8 | + |
| 9 | + if (block.has_children) { |
| 10 | + childBlocksString = "<div notion-nested>" |
| 11 | + .concat(childBlocksString) |
| 12 | + .concat(this.getNotionPageMD(block)) |
| 13 | + .concat("</div>") |
| 14 | + .concat(EOL_MD) |
| 15 | + } |
| 16 | + |
| 17 | + if (block.type == "paragraph") { |
| 18 | + return acc.concat(blockToString(block.paragraph.text)).concat(`\n\n`) |
| 19 | + } |
| 20 | + |
| 21 | + if (block.type.startsWith("heading_")) { |
| 22 | + const headingLevel = Number(block.type.split("_")[1]) |
| 23 | + |
| 24 | + return acc |
| 25 | + .concat("#".repeat(headingLevel)) |
| 26 | + .concat(" ") |
| 27 | + .concat(blockToString(block[block.type].text)) |
| 28 | + .concat(EOL_MD) |
| 29 | + } |
| 30 | + |
| 31 | + if (block.type == "to_do") { |
| 32 | + return acc |
| 33 | + .concat(`- [${block.to_do.checked ? "x" : " "}] `) |
| 34 | + .concat(blockToString(block.to_do.text)) |
| 35 | + .concat(EOL_MD) |
| 36 | + } |
| 37 | + |
| 38 | + if (block.type == "bulleted_list_item") { |
| 39 | + return acc.concat("* ").concat(blockToString(block.bulleted_list_item.text)).concat(EOL_MD) |
| 40 | + } |
| 41 | + |
| 42 | + if (block.type == "numbered_list_item") { |
| 43 | + return acc.concat("1. ").concat(blockToString(block.numbered_list_item.text)).concat(EOL_MD) |
| 44 | + } |
| 45 | + |
| 46 | + if (block.type == "toggle") { |
| 47 | + return acc |
| 48 | + .concat("<details><summary>") |
| 49 | + .concat(blockToString(block.toggle.text)) |
| 50 | + .concat("</summary>") |
| 51 | + .concat(childBlocksString) |
| 52 | + .concat("</details>") |
| 53 | + } |
| 54 | + |
| 55 | + if (block.type == "unsupported") { |
| 56 | + return acc |
| 57 | + .concat(`<!-- Block ${block.id} is not supported by Notion API. Yet. -->`) |
| 58 | + .concat(EOL_MD) |
| 59 | + } |
| 60 | + |
| 61 | + return acc |
| 62 | + }, "") |
0 commit comments