Skip to content

Instantly share code, notes, and snippets.

@zeyadetman
Last active September 28, 2022 19:03
Show Gist options
  • Select an option

  • Save zeyadetman/a9bc5357225ccf6b75427d88e4f6a403 to your computer and use it in GitHub Desktop.

Select an option

Save zeyadetman/a9bc5357225ccf6b75427d88e4f6a403 to your computer and use it in GitHub Desktop.
Export nested notion databases to GitHub repo using typescript

Hey 👋 This is the function I've created for exporting specific notion database to GitHub Repo in my telegram bot (full code) The function supports nested databases.

Database example. image

ctx is used for telegram bot you can ignore it.

const handleDatabases = async (
  databaseId: string,
  pagesToPublish: any,
  parentSlug = ""
) => {
  return new Promise(async (resolve, reject) => {
    try {
      const { results } = await notion.databases.query({
        database_id: databaseId,
      });
      const pages = results.filter((res) => res.object === "page");
      pages.forEach(async (page: any) => {
        if (page?.properties?.Slug?.rich_text?.[0]?.plain_text) {
          pagesToPublish[page.properties.Slug.rich_text[0].plain_text] = {
            slug: page.properties.Slug.rich_text[0].plain_text,
            isPublish: page.properties?.Published?.checkbox || false,
            path: `${parentSlug || ""}${
              page.properties.Slug.rich_text[0].plain_text
            }`,
            page,
          };
        } else {
          // return ctx.reply("Please add a slug to the page");
          console.error("Please add a slug to the page");
        }
      });

      for await (let page of Object.values(pagesToPublish)) {
        const { slug, page: pageInfo } = page as any;
        const mdblocks = await n2m.pageToMarkdown(pageInfo.id);
        const finalMdBlocks = [];
        mdblocks?.forEach(async (block: any, index) => {
          if (index === mdblocks?.length - 1) {
            return;
          }
          if (block.type === "child_database") {
            const databaseId = block.parent.split("(").pop().split(")")[0];
            pagesToPublish[slug].children = {
              databaseId,
              slug,
              parentSlug: `${parentSlug || ""}${slug}`,
            };
          } else {
            finalMdBlocks.push(block);
          }
        });

        pagesToPublish[slug].mdBlocks = finalMdBlocks;
      }

      for await (let page of Object.keys(pagesToPublish)) {
        const { children, slug } = pagesToPublish[page] as any;
        if (children?.databaseId) {
          pagesToPublish[slug].childs = await handleDatabases(
            children.databaseId,
            {},
            `${children.parentSlug}`
          );
        }
      }

      console.log(util.inspect(pagesToPublish, false, null, true));
      return resolve(pagesToPublish);
    } catch (error) {
      return reject(error.message);
    }
  });
};


const pagesToPublish = await handleDatabases(
  process.env.NOTION_DATABASE_ID,
  {}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment