n8n-nodes-notion-advanced 1.2.1-beta → 1.2.3-beta

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.
@@ -1,1121 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NotionAITool = void 0;
4
- const n8n_workflow_1 = require("n8n-workflow");
5
- const NotionUtils_1 = require("./NotionUtils");
6
- class NotionAITool {
7
- constructor() {
8
- this.description = {
9
- displayName: 'Notion AI Tool',
10
- name: 'notionAiTool',
11
- icon: 'file:notion.svg',
12
- group: ['ai'],
13
- version: 1,
14
- subtitle: '={{$parameter["operation"]}}',
15
- description: 'AI-powered tool for creating and managing Notion content. Designed for use with AI Agent Nodes.',
16
- defaults: {
17
- name: 'Notion AI Tool',
18
- },
19
- inputs: ['main'],
20
- outputs: ['main'],
21
- usableAsTool: true,
22
- codex: {
23
- categories: ['Productivity', 'AI', 'Documentation'],
24
- subcategories: {
25
- 'Productivity': ['Notion', 'Knowledge Management'],
26
- 'AI': ['AI Agent Tools', 'Natural Language Processing'],
27
- 'Documentation': ['Page Creation', 'Content Management']
28
- },
29
- resources: {
30
- primaryDocumentation: [
31
- {
32
- url: 'https://github.com/AZ-IT-US/n8n-notion-advanced-node#ai-tool-usage',
33
- },
34
- ],
35
- },
36
- alias: ['notion', 'productivity', 'ai-tool', 'pages', 'database'],
37
- },
38
- credentials: [
39
- {
40
- name: 'notionApi',
41
- required: true,
42
- },
43
- ],
44
- properties: [
45
- {
46
- displayName: 'Operation',
47
- name: 'operation',
48
- type: 'options',
49
- noDataExpression: true,
50
- options: [
51
- {
52
- name: 'Create Page with Content',
53
- value: 'createPageWithContent',
54
- description: 'Create a new Notion page with structured content including text, headings, lists, and formatting',
55
- action: 'Create a Notion page with content',
56
- },
57
- {
58
- name: 'Add Content to Page',
59
- value: 'addContentToPage',
60
- description: 'Append new content blocks (paragraphs, headings, lists, etc.) to an existing Notion page',
61
- action: 'Add content to existing page',
62
- },
63
- {
64
- name: 'Search and Retrieve Pages',
65
- value: 'searchPages',
66
- description: 'Search for Notion pages by title, content, or properties and retrieve their information',
67
- action: 'Search and retrieve pages',
68
- },
69
- {
70
- name: 'Update Page Properties',
71
- value: 'updatePageProperties',
72
- description: 'Update page title, properties, status, tags, or other metadata',
73
- action: 'Update page properties',
74
- },
75
- {
76
- name: 'Create Database Entry',
77
- value: 'createDatabaseEntry',
78
- description: 'Create a new entry in a Notion database with specified properties and values',
79
- action: 'Create database entry',
80
- },
81
- {
82
- name: 'Query Database',
83
- value: 'queryDatabase',
84
- description: 'Search and filter database entries based on criteria and retrieve matching records',
85
- action: 'Query database',
86
- },
87
- ],
88
- default: 'createPageWithContent',
89
- },
90
- // CREATE PAGE WITH CONTENT
91
- {
92
- displayName: 'Page Title',
93
- name: 'pageTitle',
94
- type: 'string',
95
- required: true,
96
- displayOptions: {
97
- show: {
98
- operation: ['createPageWithContent'],
99
- },
100
- },
101
- default: '',
102
- description: 'The title of the new page to create',
103
- },
104
- {
105
- displayName: 'Parent Page/Database ID',
106
- name: 'parentId',
107
- type: 'string',
108
- required: true,
109
- displayOptions: {
110
- show: {
111
- operation: ['createPageWithContent', 'createDatabaseEntry'],
112
- },
113
- },
114
- default: '',
115
- description: 'ID of the parent page or database where this should be created. Can be a Notion URL or page ID.',
116
- },
117
- {
118
- displayName: 'Content',
119
- name: 'content',
120
- type: 'string',
121
- typeOptions: {
122
- rows: 6,
123
- },
124
- displayOptions: {
125
- show: {
126
- operation: ['createPageWithContent', 'addContentToPage'],
127
- },
128
- },
129
- default: '',
130
- description: 'The content to add. Use natural language - AI will structure it into appropriate blocks (headings, paragraphs, lists, etc.)',
131
- placeholder: 'Example:\n# Main Heading\nThis is a paragraph with **bold** and *italic* text.\n\n## Subheading\n- First bullet point\n- Second bullet point\n\n> This is a quote block',
132
- },
133
- // ADD CONTENT TO PAGE
134
- {
135
- displayName: 'Target Page ID',
136
- name: 'targetPageId',
137
- type: 'string',
138
- required: true,
139
- displayOptions: {
140
- show: {
141
- operation: ['addContentToPage', 'updatePageProperties'],
142
- },
143
- },
144
- default: '',
145
- description: 'ID or URL of the existing page to modify',
146
- },
147
- // SEARCH PAGES
148
- {
149
- displayName: 'Search Query',
150
- name: 'searchQuery',
151
- type: 'string',
152
- displayOptions: {
153
- show: {
154
- operation: ['searchPages'],
155
- },
156
- },
157
- default: '',
158
- description: 'Search terms to find pages. Leave empty to get all pages.',
159
- },
160
- // UPDATE PAGE PROPERTIES
161
- {
162
- displayName: 'Properties to Update',
163
- name: 'propertiesToUpdate',
164
- type: 'string',
165
- typeOptions: {
166
- rows: 4,
167
- },
168
- displayOptions: {
169
- show: {
170
- operation: ['updatePageProperties'],
171
- },
172
- },
173
- default: '',
174
- description: 'Properties to update in JSON format or natural language. Example: {"status": "In Progress", "priority": "High"} or "Set status to Done and priority to Low"',
175
- },
176
- // DATABASE OPERATIONS
177
- {
178
- displayName: 'Database ID',
179
- name: 'databaseId',
180
- type: 'string',
181
- required: true,
182
- displayOptions: {
183
- show: {
184
- operation: ['queryDatabase'],
185
- },
186
- },
187
- default: '',
188
- description: 'ID or URL of the database to query',
189
- },
190
- {
191
- displayName: 'Entry Properties',
192
- name: 'entryProperties',
193
- type: 'string',
194
- typeOptions: {
195
- rows: 4,
196
- },
197
- displayOptions: {
198
- show: {
199
- operation: ['createDatabaseEntry'],
200
- },
201
- },
202
- default: '',
203
- description: 'Properties for the new database entry in JSON format or natural language description',
204
- },
205
- {
206
- displayName: 'Query Filter',
207
- name: 'queryFilter',
208
- type: 'string',
209
- displayOptions: {
210
- show: {
211
- operation: ['queryDatabase'],
212
- },
213
- },
214
- default: '',
215
- description: 'Filter criteria in natural language (e.g., "status is Done and priority is High") or JSON format',
216
- },
217
- // COMMON OPTIONS
218
- {
219
- displayName: 'Additional Options',
220
- name: 'additionalOptions',
221
- type: 'collection',
222
- placeholder: 'Add Option',
223
- default: {},
224
- options: [
225
- {
226
- displayName: 'Icon',
227
- name: 'icon',
228
- type: 'string',
229
- default: '',
230
- description: 'Emoji icon for the page (e.g., 📝, 🎯, 📊)',
231
- },
232
- {
233
- displayName: 'Cover Image URL',
234
- name: 'coverUrl',
235
- type: 'string',
236
- default: '',
237
- description: 'URL of cover image for the page',
238
- },
239
- {
240
- displayName: 'Max Results',
241
- name: 'maxResults',
242
- type: 'number',
243
- default: 20,
244
- description: 'Maximum number of results to return (1-100)',
245
- },
246
- ],
247
- },
248
- ],
249
- };
250
- }
251
- async execute() {
252
- const items = this.getInputData();
253
- const responseData = [];
254
- // Validate credentials
255
- const isValid = await NotionUtils_1.validateCredentials.call(this);
256
- if (!isValid) {
257
- throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Invalid Notion API credentials');
258
- }
259
- for (let i = 0; i < items.length; i++) {
260
- try {
261
- const operation = this.getNodeParameter('operation', i);
262
- let result;
263
- switch (operation) {
264
- case 'createPageWithContent':
265
- result = await NotionAITool.createPageWithContent(this, i);
266
- break;
267
- case 'addContentToPage':
268
- result = await NotionAITool.addContentToPage(this, i);
269
- break;
270
- case 'searchPages':
271
- result = await NotionAITool.searchPages(this, i);
272
- break;
273
- case 'updatePageProperties':
274
- result = await NotionAITool.updatePageProperties(this, i);
275
- break;
276
- case 'createDatabaseEntry':
277
- result = await NotionAITool.createDatabaseEntry(this, i);
278
- break;
279
- case 'queryDatabase':
280
- result = await NotionAITool.queryDatabase(this, i);
281
- break;
282
- default:
283
- throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unknown operation: ${operation}`);
284
- }
285
- responseData.push({
286
- operation,
287
- success: true,
288
- ...result,
289
- });
290
- }
291
- catch (error) {
292
- if (this.continueOnFail()) {
293
- responseData.push({
294
- error: error.message,
295
- success: false,
296
- });
297
- }
298
- else {
299
- throw error;
300
- }
301
- }
302
- }
303
- return [this.helpers.returnJsonArray(responseData)];
304
- }
305
- static async createPageWithContent(executeFunctions, itemIndex) {
306
- const pageTitle = executeFunctions.getNodeParameter('pageTitle', itemIndex);
307
- const parentId = executeFunctions.getNodeParameter('parentId', itemIndex);
308
- const content = executeFunctions.getNodeParameter('content', itemIndex, '');
309
- const additionalOptions = executeFunctions.getNodeParameter('additionalOptions', itemIndex, {});
310
- const resolvedParentId = await NotionUtils_1.resolvePageId.call(executeFunctions, parentId);
311
- // Create the page first
312
- const pageBody = {
313
- parent: { page_id: resolvedParentId },
314
- properties: {
315
- title: {
316
- title: [(0, NotionUtils_1.createRichText)(pageTitle)],
317
- },
318
- },
319
- };
320
- // Add icon and cover if provided
321
- if (additionalOptions.icon) {
322
- pageBody.icon = { type: 'emoji', emoji: additionalOptions.icon };
323
- }
324
- if (additionalOptions.coverUrl) {
325
- pageBody.cover = { type: 'external', external: { url: additionalOptions.coverUrl } };
326
- }
327
- const page = await NotionUtils_1.notionApiRequest.call(executeFunctions, 'POST', '/pages', pageBody);
328
- // If content is provided, add it to the page
329
- if (content) {
330
- const blocks = NotionAITool.parseContentToBlocks(content);
331
- if (blocks.length > 0) {
332
- await NotionUtils_1.notionApiRequest.call(executeFunctions, 'PATCH', `/blocks/${page.id}/children`, {
333
- children: blocks,
334
- });
335
- }
336
- }
337
- return {
338
- pageId: page.id,
339
- title: pageTitle,
340
- url: page.url,
341
- message: `Created page "${pageTitle}" with content`,
342
- };
343
- }
344
- static async addContentToPage(executeFunctions, itemIndex) {
345
- const targetPageId = executeFunctions.getNodeParameter('targetPageId', itemIndex);
346
- const content = executeFunctions.getNodeParameter('content', itemIndex);
347
- const resolvedPageId = await NotionUtils_1.resolvePageId.call(executeFunctions, targetPageId);
348
- const blocks = NotionAITool.parseContentToBlocks(content);
349
- if (blocks.length === 0) {
350
- throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), 'No valid content blocks found to add');
351
- }
352
- const result = await NotionUtils_1.notionApiRequest.call(executeFunctions, 'PATCH', `/blocks/${resolvedPageId}/children`, {
353
- children: blocks,
354
- });
355
- return {
356
- pageId: resolvedPageId,
357
- blocksAdded: blocks.length,
358
- message: `Added ${blocks.length} content blocks to page`,
359
- result,
360
- };
361
- }
362
- static async searchPages(executeFunctions, itemIndex) {
363
- var _a, _b;
364
- const searchQuery = executeFunctions.getNodeParameter('searchQuery', itemIndex, '');
365
- const additionalOptions = executeFunctions.getNodeParameter('additionalOptions', itemIndex, {});
366
- const maxResults = additionalOptions.maxResults || 20;
367
- const body = {
368
- page_size: Math.min(maxResults, 100),
369
- };
370
- if (searchQuery) {
371
- body.query = searchQuery;
372
- }
373
- body.filter = {
374
- property: 'object',
375
- value: 'page',
376
- };
377
- const response = await NotionUtils_1.notionApiRequest.call(executeFunctions, 'POST', '/search', body);
378
- return {
379
- totalResults: ((_a = response.results) === null || _a === void 0 ? void 0 : _a.length) || 0,
380
- pages: response.results || [],
381
- message: `Found ${((_b = response.results) === null || _b === void 0 ? void 0 : _b.length) || 0} pages`,
382
- };
383
- }
384
- static async updatePageProperties(executeFunctions, itemIndex) {
385
- const targetPageId = executeFunctions.getNodeParameter('targetPageId', itemIndex);
386
- const propertiesToUpdate = executeFunctions.getNodeParameter('propertiesToUpdate', itemIndex);
387
- const resolvedPageId = await NotionUtils_1.resolvePageId.call(executeFunctions, targetPageId);
388
- const properties = NotionAITool.parsePropertiesToUpdate(propertiesToUpdate);
389
- const result = await NotionUtils_1.notionApiRequest.call(executeFunctions, 'PATCH', `/pages/${resolvedPageId}`, {
390
- properties,
391
- });
392
- return {
393
- pageId: resolvedPageId,
394
- updatedProperties: Object.keys(properties),
395
- message: `Updated ${Object.keys(properties).length} properties`,
396
- result,
397
- };
398
- }
399
- static async createDatabaseEntry(executeFunctions, itemIndex) {
400
- const parentId = executeFunctions.getNodeParameter('parentId', itemIndex);
401
- const entryProperties = executeFunctions.getNodeParameter('entryProperties', itemIndex);
402
- const resolvedParentId = await NotionUtils_1.resolvePageId.call(executeFunctions, parentId);
403
- const properties = NotionAITool.parsePropertiesToUpdate(entryProperties);
404
- const result = await NotionUtils_1.notionApiRequest.call(executeFunctions, 'POST', '/pages', {
405
- parent: { database_id: resolvedParentId },
406
- properties,
407
- });
408
- return {
409
- entryId: result.id,
410
- databaseId: resolvedParentId,
411
- message: 'Created new database entry',
412
- result,
413
- };
414
- }
415
- static async queryDatabase(executeFunctions, itemIndex) {
416
- var _a, _b;
417
- const databaseId = executeFunctions.getNodeParameter('databaseId', itemIndex);
418
- const queryFilter = executeFunctions.getNodeParameter('queryFilter', itemIndex, '');
419
- const additionalOptions = executeFunctions.getNodeParameter('additionalOptions', itemIndex, {});
420
- const maxResults = additionalOptions.maxResults || 20;
421
- const resolvedDatabaseId = await NotionUtils_1.resolvePageId.call(executeFunctions, databaseId);
422
- const body = {
423
- page_size: Math.min(maxResults, 100),
424
- };
425
- if (queryFilter) {
426
- try {
427
- body.filter = JSON.parse(queryFilter);
428
- }
429
- catch {
430
- // If not JSON, create a simple text filter
431
- body.filter = {
432
- property: 'Name',
433
- title: {
434
- contains: queryFilter,
435
- },
436
- };
437
- }
438
- }
439
- const response = await NotionUtils_1.notionApiRequest.call(executeFunctions, 'POST', `/databases/${resolvedDatabaseId}/query`, body);
440
- return {
441
- databaseId: resolvedDatabaseId,
442
- totalResults: ((_a = response.results) === null || _a === void 0 ? void 0 : _a.length) || 0,
443
- entries: response.results || [],
444
- message: `Found ${((_b = response.results) === null || _b === void 0 ? void 0 : _b.length) || 0} database entries`,
445
- };
446
- }
447
- static parseContentToBlocks(content) {
448
- const blocks = [];
449
- // Handle both actual newlines and escaped \n characters
450
- const normalizedContent = content.replace(/\\n/g, '\n');
451
- // First, process XML-like tags for reliable parsing
452
- const processedContent = NotionAITool.processXmlTags(normalizedContent, blocks);
453
- // Then process remaining content with traditional markdown patterns
454
- const lines = processedContent.split('\n');
455
- for (let i = 0; i < lines.length; i++) {
456
- const line = lines[i];
457
- const trimmedLine = line.trim();
458
- // Skip completely empty lines and XML placeholders
459
- if (!trimmedLine || trimmedLine.startsWith('__XML_BLOCK_'))
460
- continue;
461
- // Traditional markdown patterns (for backwards compatibility)
462
- if (trimmedLine.startsWith('# ')) {
463
- blocks.push({
464
- type: 'heading_1',
465
- heading_1: {
466
- rich_text: [(0, NotionUtils_1.createRichText)(trimmedLine.substring(2).trim())],
467
- },
468
- });
469
- }
470
- else if (trimmedLine.startsWith('## ')) {
471
- blocks.push({
472
- type: 'heading_2',
473
- heading_2: {
474
- rich_text: [(0, NotionUtils_1.createRichText)(trimmedLine.substring(3).trim())],
475
- },
476
- });
477
- }
478
- else if (trimmedLine.startsWith('### ')) {
479
- blocks.push({
480
- type: 'heading_3',
481
- heading_3: {
482
- rich_text: [(0, NotionUtils_1.createRichText)(trimmedLine.substring(4).trim())],
483
- },
484
- });
485
- }
486
- else if (trimmedLine === '---' || trimmedLine === '***') {
487
- blocks.push({
488
- type: 'divider',
489
- divider: {},
490
- });
491
- }
492
- else if (trimmedLine.includes('[!') && trimmedLine.startsWith('>')) {
493
- // Callout blocks: > [!info] content
494
- const calloutMatch = trimmedLine.match(/^>\s*\[!(\w+)\]\s*(.*)/i);
495
- if (calloutMatch) {
496
- const [, calloutType, text] = calloutMatch;
497
- const emoji = NotionAITool.getCalloutEmoji(calloutType.toLowerCase());
498
- const color = NotionAITool.getCalloutColor(calloutType.toLowerCase());
499
- blocks.push({
500
- type: 'callout',
501
- callout: {
502
- rich_text: NotionAITool.parseBasicMarkdown(text),
503
- icon: { type: 'emoji', emoji },
504
- color: color,
505
- },
506
- });
507
- }
508
- else {
509
- blocks.push({
510
- type: 'quote',
511
- quote: {
512
- rich_text: NotionAITool.parseBasicMarkdown(trimmedLine.substring(1).trim()),
513
- },
514
- });
515
- }
516
- }
517
- else if (trimmedLine.startsWith('![') && trimmedLine.includes('](') && trimmedLine.endsWith(')')) {
518
- // Image: ![alt text](url)
519
- const match = trimmedLine.match(/^!\[(.*?)\]\((.*?)\)$/);
520
- if (match) {
521
- const [, altText, url] = match;
522
- blocks.push({
523
- type: 'image',
524
- image: {
525
- type: 'external',
526
- external: { url },
527
- caption: altText ? NotionAITool.parseBasicMarkdown(altText) : [],
528
- },
529
- });
530
- }
531
- }
532
- else if (trimmedLine.startsWith('$$') && trimmedLine.endsWith('$$') && trimmedLine.length > 4) {
533
- // Equation: $$equation$$
534
- const equation = trimmedLine.substring(2, trimmedLine.length - 2).trim();
535
- blocks.push({
536
- type: 'equation',
537
- equation: {
538
- expression: equation,
539
- },
540
- });
541
- }
542
- else if ((trimmedLine.startsWith('http://') || trimmedLine.startsWith('https://')) && !trimmedLine.includes(' ')) {
543
- // Check if it's a video URL for embed, otherwise bookmark
544
- const videoPatterns = [
545
- /(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/watch\?v=|youtu\.be\/)/i,
546
- /(?:https?:\/\/)?(?:www\.)?(?:vimeo\.com\/)/i,
547
- /(?:https?:\/\/)?(?:www\.)?(?:dailymotion\.com\/video\/)/i,
548
- /(?:https?:\/\/)?(?:www\.)?(?:twitch\.tv\/)/i,
549
- /(?:https?:\/\/)?(?:www\.)?(?:loom\.com\/share\/)/i,
550
- /(?:https?:\/\/)?(?:www\.)?(?:figma\.com\/)/i,
551
- /(?:https?:\/\/)?(?:www\.)?(?:miro\.com\/)/i,
552
- /(?:https?:\/\/)?(?:codepen\.io\/)/i
553
- ];
554
- const isEmbeddableUrl = videoPatterns.some(pattern => pattern.test(trimmedLine));
555
- if (isEmbeddableUrl) {
556
- blocks.push({
557
- type: 'embed',
558
- embed: {
559
- url: trimmedLine,
560
- },
561
- });
562
- }
563
- else {
564
- blocks.push({
565
- type: 'bookmark',
566
- bookmark: {
567
- url: trimmedLine,
568
- },
569
- });
570
- }
571
- }
572
- else if (trimmedLine.startsWith('- [') && (trimmedLine.includes('[ ]') || trimmedLine.includes('[x]') || trimmedLine.includes('[X]'))) {
573
- // To-do list items: - [ ] or - [x] or - [X]
574
- const isChecked = trimmedLine.includes('[x]') || trimmedLine.includes('[X]');
575
- const text = trimmedLine.replace(/^-\s*\[[ xX]\]\s*/, '').trim();
576
- blocks.push({
577
- type: 'to_do',
578
- to_do: {
579
- rich_text: NotionAITool.parseBasicMarkdown(text),
580
- checked: isChecked,
581
- },
582
- });
583
- }
584
- else if (trimmedLine.startsWith('- ') && !trimmedLine.startsWith('- [')) {
585
- // Bullet list items: - item (but not todos)
586
- const listText = trimmedLine.substring(2).trim();
587
- blocks.push({
588
- type: 'bulleted_list_item',
589
- bulleted_list_item: {
590
- rich_text: NotionAITool.parseBasicMarkdown(listText),
591
- },
592
- });
593
- }
594
- else if (/^\d+\.\s/.test(trimmedLine)) {
595
- // Numbered list items: 1. item
596
- const listText = trimmedLine.replace(/^\d+\.\s/, '').trim();
597
- blocks.push({
598
- type: 'numbered_list_item',
599
- numbered_list_item: {
600
- rich_text: NotionAITool.parseBasicMarkdown(listText),
601
- },
602
- });
603
- }
604
- else if (trimmedLine.startsWith('> ') && !trimmedLine.includes('[!')) {
605
- // Quote block (but not callout)
606
- blocks.push({
607
- type: 'quote',
608
- quote: {
609
- rich_text: NotionAITool.parseBasicMarkdown(trimmedLine.substring(2).trim()),
610
- },
611
- });
612
- }
613
- else if (trimmedLine.startsWith('```')) {
614
- // Handle code blocks
615
- const language = trimmedLine.substring(3).trim() || 'plain text';
616
- const codeLines = [];
617
- i++; // Skip the opening ```
618
- // Collect all code lines until closing ```
619
- while (i < lines.length && !lines[i].trim().startsWith('```')) {
620
- codeLines.push(lines[i]);
621
- i++;
622
- }
623
- blocks.push({
624
- type: 'code',
625
- code: {
626
- rich_text: [(0, NotionUtils_1.createRichText)(codeLines.join('\n'))],
627
- language: language === 'plain text' ? 'plain_text' : language,
628
- },
629
- });
630
- }
631
- else {
632
- // Regular paragraph - handle basic markdown formatting
633
- const richText = NotionAITool.parseBasicMarkdown(trimmedLine);
634
- blocks.push({
635
- type: 'paragraph',
636
- paragraph: {
637
- rich_text: richText,
638
- },
639
- });
640
- }
641
- }
642
- return blocks;
643
- }
644
- // New XML-like tag processing function
645
- static processXmlTags(content, blocks) {
646
- let processedContent = content;
647
- let blockCounter = 0;
648
- // Process XML-like tags in order of priority
649
- const tagProcessors = [
650
- // Callouts: <callout type="info">content</callout>
651
- {
652
- regex: /<callout\s*(?:type="([^"]*)")?\s*>(.*?)<\/callout>/gis,
653
- processor: (match, type = 'info', content) => {
654
- const emoji = NotionAITool.getCalloutEmoji(type.toLowerCase());
655
- const color = NotionAITool.getCalloutColor(type.toLowerCase());
656
- blocks.push({
657
- type: 'callout',
658
- callout: {
659
- rich_text: NotionAITool.parseBasicMarkdown(content.trim()),
660
- icon: { type: 'emoji', emoji },
661
- color: color,
662
- },
663
- });
664
- return `__XML_BLOCK_${blockCounter++}__`;
665
- }
666
- },
667
- // Code blocks: <code language="javascript">content</code>
668
- {
669
- regex: /<code\s*(?:language="([^"]*)")?\s*>(.*?)<\/code>/gis,
670
- processor: (match, language = 'plain_text', content) => {
671
- blocks.push({
672
- type: 'code',
673
- code: {
674
- rich_text: [(0, NotionUtils_1.createRichText)(content.trim())],
675
- language: language === 'plain text' ? 'plain_text' : language,
676
- },
677
- });
678
- return `__XML_BLOCK_${blockCounter++}__`;
679
- }
680
- },
681
- // Images: <image src="url" alt="description">caption</image>
682
- {
683
- regex: /<image\s+src="([^"]*)"(?:\s+alt="([^"]*)")?\s*>(.*?)<\/image>/gis,
684
- processor: (match, src, alt = '', caption = '') => {
685
- const captionText = caption.trim() || alt;
686
- blocks.push({
687
- type: 'image',
688
- image: {
689
- type: 'external',
690
- external: { url: src },
691
- caption: captionText ? NotionAITool.parseBasicMarkdown(captionText) : [],
692
- },
693
- });
694
- return `__XML_BLOCK_${blockCounter++}__`;
695
- }
696
- },
697
- // Self-closing images: <image src="url" alt="description"/>
698
- {
699
- regex: /<image\s+src="([^"]*)"(?:\s+alt="([^"]*)")?\s*\/>/gis,
700
- processor: (match, src, alt = '') => {
701
- blocks.push({
702
- type: 'image',
703
- image: {
704
- type: 'external',
705
- external: { url: src },
706
- caption: alt ? NotionAITool.parseBasicMarkdown(alt) : [],
707
- },
708
- });
709
- return `__XML_BLOCK_${blockCounter++}__`;
710
- }
711
- },
712
- // Equations: <equation>E=mc^2</equation>
713
- {
714
- regex: /<equation>(.*?)<\/equation>/gis,
715
- processor: (match, expression) => {
716
- blocks.push({
717
- type: 'equation',
718
- equation: {
719
- expression: expression.trim(),
720
- },
721
- });
722
- return `__XML_BLOCK_${blockCounter++}__`;
723
- }
724
- },
725
- // Embeds: <embed>url</embed>
726
- {
727
- regex: /<embed>(.*?)<\/embed>/gis,
728
- processor: (match, url) => {
729
- blocks.push({
730
- type: 'embed',
731
- embed: {
732
- url: url.trim(),
733
- },
734
- });
735
- return `__XML_BLOCK_${blockCounter++}__`;
736
- }
737
- },
738
- // Bookmarks: <bookmark>url</bookmark>
739
- {
740
- regex: /<bookmark>(.*?)<\/bookmark>/gis,
741
- processor: (match, url) => {
742
- blocks.push({
743
- type: 'bookmark',
744
- bookmark: {
745
- url: url.trim(),
746
- },
747
- });
748
- return `__XML_BLOCK_${blockCounter++}__`;
749
- }
750
- },
751
- // Toggles: <toggle>title</toggle>
752
- {
753
- regex: /<toggle>(.*?)<\/toggle>/gis,
754
- processor: (match, title) => {
755
- blocks.push({
756
- type: 'toggle',
757
- toggle: {
758
- rich_text: NotionAITool.parseBasicMarkdown(title.trim()),
759
- children: [],
760
- },
761
- });
762
- return `__XML_BLOCK_${blockCounter++}__`;
763
- }
764
- },
765
- // Quotes: <quote>content</quote>
766
- {
767
- regex: /<quote>(.*?)<\/quote>/gis,
768
- processor: (match, content) => {
769
- blocks.push({
770
- type: 'quote',
771
- quote: {
772
- rich_text: NotionAITool.parseBasicMarkdown(content.trim()),
773
- },
774
- });
775
- return `__XML_BLOCK_${blockCounter++}__`;
776
- }
777
- },
778
- // Dividers: <divider/> or <divider></divider>
779
- {
780
- regex: /<divider\s*\/?>/gis,
781
- processor: (match) => {
782
- blocks.push({
783
- type: 'divider',
784
- divider: {},
785
- });
786
- return `__XML_BLOCK_${blockCounter++}__`;
787
- }
788
- },
789
- // To-do items: <todo checked="true">content</todo>
790
- {
791
- regex: /<todo\s*(?:checked="([^"]*)")?\s*>(.*?)<\/todo>/gis,
792
- processor: (match, checked = 'false', content) => {
793
- const isChecked = checked.toLowerCase() === 'true';
794
- blocks.push({
795
- type: 'to_do',
796
- to_do: {
797
- rich_text: NotionAITool.parseBasicMarkdown(content.trim()),
798
- checked: isChecked,
799
- },
800
- });
801
- return `__XML_BLOCK_${blockCounter++}__`;
802
- }
803
- },
804
- // Headings: <h1>content</h1>, <h2>content</h2>, <h3>content</h3>
805
- {
806
- regex: /<h([123])>(.*?)<\/h[123]>/gis,
807
- processor: (match, level, content) => {
808
- const headingType = `heading_${level}`;
809
- blocks.push({
810
- type: headingType,
811
- [headingType]: {
812
- rich_text: [(0, NotionUtils_1.createRichText)(content.trim())],
813
- },
814
- });
815
- return `__XML_BLOCK_${blockCounter++}__`;
816
- }
817
- },
818
- // Paragraphs: <p>content</p>
819
- {
820
- regex: /<p>(.*?)<\/p>/gis,
821
- processor: (match, content) => {
822
- blocks.push({
823
- type: 'paragraph',
824
- paragraph: {
825
- rich_text: NotionAITool.parseBasicMarkdown(content.trim()),
826
- },
827
- });
828
- return `__XML_BLOCK_${blockCounter++}__`;
829
- }
830
- },
831
- // Bulleted lists: <ul><li>item</li></ul>
832
- {
833
- regex: /<ul>(.*?)<\/ul>/gis,
834
- processor: (match, listContent) => {
835
- // Extract individual list items
836
- const items = listContent.match(/<li>(.*?)<\/li>/gis) || [];
837
- items.forEach(item => {
838
- const itemContent = item.replace(/<\/?li>/gi, '').trim();
839
- blocks.push({
840
- type: 'bulleted_list_item',
841
- bulleted_list_item: {
842
- rich_text: NotionAITool.parseBasicMarkdown(itemContent),
843
- },
844
- });
845
- });
846
- return `__XML_BLOCK_${blockCounter++}__`;
847
- }
848
- },
849
- // Numbered lists: <ol><li>item</li></ol>
850
- {
851
- regex: /<ol>(.*?)<\/ol>/gis,
852
- processor: (match, listContent) => {
853
- // Extract individual list items
854
- const items = listContent.match(/<li>(.*?)<\/li>/gis) || [];
855
- items.forEach(item => {
856
- const itemContent = item.replace(/<\/?li>/gi, '').trim();
857
- blocks.push({
858
- type: 'numbered_list_item',
859
- numbered_list_item: {
860
- rich_text: NotionAITool.parseBasicMarkdown(itemContent),
861
- },
862
- });
863
- });
864
- return `__XML_BLOCK_${blockCounter++}__`;
865
- }
866
- },
867
- // Standalone list items: <li>content</li>
868
- {
869
- regex: /<li>(.*?)<\/li>/gis,
870
- processor: (match, content) => {
871
- blocks.push({
872
- type: 'bulleted_list_item',
873
- bulleted_list_item: {
874
- rich_text: NotionAITool.parseBasicMarkdown(content.trim()),
875
- },
876
- });
877
- return `__XML_BLOCK_${blockCounter++}__`;
878
- }
879
- },
880
- // Blockquotes: <blockquote>content</blockquote>
881
- {
882
- regex: /<blockquote>(.*?)<\/blockquote>/gis,
883
- processor: (match, content) => {
884
- blocks.push({
885
- type: 'quote',
886
- quote: {
887
- rich_text: NotionAITool.parseBasicMarkdown(content.trim()),
888
- },
889
- });
890
- return `__XML_BLOCK_${blockCounter++}__`;
891
- }
892
- },
893
- // Preformatted text: <pre>content</pre>
894
- {
895
- regex: /<pre>(.*?)<\/pre>/gis,
896
- processor: (match, content) => {
897
- blocks.push({
898
- type: 'code',
899
- code: {
900
- rich_text: [(0, NotionUtils_1.createRichText)(content.trim())],
901
- language: 'plain_text',
902
- },
903
- });
904
- return `__XML_BLOCK_${blockCounter++}__`;
905
- }
906
- },
907
- // Strong/Bold: <strong>content</strong> or <b>content</b>
908
- {
909
- regex: /<(strong|b)>(.*?)<\/(strong|b)>/gis,
910
- processor: (match, tag, content) => {
911
- blocks.push({
912
- type: 'paragraph',
913
- paragraph: {
914
- rich_text: NotionAITool.parseBasicMarkdown(`**${content.trim()}**`),
915
- },
916
- });
917
- return `__XML_BLOCK_${blockCounter++}__`;
918
- }
919
- },
920
- // Emphasis/Italic: <em>content</em> or <i>content</i>
921
- {
922
- regex: /<(em|i)>(.*?)<\/(em|i)>/gis,
923
- processor: (match, tag, content) => {
924
- blocks.push({
925
- type: 'paragraph',
926
- paragraph: {
927
- rich_text: NotionAITool.parseBasicMarkdown(`*${content.trim()}*`),
928
- },
929
- });
930
- return `__XML_BLOCK_${blockCounter++}__`;
931
- }
932
- },
933
- // Line breaks: <br/> or <br>
934
- {
935
- regex: /<br\s*\/?>/gis,
936
- processor: (match) => {
937
- blocks.push({
938
- type: 'paragraph',
939
- paragraph: {
940
- rich_text: [(0, NotionUtils_1.createRichText)('')],
941
- },
942
- });
943
- return `__XML_BLOCK_${blockCounter++}__`;
944
- }
945
- },
946
- ];
947
- // Process each tag type
948
- tagProcessors.forEach(({ regex, processor }) => {
949
- processedContent = processedContent.replace(regex, (match, group1, group2, group3) => {
950
- return processor(match, group1 || '', group2 || '', group3 || '');
951
- });
952
- });
953
- return processedContent;
954
- }
955
- // Helper function to get callout emoji based on type
956
- static getCalloutEmoji(type) {
957
- const emojiMap = {
958
- 'info': 'ℹ️',
959
- 'warning': '⚠️',
960
- 'danger': '🚨',
961
- 'error': '❌',
962
- 'note': '📝',
963
- 'tip': '💡',
964
- 'success': '✅',
965
- 'question': '❓',
966
- };
967
- return emojiMap[type] || 'ℹ️';
968
- }
969
- // Helper function to get callout color based on type
970
- static getCalloutColor(type) {
971
- const colorMap = {
972
- 'info': 'blue',
973
- 'warning': 'yellow',
974
- 'danger': 'red',
975
- 'error': 'red',
976
- 'note': 'gray',
977
- 'tip': 'green',
978
- 'success': 'green',
979
- 'question': 'purple',
980
- };
981
- return colorMap[type] || 'gray';
982
- }
983
- // Helper function to parse basic markdown formatting in text
984
- static parseBasicMarkdown(text) {
985
- const richTextObjects = [];
986
- // Find and collect all formatting patterns with their positions
987
- const patterns = [
988
- { regex: /\[([^\]]+)\]\(([^)]+)\)/g, type: 'link' }, // [text](url)
989
- { regex: /\*\*\*([^*]+)\*\*\*/g, type: 'bold_italic' }, // ***bold italic***
990
- { regex: /\*\*([^*]+)\*\*/g, type: 'bold' }, // **bold**
991
- { regex: /\*([^*]+)\*/g, type: 'italic' }, // *italic*
992
- { regex: /~~([^~]+)~~/g, type: 'strikethrough' }, // ~~strikethrough~~
993
- { regex: /`([^`]+)`/g, type: 'code' }, // `code`
994
- ];
995
- const matches = [];
996
- // Collect all matches
997
- patterns.forEach(pattern => {
998
- const regex = new RegExp(pattern.regex.source, 'g');
999
- let match;
1000
- while ((match = regex.exec(text)) !== null) {
1001
- if (pattern.type === 'link') {
1002
- matches.push({
1003
- start: match.index,
1004
- end: match.index + match[0].length,
1005
- text: match[1], // Link text
1006
- type: pattern.type,
1007
- url: match[2] // Link URL
1008
- });
1009
- }
1010
- else {
1011
- matches.push({
1012
- start: match.index,
1013
- end: match.index + match[0].length,
1014
- text: match[1], // Inner text
1015
- type: pattern.type
1016
- });
1017
- }
1018
- }
1019
- });
1020
- // Sort matches by position and resolve overlaps (prefer longer matches)
1021
- matches.sort((a, b) => {
1022
- if (a.start !== b.start)
1023
- return a.start - b.start;
1024
- return (b.end - b.start) - (a.end - a.start); // Prefer longer matches
1025
- });
1026
- // Remove overlapping matches (keep the first/longer one)
1027
- const resolvedMatches = [];
1028
- for (const match of matches) {
1029
- const hasOverlap = resolvedMatches.some(existing => (match.start < existing.end && match.end > existing.start));
1030
- if (!hasOverlap) {
1031
- resolvedMatches.push(match);
1032
- }
1033
- }
1034
- // Sort again by position
1035
- resolvedMatches.sort((a, b) => a.start - b.start);
1036
- // If no formatting found, return simple rich text
1037
- if (resolvedMatches.length === 0) {
1038
- return [(0, NotionUtils_1.createRichText)(text)];
1039
- }
1040
- // Build rich text segments
1041
- let lastIndex = 0;
1042
- resolvedMatches.forEach(match => {
1043
- // Add plain text before this match
1044
- if (match.start > lastIndex) {
1045
- const plainText = text.substring(lastIndex, match.start);
1046
- if (plainText) {
1047
- richTextObjects.push((0, NotionUtils_1.createRichText)(plainText));
1048
- }
1049
- }
1050
- // Add formatted text
1051
- const richTextObj = {
1052
- type: 'text',
1053
- text: { content: match.text },
1054
- annotations: {}
1055
- };
1056
- // Apply formatting based on type
1057
- switch (match.type) {
1058
- case 'bold':
1059
- richTextObj.annotations.bold = true;
1060
- break;
1061
- case 'italic':
1062
- richTextObj.annotations.italic = true;
1063
- break;
1064
- case 'bold_italic':
1065
- richTextObj.annotations.bold = true;
1066
- richTextObj.annotations.italic = true;
1067
- break;
1068
- case 'strikethrough':
1069
- richTextObj.annotations.strikethrough = true;
1070
- break;
1071
- case 'code':
1072
- richTextObj.annotations.code = true;
1073
- break;
1074
- case 'link':
1075
- richTextObj.text.link = { url: match.url };
1076
- break;
1077
- }
1078
- // Clean up empty annotations
1079
- if (Object.keys(richTextObj.annotations).length === 0) {
1080
- delete richTextObj.annotations;
1081
- }
1082
- richTextObjects.push(richTextObj);
1083
- lastIndex = match.end;
1084
- });
1085
- // Add remaining plain text
1086
- if (lastIndex < text.length) {
1087
- const remainingText = text.substring(lastIndex);
1088
- if (remainingText) {
1089
- richTextObjects.push((0, NotionUtils_1.createRichText)(remainingText));
1090
- }
1091
- }
1092
- return richTextObjects.length > 0 ? richTextObjects : [(0, NotionUtils_1.createRichText)(text)];
1093
- }
1094
- static parsePropertiesToUpdate(propertiesString) {
1095
- try {
1096
- // Try to parse as JSON first
1097
- return JSON.parse(propertiesString);
1098
- }
1099
- catch {
1100
- // If not JSON, try to parse natural language
1101
- const properties = {};
1102
- // Simple natural language parsing
1103
- const patterns = [
1104
- /set\s+(\w+)\s+to\s+(.+?)(?:\s+and|$)/gi,
1105
- /(\w+)\s*:\s*(.+?)(?:\s*,|$)/gi,
1106
- /(\w+)\s*=\s*(.+?)(?:\s*,|$)/gi,
1107
- ];
1108
- for (const pattern of patterns) {
1109
- let match;
1110
- while ((match = pattern.exec(propertiesString)) !== null) {
1111
- const [, key, value] = match;
1112
- properties[key.trim()] = {
1113
- rich_text: [(0, NotionUtils_1.createRichText)(value.trim())],
1114
- };
1115
- }
1116
- }
1117
- return properties;
1118
- }
1119
- }
1120
- }
1121
- exports.NotionAITool = NotionAITool;