diff --git a/block.go b/block.go index 50e65d0..acffe7f 100644 --- a/block.go +++ b/block.go @@ -257,6 +257,15 @@ type BasicBlock struct { Parent *Parent `json:"parent,omitempty"` } +// NewBasicBlock returns a new BasicBlock with the ObjectTypeBlock and given block type. +// It's used as a basic block for all other blocks. +func NewBasicBlock(blockType BlockType) BasicBlock { + return BasicBlock{ + Object: ObjectTypeBlock, + Type: blockType, + } +} + func (b BasicBlock) GetType() BlockType { return b.Type } @@ -401,6 +410,13 @@ type Paragraph struct { Color string `json:"color,omitempty"` } +func NewParagraphBlock(paragraph Paragraph) *ParagraphBlock { + return &ParagraphBlock{ + BasicBlock: NewBasicBlock(BlockTypeParagraph), + Paragraph: paragraph, + } +} + type Heading1Block struct { BasicBlock Heading1 Heading `json:"heading_1"` @@ -413,16 +429,54 @@ type Heading struct { IsToggleable bool `json:"is_toggleable,omitempty"` } +func NewHeading1Block(heading1 Heading) *Heading1Block { + return &Heading1Block{ + BasicBlock: NewBasicBlock(BlockTypeHeading1), + Heading1: heading1, + } +} + type Heading2Block struct { BasicBlock Heading2 Heading `json:"heading_2"` } +func NewHeading2Block(heading2 Heading) *Heading2Block { + return &Heading2Block{ + BasicBlock: NewBasicBlock(BlockTypeHeading2), + Heading2: heading2, + } +} + type Heading3Block struct { BasicBlock Heading3 Heading `json:"heading_3"` } +func NewHeading3Block(heading3 Heading) *Heading3Block { + return &Heading3Block{ + BasicBlock: NewBasicBlock(BlockTypeHeading3), + Heading3: heading3, + } +} + +// NewHeadingBlock returns a new Heading[1-3]Block (hidden below Block interface) +// corresponding to the given heading level. +// It defaults to Heading 3 if the given level is not supported. +func NewHeadingBlock(heading Heading, level int) Block { + switch level { + case 1: + return NewHeading1Block(heading) + case 2: + return NewHeading2Block(heading) + case 3: + return NewHeading3Block(heading) + default: + // fallback to level 3 + return NewHeading3Block(heading) + } +} + type CalloutBlock struct { BasicBlock Callout Callout `json:"callout"` @@ -435,6 +489,13 @@ type Callout struct { Color string `json:"color,omitempty"` } +func NewCalloutBlock(callout Callout) *CalloutBlock { + return &CalloutBlock{ + BasicBlock: NewBasicBlock(BlockTypeCallout), + Callout: callout, + } +} + type QuoteBlock struct { BasicBlock Quote Quote `json:"quote"` @@ -446,6 +507,13 @@ type Quote struct { Color string `json:"color,omitempty"` } +func NewQuoteBlock(quote Quote) *QuoteBlock { + return &QuoteBlock{ + BasicBlock: NewBasicBlock(BlockTypeQuote), + Quote: quote, + } +} + type TableBlock struct { BasicBlock Table Table `json:"table"` @@ -467,6 +535,20 @@ type TableRow struct { Cells [][]RichText `json:"cells"` } +func NewTableRowBlock(tr TableRow) *TableRowBlock { + return &TableRowBlock{ + BasicBlock: NewBasicBlock(BlockTypeTableRow), + TableRow: tr, + } +} + +func NewTableBlock(table Table) *TableBlock { + return &TableBlock{ + BasicBlock: NewBasicBlock(BlockTypeTable), + Table: table, + } +} + type BulletedListItemBlock struct { BasicBlock BulletedListItem ListItem `json:"bulleted_list_item"` @@ -478,11 +560,25 @@ type ListItem struct { Color string `json:"color,omitempty"` } +func NewBulletedListItemBlock(li ListItem) *BulletedListItemBlock { + return &BulletedListItemBlock{ + BasicBlock: NewBasicBlock(BlockTypeBulletedListItem), + BulletedListItem: li, + } +} + type NumberedListItemBlock struct { BasicBlock NumberedListItem ListItem `json:"numbered_list_item"` } +func NewNumberedListItemBlock(li ListItem) *NumberedListItemBlock { + return &NumberedListItemBlock{ + BasicBlock: NewBasicBlock(BlockTypeNumberedListItem), + NumberedListItem: li, + } +} + type ToDoBlock struct { BasicBlock ToDo ToDo `json:"to_do"` @@ -495,6 +591,13 @@ type ToDo struct { Color string `json:"color,omitempty"` } +func NewToDoBlock(t ToDo) *ToDoBlock { + return &ToDoBlock{ + BasicBlock: NewBasicBlock(BlockTypeToDo), + ToDo: t, + } +} + type ToggleBlock struct { BasicBlock Toggle Toggle `json:"toggle"` @@ -506,6 +609,13 @@ type Toggle struct { Color string `json:"color,omitempty"` } +func NewToggleBlock(toggle Toggle) *ToggleBlock { + return &ToggleBlock{ + BasicBlock: NewBasicBlock(BlockTypeToggle), + Toggle: toggle, + } +} + type ChildPageBlock struct { BasicBlock ChildPage struct { @@ -513,6 +623,14 @@ type ChildPageBlock struct { } `json:"child_page"` } +func NewChildPageBlock(title string) *ChildPageBlock { + cpd := &ChildPageBlock{ + BasicBlock: NewBasicBlock(BlockTypeChildPage), + } + cpd.ChildPage.Title = title + return cpd +} + type EmbedBlock struct { BasicBlock Embed Embed `json:"embed"` @@ -523,6 +641,13 @@ type Embed struct { URL string `json:"url"` } +func NewEmbedBlock(embed Embed) *EmbedBlock { + return &EmbedBlock{ + BasicBlock: NewBasicBlock(BlockTypeEmbed), + Embed: embed, + } +} + type ImageBlock struct { BasicBlock Image Image `json:"image"` @@ -546,6 +671,13 @@ func (i Image) GetURL() string { return "" } +func NewImageBlock(image Image) *ImageBlock { + return &ImageBlock{ + BasicBlock: NewBasicBlock(BlockTypeImage), + Image: image, + } +} + type AudioBlock struct { BasicBlock Audio Audio `json:"audio"` @@ -569,6 +701,13 @@ func (i Audio) GetURL() string { return "" } +func NewAudioBlock(audio Audio) *AudioBlock { + return &AudioBlock{ + BasicBlock: NewBasicBlock(BlockTypeAudio), + Audio: audio, + } +} + type CodeBlock struct { BasicBlock Code Code `json:"code"` @@ -580,6 +719,13 @@ type Code struct { Language string `json:"language"` } +func NewCodeBlock(code Code) *CodeBlock { + return &CodeBlock{ + BasicBlock: NewBasicBlock(BlockTypeCode), + Code: code, + } +} + type VideoBlock struct { BasicBlock Video Video `json:"video"` @@ -592,6 +738,13 @@ type Video struct { External *FileObject `json:"external,omitempty"` } +func NewVideoBlock(video Video) *VideoBlock { + return &VideoBlock{ + BasicBlock: NewBasicBlock(BlockTypeVideo), + Video: video, + } +} + type FileBlock struct { BasicBlock File BlockFile `json:"file"` @@ -604,6 +757,13 @@ type BlockFile struct { External *FileObject `json:"external,omitempty"` } +func NewFileBlock(file BlockFile) *FileBlock { + return &FileBlock{ + BasicBlock: NewBasicBlock(BlockTypeFile), + File: file, + } +} + type PdfBlock struct { BasicBlock Pdf Pdf `json:"pdf"` @@ -616,6 +776,13 @@ type Pdf struct { External *FileObject `json:"external,omitempty"` } +func NewPdfBlock(pdf Pdf) *PdfBlock { + return &PdfBlock{ + BasicBlock: NewBasicBlock(BlockTypePdf), + Pdf: pdf, + } +} + type BookmarkBlock struct { BasicBlock Bookmark Bookmark `json:"bookmark"` @@ -626,6 +793,13 @@ type Bookmark struct { URL string `json:"url"` } +func NewBookmarkBlock(bookmark Bookmark) *BookmarkBlock { + return &BookmarkBlock{ + BasicBlock: NewBasicBlock(BlockTypeBookmark), + Bookmark: bookmark, + } +} + type ChildDatabaseBlock struct { BasicBlock ChildDatabase struct { @@ -633,6 +807,14 @@ type ChildDatabaseBlock struct { } `json:"child_database"` } +func NewChildDatabaseBlock(title string) *ChildDatabaseBlock { + cdb := &ChildDatabaseBlock{ + BasicBlock: NewBasicBlock(BlockTypeChildDatabase), + } + cdb.ChildDatabase.Title = title + return cdb +} + type TableOfContentsBlock struct { BasicBlock TableOfContents TableOfContents `json:"table_of_contents"` @@ -643,6 +825,13 @@ type TableOfContents struct { Color string `json:"color,omitempty"` } +func NewTableOfContentsBlock(toc TableOfContents) *TableOfContentsBlock { + return &TableOfContentsBlock{ + BasicBlock: NewBasicBlock(BlockTypeTableOfContents), + TableOfContents: toc, + } +} + type DividerBlock struct { BasicBlock Divider Divider `json:"divider"` @@ -652,6 +841,13 @@ type Divider struct { // empty } +func NewDividerBlock() *DividerBlock { + return &DividerBlock{ + BasicBlock: NewBasicBlock(BlockTypeDivider), + Divider: Divider{}, + } +} + type EquationBlock struct { BasicBlock Equation Equation `json:"equation"` @@ -661,6 +857,13 @@ type Equation struct { Expression string `json:"expression"` } +func NewEquationBlock(eq Equation) *EquationBlock { + return &EquationBlock{ + BasicBlock: NewBasicBlock(BlockTypeEquation), + Equation: eq, + } +} + type BreadcrumbBlock struct { BasicBlock Breadcrumb Breadcrumb `json:"breadcrumb"` @@ -670,6 +873,13 @@ type Breadcrumb struct { // empty } +func NewBreadcrumbBlock() *BreadcrumbBlock { + return &BreadcrumbBlock{ + BasicBlock: NewBasicBlock(BlockTypeBreadcrumb), + Breadcrumb: Breadcrumb{}, + } +} + type ColumnBlock struct { BasicBlock Column Column `json:"column"` @@ -680,6 +890,13 @@ type Column struct { Children Blocks `json:"children"` } +func NewColumnBlock(col Column) *ColumnBlock { + return &ColumnBlock{ + BasicBlock: NewBasicBlock(BlockTypeColumn), + Column: col, + } +} + type ColumnListBlock struct { BasicBlock ColumnList ColumnList `json:"column_list"` @@ -691,6 +908,13 @@ type ColumnList struct { Children Blocks `json:"children"` } +func NewColumnListBlock(cl ColumnList) *ColumnListBlock { + return &ColumnListBlock{ + BasicBlock: NewBasicBlock(BlockTypeColumnList), + ColumnList: cl, + } +} + // NOTE: will only be returned by the API. Cannot be created by the API. // https://developers.notion.com/reference/block#link-preview-blocks type LinkPreviewBlock struct { @@ -702,6 +926,13 @@ type LinkPreview struct { URL string `json:"url"` } +func NewLinkPreviewBlock(lp LinkPreview) *LinkPreviewBlock { + return &LinkPreviewBlock{ + BasicBlock: NewBasicBlock(BlockTypeLinkPreview), + LinkPreview: lp, + } +} + type LinkToPageBlock struct { BasicBlock LinkToPage LinkToPage `json:"link_to_page"` @@ -713,6 +944,13 @@ type LinkToPage struct { DatabaseID DatabaseID `json:"database_id,omitempty"` } +func NewLinkToPageBlock(ltp LinkToPage) *LinkToPageBlock { + return &LinkToPageBlock{ + BasicBlock: NewBasicBlock(BlockTypeLinkToPage), + LinkToPage: ltp, + } +} + type TemplateBlock struct { BasicBlock Template Template `json:"template"` @@ -723,6 +961,13 @@ type Template struct { Children Blocks `json:"children,omitempty"` } +func NewTemplateBlock(template Template) *TemplateBlock { + return &TemplateBlock{ + BasicBlock: NewBasicBlock(BlockTypeTemplate), + Template: template, + } +} + type SyncedBlock struct { BasicBlock SyncedBlock Synced `json:"synced_block"` @@ -738,6 +983,13 @@ type SyncedFrom struct { BlockID BlockID `json:"block_id"` } +func NewSyncedBlock(synced Synced) *SyncedBlock { + return &SyncedBlock{ + BasicBlock: NewBasicBlock(BlockTypeSyncedBlock), + SyncedBlock: synced, + } +} + type UnsupportedBlock struct { BasicBlock } @@ -804,6 +1056,8 @@ func decodeBlock(raw map[string]interface{}) (Block, error) { b = &EmbedBlock{} case BlockTypeImage: b = &ImageBlock{} + case BlockTypeAudio: + b = &AudioBlock{} case BlockTypeVideo: b = &VideoBlock{} case BlockTypeFile: @@ -834,11 +1088,10 @@ func decodeBlock(raw map[string]interface{}) (Block, error) { b = &TemplateBlock{} case BlockTypeSyncedBlock: b = &SyncedBlock{} - case BlockTypeTableBlock: + case BlockTypeTable: b = &TableBlock{} - case BlockTypeTableRowBlock: + case BlockTypeTableRow: b = &TableRowBlock{} - case BlockTypeUnsupported: b = &UnsupportedBlock{} default: diff --git a/const.go b/const.go index 3bd7b79..024876f 100644 --- a/const.go +++ b/const.go @@ -220,6 +220,7 @@ const ( BlockTypeEmbed BlockType = "embed" BlockTypeImage BlockType = "image" + BlockTypeAudio BlockType = "audio" BlockTypeVideo BlockType = "video" BlockTypeFile BlockType = "file" BlockTypePdf BlockType = "pdf" @@ -237,8 +238,8 @@ const ( BlockTypeLinkToPage BlockType = "link_to_page" BlockTypeTemplate BlockType = "template" BlockTypeSyncedBlock BlockType = "synced_block" - BlockTypeTableBlock BlockType = "table" - BlockTypeTableRowBlock BlockType = "table_row" + BlockTypeTable BlockType = "table" + BlockTypeTableRow BlockType = "table_row" BlockTypeUnsupported BlockType = "unsupported" )