-
Notifications
You must be signed in to change notification settings - Fork 1
/
notion-fetcher.js
114 lines (91 loc) · 2.78 KB
/
notion-fetcher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const { Client } = require("@notionhq/client")
const fs = require("fs")
// Initializing a client
const notion = new Client({
auth: process.env.NOTION_TOKEN,
})
async function getPagesInDB() {
const pagesByID = (
await notion.databases.query({
database_id: "a23022c1-9fa0-4b20-a4ca-5434eb4c1c9f",
})
).results.map(({ id }) => id)
return pagesByID
}
function getTextFromBlocks(blocks) {
let content = ""
for (const block of blocks) {
content = content.concat(parseBlockForText(block))
content = content.concat(parseBlockForHeading(block))
}
return content
}
function parseBlockForHeading(block) {
let content = ""
for (let i = 1; i <= 6; i++) {
const parsedHeading = block[`heading_${i}`]?.text.map(textObject => {
return `${textObject["plain_text"]}`
})
if (parsedHeading?.length > 0 && typeof parsedHeading !== undefined) {
const headingWithNewLine = `##${parsedHeading.join("\n")}\n`
content = content.concat(headingWithNewLine)
}
}
return content
}
function parseBlockForText(block) {
let content = ""
const parsedText = block.paragraph?.text.map(
textObject => `${textObject["plain_text"]}`
)
if (parsedText?.length > 0 && typeof parsedText !== undefined) {
const heading = `${parsedText.join("\n")}\n`
content = content.concat(heading)
}
return content
}
async function generateMarkDownFromInfo(page, block) {
const title = page.properties["Name"].title[0].text.content
const date = page.properties["date"]?.date.start || ""
const description =
page.properties["description"]["rich_text"]?.[0].text.content || ""
const type = page.properties["type"].select.name
const blocksResult = block.results
const tags = page.properties["tags"]["multi_select"]
.map(({ name }) => name.toLowerCase())
.toString()
const content = getTextFromBlocks(blocksResult)
const result = `---
title: ${title}
date: "${date}"
type: ${type}
description: ${description}
tags: [${tags}]
---
${content}
`
return result
}
async function fetchAndCreatePage(pageId) {
const page = await notion.pages.retrieve({
page_id: pageId,
})
const block = await notion.blocks.children.list({ block_id: pageId })
const content = await generateMarkDownFromInfo(page, block)
const slug = page.properties["slug"]["rich_text"]?.[0].text.content || ""
const type = page.properties["type"].select.name
if (type === "draft") return
try {
await fs.writeFileSync(`./content/blog/${slug}.md`, content)
console.log("Writing file for ", slug)
} catch (e) {
console.error("Error saving md file for ", slug, e)
}
}
async function callNotionAndCreatePages() {
const pagesByID = await getPagesInDB()
for (const pageId of pagesByID) {
fetchAndCreatePage(pageId)
}
}
callNotionAndCreatePages()