Skip to content

Commit 10d8c9f

Browse files
author
Sergei Orlov
committed
✨ Add support for new Notion API blocks
- audio - dividers - external video - embeds - quotes
1 parent 8100012 commit 10d8c9f

File tree

1 file changed

+63
-8
lines changed

1 file changed

+63
-8
lines changed

src/transformers/notion-block-to-markdown.js

+63-8
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,9 @@ exports.notionBlockToMarkdown = (block, lowerTitleLevel, depth = 0) =>
1919

2020
const isTableRow = p.startsWith("|") && p.endsWith("|")
2121

22-
const isCodeSnippetLine =
23-
block.paragraph &&
24-
block.paragraph.text &&
25-
block.paragraph.text[0] &&
26-
block.paragraph.text[0].plain_text &&
27-
block.paragraph.text[0].plain_text.startsWith("```")
28-
2922
return acc
3023
.concat(p)
31-
.concat(isTableRow || isCodeSnippetLine ? EOL_MD : EOL_MD.concat(EOL_MD))
24+
.concat(isTableRow ? EOL_MD : EOL_MD.concat(EOL_MD))
3225
.concat(childBlocksString)
3326
}
3427

@@ -80,11 +73,73 @@ exports.notionBlockToMarkdown = (block, lowerTitleLevel, depth = 0) =>
8073

8174
if (childBlock.type == "code") {
8275
return acc
76+
.concat(EOL_MD)
8377
.concat("```", childBlock.code.language, EOL_MD)
8478
.concat(blockToString(childBlock.code.text))
8579
.concat(EOL_MD)
8680
.concat("```")
8781
.concat(childBlocksString)
82+
.concat(EOL_MD)
83+
}
84+
85+
if (childBlock.type == "image") {
86+
const imageUrl =
87+
childBlock.image.type == "external" ? childBlock.image.external.url : childBlock.image.file.url
88+
89+
return acc
90+
.concat("![")
91+
.concat(blockToString(childBlock.image.caption))
92+
.concat("](")
93+
.concat(imageUrl)
94+
.concat(")")
95+
.concat(EOL_MD)
96+
}
97+
98+
if (childBlock.type == "audio") {
99+
const audioUrl =
100+
childBlock.audio.type == "external" ? childBlock.audio.external.url : childBlock.audio.file.url
101+
102+
return acc
103+
.concat("<audio controls>")
104+
.concat(EOL_MD)
105+
.concat(`<source src="${audioUrl}" />`)
106+
.concat(EOL_MD)
107+
.concat("</audio>")
108+
.concat(EOL_MD)
109+
}
110+
111+
if (childBlock.type == "video" && childBlock.video.type == "external") {
112+
const videoUrl = childBlock.video.external.url
113+
114+
return acc.concat(videoUrl).concat(EOL_MD)
115+
}
116+
117+
if (childBlock.type == "embed") {
118+
return acc.concat(childBlock.embed.url).concat(EOL_MD)
119+
}
120+
121+
if (childBlock.type == "quote") {
122+
return acc.concat("> ").concat(blockToString(childBlock.quote.text)).concat(EOL_MD)
123+
}
124+
125+
// TODO: Add support for callouts, internal video, andd files
126+
127+
if (childBlock.type == "bookmark") {
128+
const bookmarkUrl = childBlock.bookmark.url
129+
130+
const bookmarkCaption = blockToString(childBlock.bookmark.caption) || bookmarkUrl
131+
132+
return acc
133+
.concat("[")
134+
.concat(bookmarkCaption)
135+
.concat("](")
136+
.concat(bookmarkUrl)
137+
.concat(")")
138+
.concat(EOL_MD)
139+
}
140+
141+
if (childBlock.type == "divider") {
142+
return acc.concat("---").concat(EOL_MD)
88143
}
89144

90145
if (childBlock.type == "unsupported") {

0 commit comments

Comments
 (0)