|
1 | 1 | 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") |
2 | 4 |
|
3 | 5 | const NODE_TYPE = "Notion" |
4 | 6 |
|
5 | 7 | exports.sourceNodes = async ( |
6 | 8 | { actions, createContentDigest, createNodeId, reporter }, |
7 | 9 | pluginOptions, |
8 | 10 | ) => { |
9 | | - const { createNode } = actions |
| 11 | + const pages = await getPages( |
| 12 | + { token: pluginOptions.token, databaseId: pluginOptions.databaseId }, |
| 13 | + reporter, |
| 14 | + ) |
10 | 15 |
|
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({ |
63 | 18 | id: createNodeId(`${NODE_TYPE}-${page.id}`), |
64 | | - title: title.trim(), |
65 | | - properties, |
| 19 | + title: getNotionPageTitle(page), |
| 20 | + properties: getNotionPageProperties(page), |
66 | 21 | archived: page.archived, |
67 | 22 | createdAt: page.created_time, |
68 | 23 | updatedAt: page.last_edited_time, |
|
0 commit comments