gatsby-source-notion-churnotion 1.2.4 → 1.2.5

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.
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createSchemaCustomization = void 0;
4
4
  const constants_1 = require("./constants");
5
+ const resolvers_1 = require("./util/resolvers");
5
6
  const createSchemaCustomization = ({ actions, schema }) => {
6
7
  const { createTypes } = actions;
7
8
  createTypes([
@@ -119,57 +120,118 @@ const createSchemaCustomization = ({ actions, schema }) => {
119
120
  },
120
121
  },
121
122
  }),
122
- `
123
- type ${constants_1.NODE_TYPE.Post} implements Node {
124
- id: ID!
125
- category: ${constants_1.NODE_TYPE.Category}! @link(by: "id", from: "category")
126
- tags: [${constants_1.NODE_TYPE.Tag}] @link(by: "id")
127
- book: ${constants_1.NODE_TYPE.Book} @link(by: "id")
128
- book_index: Int
129
- title: String
130
- content: [JSON]
131
- create_date: Date! @dateformat
132
- update_date: Date! @dateformat
133
- version: Int
134
- description: String
135
- slug: String
136
- tableOfContents: [TocEntry]
137
- category_list: [${constants_1.NODE_TYPE.Category}]
138
- url: String!
139
- thumbnail: File @link(by: "id", from: "thumbnail")
140
- rawText: String
141
- }
142
-
143
- type ${constants_1.NODE_TYPE.Tag} implements Node {
144
- id: ID!
145
- tag_name: String!
146
- slug: String!
147
- color: String!
148
- children: [${constants_1.NODE_TYPE.Post}] @link(by: "id", from: "tags")
149
- url: String!
150
- }
151
-
152
- type ${constants_1.NODE_TYPE.Metadata} implements Node {
153
- id: ID!,
154
- title: String,
155
- description: String,
156
- image: String,
157
- url: String,
158
- }
159
-
160
- type ${constants_1.NODE_TYPE.RelatedPost} implements Node {
161
- posts: [${constants_1.NODE_TYPE.Post}] @link(by: "id")
162
- }
163
-
164
- type TocEntry {
165
- type: String!
166
- hash: String!
167
- title: String!
168
- parentHash: String
169
- level: Int
170
- contextTitle: String
171
- }
172
- `,
123
+ schema.buildObjectType({
124
+ name: constants_1.NODE_TYPE.Post,
125
+ interfaces: ["Node"],
126
+ fields: {
127
+ id: "ID!",
128
+ category: {
129
+ type: `${constants_1.NODE_TYPE.Category}!`,
130
+ extensions: {
131
+ link: { by: "id", from: "category" },
132
+ },
133
+ },
134
+ tags: {
135
+ type: `[${constants_1.NODE_TYPE.Tag}]`,
136
+ extensions: {
137
+ link: { by: "id" },
138
+ },
139
+ },
140
+ book: {
141
+ type: constants_1.NODE_TYPE.Book,
142
+ extensions: {
143
+ link: { by: "id" },
144
+ },
145
+ },
146
+ book_index: "Int",
147
+ title: "String",
148
+ content: "[JSON]",
149
+ create_date: {
150
+ type: "Date!",
151
+ extensions: {
152
+ dateformat: {},
153
+ },
154
+ },
155
+ update_date: {
156
+ type: "Date!",
157
+ extensions: {
158
+ dateformat: {},
159
+ },
160
+ },
161
+ version: "Int",
162
+ description: "String",
163
+ slug: "String",
164
+ tableOfContents: "[TocEntry]",
165
+ formattedTableOfContents: {
166
+ type: "[TocEntry]",
167
+ resolve: (source, args, context) => {
168
+ if (!source.tableOfContents)
169
+ return [];
170
+ return (0, resolvers_1.formatTableOfContents)(source.tableOfContents, context.reporter);
171
+ },
172
+ },
173
+ category_list: `[${constants_1.NODE_TYPE.Category}]`,
174
+ url: "String!",
175
+ thumbnail: {
176
+ type: "File",
177
+ extensions: {
178
+ link: { by: "id", from: "thumbnail" },
179
+ },
180
+ },
181
+ rawText: "String",
182
+ },
183
+ }),
184
+ schema.buildObjectType({
185
+ name: constants_1.NODE_TYPE.Tag,
186
+ interfaces: ["Node"],
187
+ fields: {
188
+ id: "ID!",
189
+ tag_name: "String!",
190
+ slug: "String!",
191
+ color: "String!",
192
+ children: {
193
+ type: `[${constants_1.NODE_TYPE.Post}]`,
194
+ extensions: {
195
+ link: { by: "id", from: "tags" },
196
+ },
197
+ },
198
+ url: "String!",
199
+ },
200
+ }),
201
+ schema.buildObjectType({
202
+ name: constants_1.NODE_TYPE.Metadata,
203
+ interfaces: ["Node"],
204
+ fields: {
205
+ id: "ID!",
206
+ title: "String",
207
+ description: "String",
208
+ image: "String",
209
+ url: "String",
210
+ },
211
+ }),
212
+ schema.buildObjectType({
213
+ name: constants_1.NODE_TYPE.RelatedPost,
214
+ interfaces: ["Node"],
215
+ fields: {
216
+ posts: {
217
+ type: `[${constants_1.NODE_TYPE.Post}]`,
218
+ extensions: {
219
+ link: { by: "id" },
220
+ },
221
+ },
222
+ },
223
+ }),
224
+ schema.buildObjectType({
225
+ name: "TocEntry",
226
+ fields: {
227
+ type: "String!",
228
+ hash: "String!",
229
+ title: "String!",
230
+ contextTitle: "String",
231
+ parentHash: "String",
232
+ level: "Int",
233
+ },
234
+ }),
173
235
  ]);
174
236
  };
175
237
  exports.createSchemaCustomization = createSchemaCustomization;
@@ -29,6 +29,11 @@ const processBlocksForContent = async (blocks, actions, getCache, createNodeId,
29
29
  const updatedBlocks = [];
30
30
  // 첫 번째 이미지 블록을 찾아 썸네일로 사용
31
31
  let firstImageIndex = blocks.findIndex((block) => block.type === "image");
32
+ // 헤딩 타입 블록을 먼저 처리하여 구조를 생성
33
+ const headingBlocks = blocks.filter((block) => ["heading_1", "heading_2", "heading_3"].includes(block.type));
34
+ if (headingBlocks.length > 0) {
35
+ reporter.info(`Found ${headingBlocks.length} heading blocks for TOC generation`);
36
+ }
32
37
  // 블록 처리
33
38
  const processResults = await Promise.all(blocks.map(async (block, index) => {
34
39
  const result = await processorRegistry.processBlock(block);
@@ -46,8 +51,19 @@ const processBlocksForContent = async (blocks, actions, getCache, createNodeId,
46
51
  }
47
52
  return result;
48
53
  }));
54
+ // 목차 로깅 및 디버깅
55
+ const h1Count = tableOfContents.filter((item) => item.type === "heading_1").length;
56
+ const h2Count = tableOfContents.filter((item) => item.type === "heading_2").length;
57
+ const h3Count = tableOfContents.filter((item) => item.type === "heading_3").length;
58
+ reporter.info(`TOC entries before optimization: ${tableOfContents.length} total, ${h1Count} h1, ${h2Count} h2, ${h3Count} h3`);
49
59
  // 목차 최적화
50
- const processedToc = (0, tocHelper_1.optimizeTocArray)(tableOfContents, reporter);
60
+ const processedToc = (0, tocHelper_1.optimizeTocArray)(tableOfContents, reporter, {
61
+ maxSize: 2000,
62
+ warnThreshold: 500,
63
+ removeDuplicates: true,
64
+ enhanceDuplicates: true,
65
+ buildHierarchy: true,
66
+ });
51
67
  // 업데이트된 블록 적용
52
68
  processResults.forEach((result, index) => {
53
69
  if (result.updatedBlock) {
@@ -0,0 +1,6 @@
1
+ import { Reporter } from "gatsby";
2
+ import { TocEntry } from "./tocHelper";
3
+ /**
4
+ * 목차 구조를 계층적으로 변환하는 함수
5
+ */
6
+ export declare const formatTableOfContents: (tableOfContents: TocEntry[], reporter: Reporter) => TocEntry[];
@@ -0,0 +1,137 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatTableOfContents = void 0;
4
+ /**
5
+ * 목차 구조를 계층적으로 변환하는 함수
6
+ */
7
+ const formatTableOfContents = (tableOfContents, reporter) => {
8
+ if (!tableOfContents || tableOfContents.length === 0) {
9
+ return [];
10
+ }
11
+ try {
12
+ // Deep clone to avoid modifying original
13
+ const tocClone = JSON.parse(JSON.stringify(tableOfContents));
14
+ // Ensure TOC entries are sorted by level first, then by appearance order
15
+ const sortedToc = tocClone.sort((a, b) => {
16
+ // Level 기준 정렬
17
+ if ((a.level || 0) !== (b.level || 0)) {
18
+ return (a.level || 0) - (b.level || 0);
19
+ }
20
+ // Original order is preserved
21
+ return 0;
22
+ });
23
+ // Add contextTitle where missing
24
+ for (const entry of sortedToc) {
25
+ if (!entry.contextTitle) {
26
+ entry.contextTitle = entry.title;
27
+ }
28
+ }
29
+ // 1. Find all main headings (h1)
30
+ const mainHeadings = sortedToc.filter((entry) => entry.level === 1);
31
+ // 2. Find all command headings (h2) with their parent h1
32
+ const commandHeadings = sortedToc.filter((entry) => entry.level === 2);
33
+ // Group h2 by parent h1 for easier traversal
34
+ const h1ToH2Map = new Map();
35
+ for (const h1 of mainHeadings) {
36
+ h1ToH2Map.set(h1.hash, []);
37
+ }
38
+ for (const h2 of commandHeadings) {
39
+ if (h2.parentHash) {
40
+ const h2List = h1ToH2Map.get(h2.parentHash) || [];
41
+ h2List.push(h2);
42
+ h1ToH2Map.set(h2.parentHash, h2List);
43
+ }
44
+ }
45
+ // 3. Find all subheadings (h3) with their parent h2
46
+ const subheadings = sortedToc.filter((entry) => entry.level === 3);
47
+ // Group h3 by parent h2 for easier traversal
48
+ const h2ToH3Map = new Map();
49
+ for (const h2 of commandHeadings) {
50
+ h2ToH3Map.set(h2.hash, []);
51
+ }
52
+ for (const h3 of subheadings) {
53
+ if (h3.parentHash) {
54
+ const h3List = h2ToH3Map.get(h3.parentHash) || [];
55
+ h3List.push(h3);
56
+ h2ToH3Map.set(h3.parentHash, h3List);
57
+ }
58
+ }
59
+ // Find all unique h3 titles that appear anywhere
60
+ const allH3Titles = new Set();
61
+ for (const h3 of subheadings) {
62
+ allH3Titles.add(h3.title);
63
+ }
64
+ // Common expected subheadings for commands
65
+ const commonSubheadings = [
66
+ "요약",
67
+ "개요",
68
+ "설명",
69
+ "옵션",
70
+ "문법",
71
+ "주요 명령어",
72
+ "주요 속성 설명",
73
+ "설치",
74
+ "기본 명령",
75
+ ];
76
+ // Add common subheadings to the set
77
+ for (const title of commonSubheadings) {
78
+ allH3Titles.add(title);
79
+ }
80
+ reporter.info(`Found ${allH3Titles.size} unique h3 titles across sections`);
81
+ // For each command, ensure all common subheadings exist
82
+ const finalToc = [...sortedToc];
83
+ let addedEntries = 0;
84
+ // Create a map of known title combinations to avoid duplicates
85
+ const knownCombinations = new Set();
86
+ // Add all existing combinations to the known set
87
+ for (const entry of subheadings) {
88
+ if (entry.parentHash) {
89
+ const parentCommand = commandHeadings.find((h2) => h2.hash === entry.parentHash);
90
+ if (parentCommand) {
91
+ const combo = `${parentCommand.title}:${entry.title}`;
92
+ knownCombinations.add(combo);
93
+ }
94
+ }
95
+ }
96
+ // Check which commands are missing which subheadings
97
+ for (const h2 of commandHeadings) {
98
+ const h3Entries = h2ToH3Map.get(h2.hash) || [];
99
+ const existingTitles = new Set(h3Entries.map((entry) => entry.title));
100
+ // For each expected subheading, check if it exists
101
+ for (const title of commonSubheadings) {
102
+ const combo = `${h2.title}:${title}`;
103
+ // Skip if we already have this combination
104
+ if (knownCombinations.has(combo) || existingTitles.has(title)) {
105
+ continue;
106
+ }
107
+ // We don't have this subheading for this command - but don't add synthetic entries
108
+ // in this implementation as we want to show only what's actually in the document
109
+ }
110
+ }
111
+ // If we added entries, re-sort the TOC
112
+ if (addedEntries > 0) {
113
+ reporter.info(`Added ${addedEntries} missing subheadings to TOC`);
114
+ finalToc.sort((a, b) => {
115
+ if ((a.level || 0) !== (b.level || 0)) {
116
+ return (a.level || 0) - (b.level || 0);
117
+ }
118
+ return 0;
119
+ });
120
+ }
121
+ // Always make sure all h3 entries have context titles that include their parent command
122
+ for (const entry of finalToc) {
123
+ if (entry.level === 3 && entry.parentHash) {
124
+ const parentCommand = commandHeadings.find((h2) => h2.hash === entry.parentHash);
125
+ if (parentCommand) {
126
+ entry.contextTitle = `${entry.title} (${parentCommand.title})`;
127
+ }
128
+ }
129
+ }
130
+ return finalToc;
131
+ }
132
+ catch (error) {
133
+ reporter.error(`Error formatting table of contents: ${error}`);
134
+ return tableOfContents; // Return original on error
135
+ }
136
+ };
137
+ exports.formatTableOfContents = formatTableOfContents;
@@ -9,11 +9,13 @@ let lastH1Hash = "";
9
9
  let lastH2Hash = "";
10
10
  let lastH1Title = "";
11
11
  let lastH2Title = "";
12
+ let currentCommandSection = ""; // 현재 명령어 섹션 추적
12
13
  const resetTocContext = () => {
13
14
  lastH1Hash = "";
14
15
  lastH2Hash = "";
15
16
  lastH1Title = "";
16
17
  lastH2Title = "";
18
+ currentCommandSection = "";
17
19
  };
18
20
  exports.resetTocContext = resetTocContext;
19
21
  const processTableOfContents = async (block, tableOfContents) => {
@@ -28,18 +30,22 @@ const processTableOfContents = async (block, tableOfContents) => {
28
30
  if (block.type === "heading_1") {
29
31
  lastH1Title = plainText;
30
32
  lastH2Title = "";
33
+ currentCommandSection = ""; // 새 섹션 시작
31
34
  contextualId = `h1-${plainText}`;
32
35
  level = 1;
33
36
  // heading_1은 최상위 레벨이므로 부모가 없음
34
37
  }
35
38
  else if (block.type === "heading_2") {
36
39
  lastH2Title = plainText;
40
+ currentCommandSection = plainText; // 현재 명령어 설정 (예: useradd, usermod 등)
37
41
  contextualId = `h2-${lastH1Title}-${plainText}`;
38
42
  parentHash = lastH1Hash; // heading_2의 부모는 가장 최근의 heading_1
39
43
  level = 2;
40
44
  }
41
45
  else if (block.type === "heading_3") {
42
- contextualId = `h3-${lastH1Title}-${lastH2Title}-${plainText}`;
46
+ // 서브섹션 제목에 명령어 컨텍스트 추가 (예: 요약 -> useradd 요약)
47
+ const fullContextId = `h3-${lastH1Title}-${currentCommandSection}-${plainText}`;
48
+ contextualId = fullContextId;
43
49
  parentHash = lastH2Hash || lastH1Hash; // heading_3의 부모는 가장 최근의 heading_2, 없으면 heading_1
44
50
  level = 3;
45
51
  }
@@ -64,13 +70,21 @@ const processTableOfContents = async (block, tableOfContents) => {
64
70
  lastH2Hash = hash;
65
71
  }
66
72
  block.hash = hash;
73
+ // 항상 복사본을 사용하여 중복을 방지
74
+ const displayTitle = plainText;
75
+ let contextTitle = displayTitle;
76
+ // 서브헤딩에 컨텍스트 추가
77
+ if (level === 3 && currentCommandSection) {
78
+ contextTitle = `${plainText} (${currentCommandSection})`;
79
+ }
67
80
  // 중복 체크 - 동일한 hash가 이미 존재하는지 확인
68
81
  const existingTocIndex = tableOfContents.findIndex((toc) => toc.hash === hash);
69
82
  if (existingTocIndex === -1) {
70
83
  tableOfContents.push({
71
84
  type: block.type,
72
85
  hash,
73
- title: plainText,
86
+ title: displayTitle,
87
+ contextTitle,
74
88
  parentHash,
75
89
  level,
76
90
  });
@@ -6,9 +6,10 @@ export interface TocEntry {
6
6
  type: string;
7
7
  hash: string;
8
8
  title: string;
9
+ contextTitle?: string;
9
10
  parentHash?: string;
10
11
  level?: number;
11
- contextTitle?: string;
12
+ children?: TocEntry[];
12
13
  }
13
14
  /**
14
15
  * Utility function to efficiently handle large table of contents arrays
@@ -18,8 +19,8 @@ export declare const optimizeTocArray: (tocEntries: TocEntry[], reporter: Report
18
19
  maxSize?: number;
19
20
  warnThreshold?: number;
20
21
  removeDuplicates?: boolean;
21
- structureByLevel?: boolean;
22
22
  enhanceDuplicates?: boolean;
23
+ buildHierarchy?: boolean;
23
24
  }) => TocEntry[];
24
25
  /**
25
26
  * Enhances TOC entries with contextual titles to disambiguate duplicate titles
@@ -6,11 +6,11 @@ exports.enrichDuplicateTitles = exports.optimizeTocArray = void 0;
6
6
  * by removing duplicates and optionally limiting size while preserving hierarchy
7
7
  */
8
8
  const optimizeTocArray = (tocEntries, reporter, options = {}) => {
9
- const { maxSize = 1000, // Maximum entries to include
9
+ const { maxSize = 2000, // Maximum entries to include
10
10
  warnThreshold = 300, // Threshold to issue warning
11
11
  removeDuplicates = true, // Whether to remove duplicates
12
- structureByLevel = true, // Whether to structure by heading level
13
12
  enhanceDuplicates = true, // Whether to enhance duplicate titles with context
13
+ buildHierarchy = true, // Whether to build the hierarchy tree
14
14
  } = options;
15
15
  if (!tocEntries || tocEntries.length === 0) {
16
16
  return [];
@@ -19,7 +19,7 @@ const optimizeTocArray = (tocEntries, reporter, options = {}) => {
19
19
  if (tocEntries.length > warnThreshold) {
20
20
  reporter.warn(`Large table of contents detected (${tocEntries.length} items). This might affect performance.`);
21
21
  }
22
- // Enrich TOC entries with level information if not present
22
+ // Pre-process all entries to ensure proper levels
23
23
  const enrichedTocEntries = tocEntries.map((entry) => {
24
24
  if (!entry.level) {
25
25
  const level = entry.type === "heading_1"
@@ -33,21 +33,12 @@ const optimizeTocArray = (tocEntries, reporter, options = {}) => {
33
33
  }
34
34
  return entry;
35
35
  });
36
- // Sort by appearance (using array index as proxy) and then by level
37
- enrichedTocEntries.sort((a, b) => {
38
- // Sort by level to ensure headers are properly nested
39
- return (a.level || 0) - (b.level || 0);
40
- });
41
- // Add context to duplicate title entries for better display
42
- if (enhanceDuplicates) {
43
- (0, exports.enrichDuplicateTitles)(enrichedTocEntries, reporter);
44
- }
45
36
  // Remove duplicates if requested
46
37
  let processedToc = enrichedTocEntries;
47
38
  if (removeDuplicates) {
48
39
  const startTime = Date.now();
49
40
  const uniqueMap = new Map();
50
- // Use the hash to ensure uniqueness, but preserve context when display titles are the same
41
+ // Use the hash to ensure uniqueness, but preserve context
51
42
  for (const entry of enrichedTocEntries) {
52
43
  uniqueMap.set(entry.hash, entry);
53
44
  }
@@ -58,11 +49,30 @@ const optimizeTocArray = (tocEntries, reporter, options = {}) => {
58
49
  reporter.info(`Removed ${removedCount} duplicate TOC entries in ${processTime}ms.`);
59
50
  }
60
51
  }
52
+ // Add context to duplicate title entries for better display
53
+ if (enhanceDuplicates) {
54
+ (0, exports.enrichDuplicateTitles)(processedToc, reporter);
55
+ }
61
56
  // Limit size if necessary
62
57
  if (processedToc.length > maxSize) {
63
58
  reporter.warn(`Table of contents exceeds maximum size (${processedToc.length} > ${maxSize}). Truncating.`);
64
59
  processedToc = processedToc.slice(0, maxSize);
65
60
  }
61
+ // Build the hierarchical structure if requested
62
+ if (buildHierarchy) {
63
+ // First level sort - by level, then by approximate order
64
+ processedToc.sort((a, b) => {
65
+ if ((a.level || 0) !== (b.level || 0)) {
66
+ return (a.level || 0) - (b.level || 0);
67
+ }
68
+ return 0; // Keep original order for same level
69
+ });
70
+ // 확인을 위한 로깅
71
+ const h1Count = processedToc.filter((item) => item.level === 1).length;
72
+ const h2Count = processedToc.filter((item) => item.level === 2).length;
73
+ const h3Count = processedToc.filter((item) => item.level === 3).length;
74
+ reporter.info(`TOC structure: ${h1Count} h1, ${h2Count} h2, ${h3Count} h3 headings`);
75
+ }
66
76
  return processedToc;
67
77
  };
68
78
  exports.optimizeTocArray = optimizeTocArray;
@@ -91,9 +101,9 @@ const enrichDuplicateTitles = (tocEntries, reporter) => {
91
101
  for (const entry of tocEntries) {
92
102
  entryMap.set(entry.hash, entry);
93
103
  }
94
- // Create a hierarchical map to find parents
104
+ // Process each entry to ensure all duplicate titles have context
95
105
  for (const entry of tocEntries) {
96
- if (duplicateTitles.has(entry.title)) {
106
+ if (duplicateTitles.has(entry.title) && !entry.contextTitle) {
97
107
  let context = "";
98
108
  let parentEntry;
99
109
  // Find parent context
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.2.4",
4
+ "version": "1.2.5",
5
5
  "skipLibCheck": true,
6
6
  "license": "0BSD",
7
7
  "main": "./dist/gatsby-node.js",