|
| 1 | +import { NotionToMarkdown } from "notion-to-md"; |
| 2 | +import { ListBlockChildrenResponseResult } from "notion-to-md/build/types"; |
| 3 | +import { Client } from "@notionhq/client"; |
| 4 | +import { getBlockChildren } from "./CustomTranformers"; |
| 5 | + |
| 6 | +export async function notionCalloutToAdmonition( |
| 7 | + notionToMarkdown: NotionToMarkdown, |
| 8 | + notionClient: Client, |
| 9 | + block: ListBlockChildrenResponseResult |
| 10 | +): Promise<string> { |
| 11 | + // In this case typescript is not able to index the types properly, hence ignoring the error |
| 12 | + // @ts-ignore |
| 13 | + const blockContent = block.callout.text || block.callout.rich_text || []; |
| 14 | + // @ts-ignore |
| 15 | + const icon = block.callout.icon; |
| 16 | + let parsedData = ""; |
| 17 | + blockContent.map((content: Text) => { |
| 18 | + const annotations = content.annotations; |
| 19 | + let plain_text = content.plain_text; |
| 20 | + |
| 21 | + plain_text = notionToMarkdown.annotatePlainText(plain_text, annotations); |
| 22 | + |
| 23 | + // if (content["href"]) |
| 24 | + // plain_text = md.link(plain_text, content["href"]); |
| 25 | + |
| 26 | + parsedData += plain_text; |
| 27 | + }); |
| 28 | + |
| 29 | + let callout_string = ""; |
| 30 | + const { id, has_children } = block as any; |
| 31 | + if (!has_children) { |
| 32 | + const result1 = callout(parsedData, icon); |
| 33 | + return result1; |
| 34 | + } |
| 35 | + |
| 36 | + const callout_children_object = await getBlockChildren(notionClient, id, 100); |
| 37 | + |
| 38 | + // // parse children blocks to md object |
| 39 | + const callout_children = await notionToMarkdown.blocksToMarkdown( |
| 40 | + callout_children_object |
| 41 | + ); |
| 42 | + |
| 43 | + callout_string += `${parsedData}\n`; |
| 44 | + callout_children.map(child => { |
| 45 | + callout_string += `${child.parent}\n\n`; |
| 46 | + }); |
| 47 | + |
| 48 | + const result = callout(callout_string.trim(), icon); |
| 49 | + return result; |
| 50 | +} |
| 51 | + |
| 52 | +// types copied from notion-to-md to allow compilation of copied code. |
| 53 | +type TextRequest = string; |
| 54 | + |
| 55 | +type Annotations = { |
| 56 | + bold: boolean; |
| 57 | + italic: boolean; |
| 58 | + strikethrough: boolean; |
| 59 | + underline: boolean; |
| 60 | + code: boolean; |
| 61 | + color: |
| 62 | + | "default" |
| 63 | + | "gray" |
| 64 | + | "brown" |
| 65 | + | "orange" |
| 66 | + | "yellow" |
| 67 | + | "green" |
| 68 | + | "blue" |
| 69 | + | "purple" |
| 70 | + | "pink" |
| 71 | + | "red" |
| 72 | + | "gray_background" |
| 73 | + | "brown_background" |
| 74 | + | "orange_background" |
| 75 | + | "yellow_background" |
| 76 | + | "green_background" |
| 77 | + | "blue_background" |
| 78 | + | "purple_background" |
| 79 | + | "pink_background" |
| 80 | + | "red_background"; |
| 81 | +}; |
| 82 | +type Text = { |
| 83 | + type: "text"; |
| 84 | + text: { |
| 85 | + content: string; |
| 86 | + link: { |
| 87 | + url: TextRequest; |
| 88 | + } | null; |
| 89 | + }; |
| 90 | + annotations: Annotations; |
| 91 | + plain_text: string; |
| 92 | + href: string | null; |
| 93 | +}; |
| 94 | + |
| 95 | +type CalloutIcon = |
| 96 | + | { type: "emoji"; emoji?: string } |
| 97 | + | { type: "external"; external?: { url: string } } |
| 98 | + | { type: "file"; file: { url: string; expiry_time: string } } |
| 99 | + | null; |
| 100 | + |
| 101 | +const calloutsToAdmonitions = { |
| 102 | + /* prettier-ignore */ "ℹ️": "note", |
| 103 | + "💡": "tip", |
| 104 | + "❗": "info", |
| 105 | + "⚠️": "caution", |
| 106 | + "🔥": "danger", |
| 107 | +}; |
| 108 | + |
| 109 | +// This is the main change from the notion-to-md code. |
| 110 | +function callout(text: string, icon?: CalloutIcon) { |
| 111 | + let emoji: string | undefined; |
| 112 | + if (icon?.type === "emoji") { |
| 113 | + emoji = icon.emoji; |
| 114 | + } |
| 115 | + let docusaurusAdmonition = "note"; |
| 116 | + if (emoji) { |
| 117 | + // the keyof typeof magic persuades typescript that it really is OK to use emoji as a key into calloutsToAdmonitions |
| 118 | + docusaurusAdmonition = |
| 119 | + calloutsToAdmonitions[emoji as keyof typeof calloutsToAdmonitions] ?? |
| 120 | + // For Notion callouts with other emojis, pass them through using hte emoji as the name. |
| 121 | + // For this to work on a Docusaurus site, it will need to define that time on the remark-admonitions options in the docusaurus.config.js. |
| 122 | + // See https://github.com/elviswolcott/remark-admonitions and https://docusaurus.io/docs/using-plugins#using-presets. |
| 123 | + emoji; |
| 124 | + } |
| 125 | + return `:::${docusaurusAdmonition}\n\n${text}\n\n:::\n\n`; |
| 126 | +} |
0 commit comments