Edits history of script submission #23125 for ' Append Markdown to Page (notion)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Append Markdown to Page
     * 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.
     */
    export async function main(
      auth: RT.Notion,
      page_id: string,
      markdown: string,
      at: "end" | "start" = "end",
      after: string | undefined
    ) {
      const insert_content: { [key: string]: any } = { content: markdown }
      if (after !== undefined && after !== "") {
        insert_content.after = after
      } else {
        insert_content.position = { type: at }
      }
    
      const response = await fetch(
        `https://api.notion.com/v1/pages/${page_id}/markdown`,
        {
          method: "PATCH",
          headers: {
            Authorization: `Bearer ${auth.token}`,
            "Content-Type": "application/json",
            Accept: "application/json",
            "Notion-Version": "2026-03-11",
          },
          body: JSON.stringify({ type: "insert_content", insert_content }),
        }
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 4 hours ago