Skip to content

Commit b339d2e

Browse files
author
Sergei Orlov
committed
♻️ Refactor gatsby-node.js
1 parent e7209a3 commit b339d2e

File tree

3 files changed

+59
-55
lines changed

3 files changed

+59
-55
lines changed

gatsby-node.js

Lines changed: 10 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,23 @@
11
const { getPages } = require("./src/notion-api/get-pages")
2+
const { getNotionPageProperties } = require("./src/transformers/get-page-properties")
3+
const { getNotionPageTitle } = require("./src/transformers/get-page-title")
24

35
const NODE_TYPE = "Notion"
46

57
exports.sourceNodes = async (
68
{ actions, createContentDigest, createNodeId, reporter },
79
pluginOptions,
810
) => {
9-
const { createNode } = actions
11+
const pages = await getPages(
12+
{ token: pluginOptions.token, databaseId: pluginOptions.databaseId },
13+
reporter,
14+
)
1015

11-
const data = {
12-
pages: await getPages(pluginOptions, reporter),
13-
}
14-
15-
data.pages.forEach((page) => {
16-
const properties = Object.keys(page.properties).reduce((acc, key) => {
17-
if (page.properties[key].type == "title") {
18-
return acc
19-
}
20-
21-
return {
22-
...acc,
23-
[key]: {
24-
id: page.properties[key].id,
25-
key,
26-
value: page.properties[key][page.properties[key].type],
27-
type: page.properties[key].type,
28-
},
29-
}
30-
}, {})
31-
32-
const titleProperty = Object.keys(page.properties).find(
33-
(key) => page.properties[key].type == "title",
34-
)
35-
36-
const title = page.properties[titleProperty].title.reduce((acc, chunk) => {
37-
if (chunk.type == "text") {
38-
return acc.concat(chunk.plain_text)
39-
}
40-
41-
if (chunk.type == "mention") {
42-
if (chunk.mention.type == "user") {
43-
return acc.concat(chunk.mention.user.name)
44-
}
45-
46-
if (chunk.mention.type == "date") {
47-
if (chunk.mention.date.end) {
48-
return acc.concat(`${chunk.mention.date.start}${chunk.mention.date.start}`)
49-
}
50-
51-
return acc.concat(chunk.mention.date.start)
52-
}
53-
54-
if (chunk.mention.type == "page") {
55-
return acc.concat(chunk.plain_text)
56-
}
57-
}
58-
59-
return acc
60-
}, "")
61-
62-
createNode({
16+
pages.forEach((page) => {
17+
actions.createNode({
6318
id: createNodeId(`${NODE_TYPE}-${page.id}`),
64-
title: title.trim(),
65-
properties,
19+
title: getNotionPageTitle(page),
20+
properties: getNotionPageProperties(page),
6621
archived: page.archived,
6722
createdAt: page.created_time,
6823
updatedAt: page.last_edited_time,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
exports.getNotionPageProperties = (page) =>
2+
Object.keys(page.properties).reduce((acc, key) => {
3+
if (page.properties[key].type == "title") {
4+
return acc
5+
}
6+
7+
return {
8+
...acc,
9+
[key]: {
10+
id: page.properties[key].id,
11+
key,
12+
value: page.properties[key][page.properties[key].type],
13+
type: page.properties[key].type,
14+
},
15+
}
16+
}, {})

src/transformers/get-page-title.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
exports.getNotionPageTitle = (page) => {
2+
const titleProperty = Object.keys(page.properties).find(
3+
(key) => page.properties[key].type == "title",
4+
)
5+
6+
return page.properties[titleProperty].title
7+
.reduce((acc, chunk) => {
8+
if (chunk.type == "text") {
9+
return acc.concat(chunk.plain_text)
10+
}
11+
12+
if (chunk.type == "mention") {
13+
if (chunk.mention.type == "user") {
14+
return acc.concat(chunk.mention.user.name)
15+
}
16+
17+
if (chunk.mention.type == "date") {
18+
if (chunk.mention.date.end) {
19+
return acc.concat(`${chunk.mention.date.start}${chunk.mention.date.start}`)
20+
}
21+
22+
return acc.concat(chunk.mention.date.start)
23+
}
24+
25+
if (chunk.mention.type == "page") {
26+
return acc.concat(chunk.plain_text)
27+
}
28+
}
29+
30+
return acc
31+
}, "")
32+
.trim()
33+
}

0 commit comments

Comments
 (0)