gatsby-source-notion-churnotion 1.0.6 → 1.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -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`;
@@ -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
  // 페이지인 경우
@@ -83,19 +83,38 @@ const getPages = async ({ databaseId, reporter, getCache, actions, createNode, c
83
83
  }
84
84
  const nodeId = createNodeId(`${page.id}-page`);
85
85
  // Tag 노드 만들기
86
+ const tagIds = [];
86
87
  if (page.properties.tags && page.properties.tags.multi_select) {
87
- page.properties.tags.multi_select.map((tagData) => createNode({
88
- id: createNodeId(`${tagData.id}-tag`),
89
- tag_name: tagData.name,
90
- color: tagData.color,
91
- internal: {
92
- type: constants_1.NODE_TYPE.Tag,
93
- contentDigest: crypto_1.default
94
- .createHash(`md5`)
95
- .update(JSON.stringify(tagData.id))
96
- .digest(`hex`),
97
- },
98
- }));
88
+ page.properties.tags.multi_select.forEach((tagData) => {
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
+ }
117
+ });
99
118
  }
100
119
  const bookId = page.properties?.book?.relation?.[0]?.id || null;
101
120
  const markdownContent = await connector_1.n2m.pageToMarkdown(page.id);
@@ -120,10 +139,25 @@ const getPages = async ({ databaseId, reporter, getCache, actions, createNode, c
120
139
  .update(JSON.stringify(nodeId))
121
140
  .digest(`hex`),
122
141
  },
123
- tags: [],
142
+ tags: tagIds,
124
143
  parent: null,
125
144
  };
126
145
  await createNode(postNode);
146
+ // tag와 post 부모-자식 관계 설정정
147
+ tagIds.forEach((tagId) => {
148
+ const tagNode = getNode(tagId);
149
+ if (tagNode) {
150
+ createParentChildLink({
151
+ parent: tagNode,
152
+ child: postNode,
153
+ });
154
+ reporter.info(`[SUCCESS] Linked tag: ${tagNode.tag_name} -> page: ${postNode.title}`);
155
+ }
156
+ else {
157
+ reporter.warn(`[WARNING] Tag node not found for ID: ${tagId}`);
158
+ }
159
+ });
160
+ // category와 post 부모-자식 관계 설정
127
161
  if (parentCategoryId && postNode) {
128
162
  const parentNode = getNode(parentCategoryId); // Gatsby에서 노드를 검색
129
163
  if (parentNode) {
@@ -8,7 +8,7 @@ const createSchemaCustomization = ({ actions }) => {
8
8
  type ${constants_1.NODE_TYPE.Post} implements Node {
9
9
  id: ID!
10
10
  category: ${constants_1.NODE_TYPE.Category}! @link(by: "id", from: "category")
11
- tags: ${constants_1.NODE_TYPE.Tag} @link(by: "id")
11
+ tags: [${constants_1.NODE_TYPE.Tag}] @link(by: "id")
12
12
  book_id: ${constants_1.NODE_TYPE.Book} @link(by: "id")
13
13
  title: String
14
14
  content: [JSON]
@@ -24,6 +24,7 @@ const createSchemaCustomization = ({ actions }) => {
24
24
  id: ID!
25
25
  tag_name: String!
26
26
  color: String!
27
+ churnotions: [${constants_1.NODE_TYPE.Post}] @link(by: "id", from: "tags")
27
28
  }
28
29
 
29
30
  type ${constants_1.NODE_TYPE.Category} implements Node {
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.6",
4
+ "version": "1.0.8",
5
5
  "skipLibCheck": true,
6
6
  "license": "0BSD",
7
7
  "main": "./dist/gatsby-node.js",