Touch 4 Tech

One brick at a time contributing to the DKIW pyramid fundation

notion2md

categories: misc

Authors: Oberron , Rezpe , Rezpe & Oberron , sebas ,

Notion to .md

  1. a simple gist to convert notion pages to markdown

https://gist.github.com/oberron/28939ddb1fd4ea4721475cf2edf474e8

  1. a simple github action clones the gist and uses it to create markdown files

source: frenchtouch/.github/workflows/notion2pelican.yml

name: Notion2Pelican
# Controls when the workflow will run
on:
  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  sync_notion:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest
    # Steps represent a sequence of tasks that will be executed as part of the job
    environment: env  # THIS WAS MISSING
    steps:
      - name: Checkout website source
        id: checkout-master
        uses: actions/checkout@v3
      - name : setup notion key
        env: # Or as an environment variable
          NOTIONKEY: ${{ secrets.NOTIONKEY }}
        run: python importnotion.py

a wrapper file importnotion.py

frenchtouch/importnotion.py

import subprocess
from os import environ

gist = "28939ddb1fd4ea4721475cf2edf474e8"
gist_url = f"https://gist.github.com/{gist}.git"
output = subprocess.call(["git", "clone", gist_url])
if not output==0:
  print(f"gist error code: {output}")
output = subprocess.call(["mv",f"{gist}/notion2md.py","./notion2md.py"])
if not output==0:
  print(f"mv error code: {output}")

from notion2md import readDatabase, process_page_results
from notion2md import get_notion_headers, pageid_2_md

# notion db id for frenchtouch.dev is:
FT_dbid = 'af9dd2942ef74d7c9faf32e694055a7e'
# get the GitHub secret which has the Notion Key
my_secret = environ.get("NOTIONKEY")
headers = get_notion_headers(my_secret)
print("headers",headers)
res = readDatabase(databaseId=FT_dbid, notion_headers=headers)
res2 = process_page_results(res, headers)

res_t = readDatabase(databaseId=res2["page_children_ids"][1], notion_headers=headers)
md = pageid_2_md(res_t, headers)
print(md)