1 | |
2 |
|
3 | |
4 | * Append Markdown to Page |
5 | * Insert Notion-flavored Markdown into a page: at the end (default), at the start, or after existing text given as "start text...end text" in `after`. Needs the update content capability. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Notion, |
9 | page_id: string, |
10 | markdown: string, |
11 | at: "end" | "start" = "end", |
12 | after: string | undefined |
13 | ) { |
14 | const insert_content: { [key: string]: any } = { content: markdown } |
15 | if (after !== undefined && after !== "") { |
16 | insert_content.after = after |
17 | } else { |
18 | insert_content.position = { type: at } |
19 | } |
20 |
|
21 | const response = await fetch( |
22 | `https://api.notion.com/v1/pages/${page_id}/markdown`, |
23 | { |
24 | method: "PATCH", |
25 | headers: { |
26 | Authorization: `Bearer ${auth.token}`, |
27 | "Content-Type": "application/json", |
28 | Accept: "application/json", |
29 | "Notion-Version": "2026-03-11", |
30 | }, |
31 | body: JSON.stringify({ type: "insert_content", insert_content }), |
32 | } |
33 | ) |
34 |
|
35 | if (!response.ok) { |
36 | throw new Error(`${response.status} ${await response.text()}`) |
37 | } |
38 |
|
39 | return await response.json() |
40 | } |
41 |
|