Skip to content

Commit 8e62588

Browse files
author
Sergei Orlov
committed
✨ Add support for retrieving page content in Markdown
1 parent 692d736 commit 8e62588

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,16 @@ query {
9090
allNotion {
9191
edges {
9292
node {
93+
id
94+
parent
95+
children
96+
internal
9397
title
9498
properties
95-
archived
99+
achived
96100
createdAt
97101
updatedAt
98-
children
102+
markdown
99103
raw
100104
}
101105
}
@@ -157,6 +161,10 @@ Date of the last page update.
157161

158162
Untouched contents of whatever Notion API returned.
159163

164+
### `markdown` (String)
165+
166+
Markdown contents of the page. Limited by blocks currently supported by Notion API. Unsupported blocks turn into HTML comments specifying that Notion marked this block as non-supported.
167+
160168
## Current state
161169

162170
- Due to the fact that Notion API only appeared recently, and it is still in beta, some blocks are

gatsby-node.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { getPages } = require("./src/notion-api/get-pages")
2+
const { getNotionPageMD } = require("./src/transformers/get-page-md")
23
const { getNotionPageProperties } = require("./src/transformers/get-page-properties")
34
const { getNotionPageTitle } = require("./src/transformers/get-page-title")
45

@@ -21,6 +22,7 @@ exports.sourceNodes = async (
2122
archived: page.archived,
2223
createdAt: page.created_time,
2324
updatedAt: page.last_edited_time,
25+
markdown: getNotionPageMD(page),
2426
raw: page,
2527
parent: null,
2628
children: [],

src/transformers/get-page-md.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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

Comments
 (0)