0

Append Markdown to Page

by
Published today

Insert Notion-flavored Markdown at the end or start of a page, or after a given piece of existing text.

Script notion Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 3 hours ago
1
//native
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