gatsby-source-notion-churnotion 1.0.7 → 1.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ import { IGetBooksParams } from "../types";
2
+ export declare const getBooks: ({ bookDatabaseId, reporter, createNode, createNodeId, }: IGetBooksParams) => Promise<void>;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getBooks = void 0;
7
+ const crypto_1 = __importDefault(require("crypto"));
8
+ const constants_1 = require("../constants");
9
+ const fetchData_1 = require("../util/fetchData");
10
+ const getBooks = async ({ bookDatabaseId, reporter, createNode, createNodeId, }) => {
11
+ const databaseUrl = `databases/${bookDatabaseId}/query`;
12
+ const body = {};
13
+ const result = await (0, fetchData_1.fetchPostWithRetry)(databaseUrl, body);
14
+ if (result?.results?.length) {
15
+ reporter.info(`[SUCCESS] total BOOK pages > ${result.results.length}`);
16
+ }
17
+ for (const page of result.results) {
18
+ reporter.info(`[CHECK] BOOK page: ${page.id}`);
19
+ const nodeId = createNodeId(`${page.id}-book`);
20
+ const bookNode = {
21
+ id: nodeId,
22
+ book_name: page.properties?.[`이름`]?.title?.[0]?.plain_text || `Unnamed`,
23
+ slug: page.properties?.slug?.rich_text?.plain_text || `unnamed-slug`,
24
+ parent: null,
25
+ children: [],
26
+ internal: {
27
+ type: constants_1.NODE_TYPE.Category,
28
+ contentDigest: crypto_1.default
29
+ .createHash(`md5`)
30
+ .update(JSON.stringify(page))
31
+ .digest(`hex`),
32
+ },
33
+ create_date: page.created_time,
34
+ update_date: page.last_edited_time,
35
+ };
36
+ createNode(bookNode);
37
+ }
38
+ };
39
+ exports.getBooks = getBooks;
@@ -17,7 +17,7 @@ const getPages = async ({ databaseId, reporter, getCache, actions, createNode, c
17
17
  * @param databaseId 데이터베이스 아이디
18
18
  * @param parentCategoryId 부모 데이터베이스 아이디
19
19
  */
20
- const processDatabase = async (databaseId, parentCategoryId = null, categoryPath = []) => {
20
+ const processDatabase = async (databaseId, parentCategoryId = null, categoryPath = [], tagMap = {}) => {
21
21
  try {
22
22
  while (hasMore) {
23
23
  const databaseUrl = `databases/${databaseId}/query`;
@@ -27,7 +27,7 @@ const getPages = async ({ databaseId, reporter, getCache, actions, createNode, c
27
27
  reporter.info(`[SUCCESS] total pages > ${result.results.length}`);
28
28
  }
29
29
  for (const page of result.results) {
30
- reporter.info(`[CHECK!!!] page: ${page.id}`);
30
+ reporter.info(`[CHECK] page: ${page.id}`);
31
31
  const pageUrl = `blocks/${page.id}/children?page_size=100`;
32
32
  // 페이지 데이터
33
33
  const pageData = await (0, fetchData_1.fetchGetWithRetry)(pageUrl);
@@ -68,7 +68,7 @@ const getPages = async ({ databaseId, reporter, getCache, actions, createNode, c
68
68
  }
69
69
  }
70
70
  const newCategoryPath = [...categoryPath, categoryNode];
71
- await processDatabase(categoryJsonData.id, nodeId, newCategoryPath);
71
+ await processDatabase(categoryJsonData.id, nodeId, newCategoryPath, tagMap);
72
72
  }
73
73
  else {
74
74
  // 페이지인 경우
@@ -86,21 +86,34 @@ const getPages = async ({ databaseId, reporter, getCache, actions, createNode, c
86
86
  const tagIds = [];
87
87
  if (page.properties.tags && page.properties.tags.multi_select) {
88
88
  page.properties.tags.multi_select.forEach((tagData) => {
89
- const tagNodeId = createNodeId(`${tagData.id}-tag`);
90
- tagIds.push(tagNodeId); // 태그 ID 저장
91
- createNode({
92
- id: tagNodeId,
93
- tag_name: tagData.name,
94
- color: tagData.color,
95
- churnotions: [],
96
- internal: {
97
- type: constants_1.NODE_TYPE.Tag,
98
- contentDigest: crypto_1.default
99
- .createHash(`md5`)
100
- .update(JSON.stringify(tagData))
101
- .digest(`hex`),
102
- },
103
- });
89
+ if (tagMap[tagData.name]) {
90
+ // 이미 존재하는 태그라면 tagMap에서 가져오기
91
+ const existingTagId = tagMap[tagData.name];
92
+ tagIds.push(existingTagId); // 기존 태그 ID 추가
93
+ reporter.info(`[INFO] Reusing existing tag: ${tagData.name}`);
94
+ }
95
+ else {
96
+ // 새로운 태그 생성
97
+ const tagNodeId = createNodeId(`${tagData.id}-tag`);
98
+ tagMap[tagData.name] = tagNodeId; // tagMap에 저장
99
+ tagIds.push(tagNodeId); // 새로운 태그 ID 추가
100
+ // 태그 노드 생성
101
+ const tagNode = {
102
+ id: tagNodeId,
103
+ tag_name: tagData.name,
104
+ color: tagData.color,
105
+ churnotions: [], // 연결된 페이지 리스트 초기화
106
+ internal: {
107
+ type: constants_1.NODE_TYPE.Tag,
108
+ contentDigest: crypto_1.default
109
+ .createHash(`md5`)
110
+ .update(JSON.stringify(tagData))
111
+ .digest(`hex`),
112
+ },
113
+ };
114
+ createNode(tagNode);
115
+ reporter.info(`[SUCCESS] Created new tag: ${tagData.name}`);
116
+ }
104
117
  });
105
118
  }
106
119
  const bookId = page.properties?.book?.relation?.[0]?.id || null;
@@ -109,12 +122,13 @@ const getPages = async ({ databaseId, reporter, getCache, actions, createNode, c
109
122
  const postNode = {
110
123
  id: nodeId,
111
124
  category: parentCategoryId,
112
- book_id: bookId,
125
+ book: getNode(`${bookId}-book`),
126
+ book_index: page.properties?.bookIndex?.number || 0,
113
127
  title: title,
114
128
  content: markdownContent,
115
129
  create_date: page.created_time,
116
130
  update_date: page.last_edited_time,
117
- version: page.properties?.version?.rich_text?.[0]?.plain_text || null,
131
+ version: page.properties?.version?.number || null,
118
132
  description: null,
119
133
  slug: slug || `no-title-${nodeId}`,
120
134
  category_list: categoryPath,
@@ -130,6 +144,29 @@ const getPages = async ({ databaseId, reporter, getCache, actions, createNode, c
130
144
  parent: null,
131
145
  };
132
146
  await createNode(postNode);
147
+ // book과 post 부모-자식 관계 설정
148
+ const bookNode = getNode(bookId);
149
+ if (bookNode) {
150
+ createParentChildLink({
151
+ parent: bookNode,
152
+ child: postNode,
153
+ });
154
+ }
155
+ // tag와 post 부모-자식 관계 설정
156
+ tagIds.forEach((tagId) => {
157
+ const tagNode = getNode(tagId);
158
+ if (tagNode) {
159
+ createParentChildLink({
160
+ parent: tagNode,
161
+ child: postNode,
162
+ });
163
+ reporter.info(`[SUCCESS] Linked tag: ${tagNode.tag_name} -> page: ${postNode.title}`);
164
+ }
165
+ else {
166
+ reporter.warn(`[WARNING] Tag node not found for ID: ${tagId}`);
167
+ }
168
+ });
169
+ // category와 post 부모-자식 관계 설정
133
170
  if (parentCategoryId && postNode) {
134
171
  const parentNode = getNode(parentCategoryId); // Gatsby에서 노드를 검색
135
172
  if (parentNode) {
@@ -9,7 +9,8 @@ const createSchemaCustomization = ({ actions }) => {
9
9
  id: ID!
10
10
  category: ${constants_1.NODE_TYPE.Category}! @link(by: "id", from: "category")
11
11
  tags: [${constants_1.NODE_TYPE.Tag}] @link(by: "id")
12
- book_id: ${constants_1.NODE_TYPE.Book} @link(by: "id")
12
+ book: ${constants_1.NODE_TYPE.Book} @link(by: "id")
13
+ book_index: Int
13
14
  title: String
14
15
  content: [JSON]
15
16
  create_date: Date! @dateformat
@@ -1,18 +1,25 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.sourceNodes = void 0;
4
- const getPage_1 = require("./api/getPage");
4
+ const getPages_1 = require("./api/getPages");
5
+ const getBooks_1 = require("./api/getBooks");
5
6
  const sourceNodes = async (gatsbyApi, options) => {
6
7
  const { actions, reporter, createNodeId, getNode, getCache } = gatsbyApi;
7
8
  const { createNode, createParentChildLink } = actions;
8
- const { token, databaseId } = options;
9
+ const { token, databaseId, bookDatabaseId } = options;
9
10
  if (!token || !databaseId) {
10
11
  reporter.error(`[ERROR] Missing Notion API token or database ID.`);
11
12
  return;
12
13
  }
13
14
  reporter.info(`[INFO] Fetching pages from Notion database...`);
14
15
  try {
15
- await (0, getPage_1.getPages)({
16
+ await (0, getBooks_1.getBooks)({
17
+ bookDatabaseId,
18
+ reporter,
19
+ createNode,
20
+ createNodeId,
21
+ });
22
+ await (0, getPages_1.getPages)({
16
23
  token,
17
24
  databaseId,
18
25
  reporter,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gatsby-source-notion-churnotion",
3
3
  "description": "Gatsby plugin that can connect with One Notion Database RECURSIVELY using official API",
4
- "version": "1.0.7",
4
+ "version": "1.0.10",
5
5
  "skipLibCheck": true,
6
6
  "license": "0BSD",
7
7
  "main": "./dist/gatsby-node.js",
File without changes