-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotion.go
More file actions
53 lines (46 loc) · 1.38 KB
/
Copy pathnotion.go
File metadata and controls
53 lines (46 loc) · 1.38 KB
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
package main
import (
"context"
"fmt"
"os"
"github.com/jomei/notionapi"
)
func notion() {
// Create Notion client
client := notionapi.NewClient(notionapi.Token(os.Getenv("NOTION_TOKEN")))
// Fetch a page (meta info, but not content)
page, err := client.Page.Get(context.Background(), notionapi.PageID("101d941266e7808ea147c6f2b07d4b07"))
if err != nil {
fmt.Println("Error occurred while fetching page:", err)
return
}
// Fetch page content (blocks)
blocks, err := client.Block.GetChildren(context.Background(), notionapi.BlockID(page.ID), nil)
if err != nil {
fmt.Println("Error occurred while fetching blocks:", err)
return
}
// Iterate over blocks and print their content
for _, block := range blocks.Results {
switch b := block.(type) {
case *notionapi.ParagraphBlock:
fmt.Println("Paragraph:", getText(b.Paragraph.RichText))
case *notionapi.Heading1Block:
fmt.Println("Heading 1:", getText(b.Heading1.RichText))
case *notionapi.Heading2Block:
fmt.Println("Heading 2:", getText(b.Heading2.RichText))
case *notionapi.Heading3Block:
fmt.Println("Heading 3:", getText(b.Heading3.RichText))
default:
fmt.Printf("Other block type: %T\n", b)
}
}
}
// Helper function to extract text from RichText array
func getText(richTexts []notionapi.RichText) string {
var result string
for _, rt := range richTexts {
result += rt.Text.Content
}
return result
}