-
Notifications
You must be signed in to change notification settings - Fork 39
/
block.go
1055 lines (889 loc) · 24.3 KB
/
block.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package notion
import (
"encoding/json"
"errors"
"fmt"
"time"
)
// ErrUnknownBlockType is used when encountering an unknown block type.
var ErrUnknownBlockType = errors.New("unknown block type")
// Block represents content on the Notion platform.
// See: https://developers.notion.com/reference/block
type Block interface {
ID() string
Parent() Parent
CreatedTime() time.Time
CreatedBy() BaseUser
LastEditedBy() BaseUser
LastEditedTime() time.Time
HasChildren() bool
Archived() bool
json.Marshaler
}
type blockDTO struct {
ID string `json:"id,omitempty"`
Parent *Parent `json:"parent,omitempty"`
Type BlockType `json:"type,omitempty"`
CreatedTime *time.Time `json:"created_time,omitempty"`
CreatedBy *BaseUser `json:"created_by,omitempty"`
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
LastEditedBy *BaseUser `json:"last_edited_by,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
Archived *bool `json:"archived,omitempty"`
Paragraph *ParagraphBlock `json:"paragraph,omitempty"`
Heading1 *Heading1Block `json:"heading_1,omitempty"`
Heading2 *Heading2Block `json:"heading_2,omitempty"`
Heading3 *Heading3Block `json:"heading_3,omitempty"`
BulletedListItem *BulletedListItemBlock `json:"bulleted_list_item,omitempty"`
NumberedListItem *NumberedListItemBlock `json:"numbered_list_item,omitempty"`
ToDo *ToDoBlock `json:"to_do,omitempty"`
Toggle *ToggleBlock `json:"toggle,omitempty"`
ChildPage *ChildPageBlock `json:"child_page,omitempty"`
ChildDatabase *ChildDatabaseBlock `json:"child_database,omitempty"`
Callout *CalloutBlock `json:"callout,omitempty"`
Quote *QuoteBlock `json:"quote,omitempty"`
Code *CodeBlock `json:"code,omitempty"`
Embed *EmbedBlock `json:"embed,omitempty"`
Image *ImageBlock `json:"image,omitempty"`
Audio *AudioBlock `json:"audio,omitempty"`
Video *VideoBlock `json:"video,omitempty"`
File *FileBlock `json:"file,omitempty"`
PDF *PDFBlock `json:"pdf,omitempty"`
Bookmark *BookmarkBlock `json:"bookmark,omitempty"`
Equation *EquationBlock `json:"equation,omitempty"`
Divider *DividerBlock `json:"divider,omitempty"`
TableOfContents *TableOfContentsBlock `json:"table_of_contents,omitempty"`
Breadcrumb *BreadcrumbBlock `json:"breadcrumb,omitempty"`
ColumnList *ColumnListBlock `json:"column_list,omitempty"`
Column *ColumnBlock `json:"column,omitempty"`
Table *TableBlock `json:"table,omitempty"`
TableRow *TableRowBlock `json:"table_row,omitempty"`
LinkPreview *LinkPreviewBlock `json:"link_preview,omitempty"`
LinkToPage *LinkToPageBlock `json:"link_to_page,omitempty"`
SyncedBlock *SyncedBlock `json:"synced_block,omitempty"`
Template *TemplateBlock `json:"template,omitempty"`
Unsupported *UnsupportedBlock `json:"unsupported,omitempty"`
}
type baseBlock struct {
id string
parent Parent
createdTime time.Time
createdBy BaseUser
lastEditedTime time.Time
lastEditedBy BaseUser
hasChildren bool
archived bool
}
// ID returns the identifier (UUIDv4) for the block.
func (b baseBlock) ID() string {
return b.id
}
func (b baseBlock) CreatedTime() time.Time {
return b.createdTime
}
func (b baseBlock) CreatedBy() BaseUser {
return b.createdBy
}
func (b baseBlock) LastEditedTime() time.Time {
return b.lastEditedTime
}
func (b baseBlock) LastEditedBy() BaseUser {
return b.lastEditedBy
}
func (b baseBlock) HasChildren() bool {
return b.hasChildren
}
func (b baseBlock) Archived() bool {
return b.archived
}
func (b baseBlock) Parent() Parent {
return b.parent
}
type ParagraphBlock struct {
baseBlock
RichText []RichText `json:"rich_text"`
Children []Block `json:"children,omitempty"`
Color Color `json:"color,omitempty"`
}
// MarshalJSON implements json.Marshaler.
func (b ParagraphBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias ParagraphBlock
dto struct {
Paragraph blockAlias `json:"paragraph"`
}
)
return json.Marshal(dto{
Paragraph: blockAlias(b),
})
}
type BulletedListItemBlock struct {
baseBlock
RichText []RichText `json:"rich_text"`
Children []Block `json:"children,omitempty"`
Color Color `json:"color,omitempty"`
}
// MarshalJSON implements json.Marshaler.
func (b BulletedListItemBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias BulletedListItemBlock
dto struct {
BulletedListItem blockAlias `json:"bulleted_list_item"`
}
)
return json.Marshal(dto{
BulletedListItem: blockAlias(b),
})
}
type NumberedListItemBlock struct {
baseBlock
RichText []RichText `json:"rich_text"`
Children []Block `json:"children,omitempty"`
Color Color `json:"color,omitempty"`
}
// MarshalJSON implements json.Marshaler.
func (b NumberedListItemBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias NumberedListItemBlock
dto struct {
NumberedListItem blockAlias `json:"numbered_list_item"`
}
)
return json.Marshal(dto{
NumberedListItem: blockAlias(b),
})
}
type QuoteBlock struct {
baseBlock
RichText []RichText `json:"rich_text"`
Children []Block `json:"children,omitempty"`
Color Color `json:"color,omitempty"`
}
// MarshalJSON implements json.Marshaler.
func (b QuoteBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias QuoteBlock
dto struct {
Quote blockAlias `json:"quote"`
}
)
return json.Marshal(dto{
Quote: blockAlias(b),
})
}
type ToggleBlock struct {
baseBlock
RichText []RichText `json:"rich_text"`
Children []Block `json:"children,omitempty"`
Color Color `json:"color,omitempty"`
}
// MarshalJSON implements json.Marshaler.
func (b ToggleBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias ToggleBlock
dto struct {
Toggle blockAlias `json:"toggle"`
}
)
return json.Marshal(dto{
Toggle: blockAlias(b),
})
}
type TemplateBlock struct {
baseBlock
RichText []RichText `json:"rich_text"`
Children []Block `json:"children,omitempty"`
}
// MarshalJSON implements json.Marshaler.
func (b TemplateBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias TemplateBlock
dto struct {
Template blockAlias `json:"template"`
}
)
return json.Marshal(dto{
Template: blockAlias(b),
})
}
type Heading1Block struct {
baseBlock
RichText []RichText `json:"rich_text"`
Children []Block `json:"children,omitempty"`
Color Color `json:"color,omitempty"`
IsToggleable bool `json:"is_toggleable"`
}
// MarshalJSON implements json.Marshaler.
func (b Heading1Block) MarshalJSON() ([]byte, error) {
type (
blockAlias Heading1Block
dto struct {
Heading1 blockAlias `json:"heading_1"`
}
)
return json.Marshal(dto{
Heading1: blockAlias(b),
})
}
type Heading2Block struct {
baseBlock
RichText []RichText `json:"rich_text"`
Children []Block `json:"children,omitempty"`
Color Color `json:"color,omitempty"`
IsToggleable bool `json:"is_toggleable"`
}
// MarshalJSON implements json.Marshaler.
func (b Heading2Block) MarshalJSON() ([]byte, error) {
type (
blockAlias Heading2Block
dto struct {
Heading2 blockAlias `json:"heading_2"`
}
)
return json.Marshal(dto{
Heading2: blockAlias(b),
})
}
type Heading3Block struct {
baseBlock
RichText []RichText `json:"rich_text"`
Children []Block `json:"children,omitempty"`
Color Color `json:"color,omitempty"`
IsToggleable bool `json:"is_toggleable"`
}
// MarshalJSON implements json.Marshaler.
func (b Heading3Block) MarshalJSON() ([]byte, error) {
type (
blockAlias Heading3Block
dto struct {
Heading3 blockAlias `json:"heading_3"`
}
)
return json.Marshal(dto{
Heading3: blockAlias(b),
})
}
type ToDoBlock struct {
baseBlock
RichText []RichText `json:"rich_text"`
Children []Block `json:"children,omitempty"`
Checked *bool `json:"checked,omitempty"`
Color Color `json:"color,omitempty"`
}
// MarshalJSON implements json.Marshaler.
func (b ToDoBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias ToDoBlock
dto struct {
ToDo blockAlias `json:"to_do"`
}
)
return json.Marshal(dto{
ToDo: blockAlias(b),
})
}
type ChildPageBlock struct {
baseBlock
Title string `json:"title"`
}
// MarshalJSON implements json.Marshaler.
func (b ChildPageBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias ChildPageBlock
dto struct {
ChildPage blockAlias `json:"child_page"`
}
)
return json.Marshal(dto{
ChildPage: blockAlias(b),
})
}
type ChildDatabaseBlock struct {
baseBlock
Title string `json:"title"`
}
// MarshalJSON implements json.Marshaler.
func (b ChildDatabaseBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias ChildDatabaseBlock
dto struct {
ChildDatabase blockAlias `json:"child_database"`
}
)
return json.Marshal(dto{
ChildDatabase: blockAlias(b),
})
}
type CalloutBlock struct {
baseBlock
RichText []RichText `json:"rich_text"`
Children []Block `json:"children,omitempty"`
Icon *Icon `json:"icon,omitempty"`
Color Color `json:"color,omitempty"`
}
// MarshalJSON implements json.Marshaler.
func (b CalloutBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias CalloutBlock
dto struct {
Callout blockAlias `json:"callout"`
}
)
return json.Marshal(dto{
Callout: blockAlias(b),
})
}
type CodeBlock struct {
baseBlock
RichText []RichText `json:"rich_text"`
Children []Block `json:"children,omitempty"`
Caption []RichText `json:"caption,omitempty"`
Language *string `json:"language,omitempty"`
}
// MarshalJSON implements json.Marshaler.
func (b CodeBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias CodeBlock
dto struct {
Code blockAlias `json:"code"`
}
)
return json.Marshal(dto{
Code: blockAlias(b),
})
}
type EmbedBlock struct {
baseBlock
URL string `json:"url"`
}
// MarshalJSON implements json.Marshaler.
func (b EmbedBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias EmbedBlock
dto struct {
Embed blockAlias `json:"embed"`
}
)
return json.Marshal(dto{
Embed: blockAlias(b),
})
}
type ImageBlock struct {
baseBlock
Type FileType `json:"type"`
File *FileFile `json:"file,omitempty"`
External *FileExternal `json:"external,omitempty"`
Caption []RichText `json:"caption,omitempty"`
}
// MarshalJSON implements json.Marshaler.
func (b ImageBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias ImageBlock
dto struct {
Image blockAlias `json:"image"`
}
)
return json.Marshal(dto{
Image: blockAlias(b),
})
}
type AudioBlock struct {
baseBlock
Type FileType `json:"type"`
File *FileFile `json:"file,omitempty"`
External *FileExternal `json:"external,omitempty"`
Caption []RichText `json:"caption,omitempty"`
}
// MarshalJSON implements json.Marshaler.
func (b AudioBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias ImageBlock
dto struct {
Audio blockAlias `json:"audio"`
}
)
return json.Marshal(dto{
Audio: blockAlias(b),
})
}
type VideoBlock struct {
baseBlock
Type FileType `json:"type"`
File *FileFile `json:"file,omitempty"`
External *FileExternal `json:"external,omitempty"`
Caption []RichText `json:"caption,omitempty"`
}
// MarshalJSON implements json.Marshaler.
func (b VideoBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias VideoBlock
dto struct {
Video blockAlias `json:"video"`
}
)
return json.Marshal(dto{
Video: blockAlias(b),
})
}
type FileBlock struct {
baseBlock
Type FileType `json:"type"`
File *FileFile `json:"file,omitempty"`
External *FileExternal `json:"external,omitempty"`
Caption []RichText `json:"caption,omitempty"`
}
// MarshalJSON implements json.Marshaler.
func (b FileBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias FileBlock
dto struct {
File blockAlias `json:"file"`
}
)
return json.Marshal(dto{
File: blockAlias(b),
})
}
type PDFBlock struct {
baseBlock
Type FileType `json:"type"`
File *FileFile `json:"file,omitempty"`
External *FileExternal `json:"external,omitempty"`
Caption []RichText `json:"caption,omitempty"`
}
// MarshalJSON implements json.Marshaler.
func (b PDFBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias PDFBlock
dto struct {
PDF blockAlias `json:"pdf"`
}
)
return json.Marshal(dto{
PDF: blockAlias(b),
})
}
type BookmarkBlock struct {
baseBlock
URL string `json:"url"`
Caption []RichText `json:"caption,omitempty"`
}
// MarshalJSON implements json.Marshaler.
func (b BookmarkBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias BookmarkBlock
dto struct {
Bookmark blockAlias `json:"bookmark"`
}
)
return json.Marshal(dto{
Bookmark: blockAlias(b),
})
}
type EquationBlock struct {
baseBlock
Expression string `json:"expression"`
}
// MarshalJSON implements json.Marshaler.
func (b EquationBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias EquationBlock
dto struct {
Equation blockAlias `json:"equation"`
}
)
return json.Marshal(dto{
Equation: blockAlias(b),
})
}
type ColumnListBlock struct {
baseBlock
Children []ColumnBlock `json:"children,omitempty"`
}
// MarshalJSON implements json.Marshaler.
func (b ColumnListBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias ColumnListBlock
dto struct {
ColumnList blockAlias `json:"column_list"`
}
)
return json.Marshal(dto{
ColumnList: blockAlias(b),
})
}
type ColumnBlock struct {
baseBlock
Children []Block `json:"children,omitempty"`
}
// MarshalJSON implements json.Marshaler.
func (b ColumnBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias ColumnBlock
dto struct {
Column blockAlias `json:"column"`
}
)
return json.Marshal(dto{
Column: blockAlias(b),
})
}
type TableBlock struct {
baseBlock
TableWidth int `json:"table_width"`
HasColumnHeader bool `json:"has_column_header"`
HasRowHeader bool `json:"has_row_header"`
Children []Block `json:"children,omitempty"`
}
// MarshalJSON implements json.Marshaler.
func (b TableBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias TableBlock
dto struct {
Table blockAlias `json:"table"`
}
)
return json.Marshal(dto{
Table: blockAlias(b),
})
}
type TableRowBlock struct {
baseBlock
Cells [][]RichText `json:"cells"`
}
// MarshalJSON implements json.Marshaler.
func (b TableRowBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias TableRowBlock
dto struct {
TableRow blockAlias `json:"table_row"`
}
)
return json.Marshal(dto{
TableRow: blockAlias(b),
})
}
type LinkPreviewBlock struct {
baseBlock
URL string `json:"url"`
}
// MarshalJSON implements json.Marshaler.
func (b LinkPreviewBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias LinkPreviewBlock
dto struct {
LinkPreview blockAlias `json:"link_preview"`
}
)
return json.Marshal(dto{
LinkPreview: blockAlias(b),
})
}
type LinkToPageBlock struct {
baseBlock
Type LinkToPageType `json:"type"`
PageID string `json:"page_id,omitempty"`
DatabaseID string `json:"database_id,omitempty"`
}
// MarshalJSON implements json.Marshaler.
func (b LinkToPageBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias LinkToPageBlock
dto struct {
LinkToPage blockAlias `json:"link_to_page"`
}
)
return json.Marshal(dto{
LinkToPage: blockAlias(b),
})
}
type LinkToPageType string
const (
LinkToPageTypePageID LinkToPageType = "page_id"
LinkToPageTypeDatabaseID LinkToPageType = "database_id"
)
type SyncedBlock struct {
baseBlock
SyncedFrom *SyncedFrom `json:"synced_from"`
Children []Block `json:"children,omitempty"`
}
// MarshalJSON implements json.Marshaler.
func (b SyncedBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias SyncedBlock
dto struct {
SyncedBlock blockAlias `json:"synced_block"`
}
)
return json.Marshal(dto{
SyncedBlock: blockAlias(b),
})
}
type SyncedFrom struct {
Type SyncedFromType `json:"type"`
BlockID string `json:"block_id"`
}
type SyncedFromType string
const SyncedFromTypeBlockID SyncedFromType = "block_id"
type DividerBlock struct {
baseBlock
}
// MarshalJSON implements json.Marshaler.
func (b DividerBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias DividerBlock
dto struct {
Divider blockAlias `json:"divider"`
}
)
return json.Marshal(dto{
Divider: blockAlias(b),
})
}
type TableOfContentsBlock struct {
baseBlock
Color Color `json:"color,omitempty"`
}
// MarshalJSON implements json.Marshaler.
func (b TableOfContentsBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias TableOfContentsBlock
dto struct {
TableOfContents blockAlias `json:"table_of_contents"`
}
)
return json.Marshal(dto{
TableOfContents: blockAlias(b),
})
}
type BreadcrumbBlock struct {
baseBlock
}
// MarshalJSON implements json.Marshaler.
func (b BreadcrumbBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias BreadcrumbBlock
dto struct {
Breadcrumb blockAlias `json:"breadcrumb"`
}
)
return json.Marshal(dto{
Breadcrumb: blockAlias(b),
})
}
type UnsupportedBlock struct {
baseBlock
}
// MarshalJSON implements json.Marshaler.
func (b UnsupportedBlock) MarshalJSON() ([]byte, error) {
type (
blockAlias UnsupportedBlock
dto struct {
Unsupported blockAlias `json:"unsupported"`
}
)
return json.Marshal(dto{
Unsupported: blockAlias(b),
})
}
type BlockType string
const (
BlockTypeParagraph BlockType = "paragraph"
BlockTypeHeading1 BlockType = "heading_1"
BlockTypeHeading2 BlockType = "heading_2"
BlockTypeHeading3 BlockType = "heading_3"
BlockTypeBulletedListItem BlockType = "bulleted_list_item"
BlockTypeNumberedListItem BlockType = "numbered_list_item"
BlockTypeToDo BlockType = "to_do"
BlockTypeToggle BlockType = "toggle"
BlockTypeChildPage BlockType = "child_page"
BlockTypeChildDatabase BlockType = "child_database"
BlockTypeCallout BlockType = "callout"
BlockTypeQuote BlockType = "quote"
BlockTypeCode BlockType = "code"
BlockTypeEmbed BlockType = "embed"
BlockTypeImage BlockType = "image"
BlockTypeAudio BlockType = "audio"
BlockTypeVideo BlockType = "video"
BlockTypeFile BlockType = "file"
BlockTypePDF BlockType = "pdf"
BlockTypeBookmark BlockType = "bookmark"
BlockTypeEquation BlockType = "equation"
BlockTypeDivider BlockType = "divider"
BlockTypeTableOfContents BlockType = "table_of_contents"
BlockTypeBreadCrumb BlockType = "breadcrumb"
BlockTypeColumnList BlockType = "column_list"
BlockTypeColumn BlockType = "column"
BlockTypeTable BlockType = "table"
BlockTypeTableRow BlockType = "table_row"
BlockTypeLinkPreview BlockType = "link_preview"
BlockTypeLinkToPage BlockType = "link_to_page"
BlockTypeSyncedBlock BlockType = "synced_block"
BlockTypeTemplate BlockType = "template"
BlockTypeUnsupported BlockType = "unsupported"
)
type PaginationQuery struct {
StartCursor string
PageSize int
}
// BlockChildrenResponse contains results (block children) and pagination data returned from a find request.
type BlockChildrenResponse struct {
Results []Block
HasMore bool
NextCursor *string
}
func (resp *BlockChildrenResponse) UnmarshalJSON(b []byte) error {
type responseDTO struct {
Results []blockDTO `json:"results"`
HasMore bool `json:"has_more"`
NextCursor *string `json:"next_cursor"`
}
var dto responseDTO
if err := json.Unmarshal(b, &dto); err != nil {
return err
}
resp.HasMore = dto.HasMore
resp.NextCursor = dto.NextCursor
resp.Results = make([]Block, len(dto.Results))
for i, blockDTO := range dto.Results {
block, err := blockDTO.Block()
if err != nil {
// Any error (even `ErrUnknownBlockType`) is explicitly returned.
// We don't silently drop blocks with an unknown/unmapped type,
// because this could lead to surprises/unexpected list behaviour
// for users.
return fmt.Errorf("notion: failed to parse block (id: %q, type: %q): %w", blockDTO.ID, blockDTO.Type, err)
}
resp.Results[i] = block
}
return nil
}
func (dto blockDTO) Block() (Block, error) {
baseBlock := baseBlock{
id: dto.ID,
hasChildren: dto.HasChildren,
}
if dto.Parent != nil {
baseBlock.parent = *dto.Parent
}
if dto.CreatedTime != nil {
baseBlock.createdTime = *dto.CreatedTime
}
if dto.CreatedBy != nil {
baseBlock.createdBy = *dto.CreatedBy
}
if dto.LastEditedTime != nil {
baseBlock.lastEditedTime = *dto.LastEditedTime
}
if dto.LastEditedBy != nil {
baseBlock.lastEditedBy = *dto.LastEditedBy
}
if dto.Archived != nil {
baseBlock.archived = *dto.Archived
}
switch dto.Type {
case BlockTypeParagraph:
dto.Paragraph.baseBlock = baseBlock
return dto.Paragraph, nil
case BlockTypeHeading1:
dto.Heading1.baseBlock = baseBlock
return dto.Heading1, nil
case BlockTypeHeading2:
dto.Heading2.baseBlock = baseBlock
return dto.Heading2, nil
case BlockTypeHeading3:
dto.Heading3.baseBlock = baseBlock
return dto.Heading3, nil
case BlockTypeBulletedListItem:
dto.BulletedListItem.baseBlock = baseBlock
return dto.BulletedListItem, nil
case BlockTypeNumberedListItem:
dto.NumberedListItem.baseBlock = baseBlock
return dto.NumberedListItem, nil
case BlockTypeToDo:
dto.ToDo.baseBlock = baseBlock
return dto.ToDo, nil
case BlockTypeToggle:
dto.Toggle.baseBlock = baseBlock
return dto.Toggle, nil
case BlockTypeChildPage:
dto.ChildPage.baseBlock = baseBlock
return dto.ChildPage, nil
case BlockTypeChildDatabase:
dto.ChildDatabase.baseBlock = baseBlock
return dto.ChildDatabase, nil
case BlockTypeCallout:
dto.Callout.baseBlock = baseBlock
return dto.Callout, nil
case BlockTypeQuote:
dto.Quote.baseBlock = baseBlock
return dto.Quote, nil
case BlockTypeCode:
dto.Code.baseBlock = baseBlock
return dto.Code, nil
case BlockTypeEmbed:
dto.Embed.baseBlock = baseBlock
return dto.Embed, nil
case BlockTypeImage:
dto.Image.baseBlock = baseBlock
return dto.Image, nil
case BlockTypeAudio:
dto.Audio.baseBlock = baseBlock
return dto.Audio, nil
case BlockTypeVideo:
dto.Video.baseBlock = baseBlock