Skip to main content

Utilities for importing Markdown files to Notion.so

Project description

twitter twitter

Notion.so Markdown Importer

An importer for Markdown files to Notion.so using notion-py

It provides these features over Notion.so's Markdown importer:

  • Picking a Notion.so page to upload to (instead of them all uploading to the root)
  • Code fences keep their original language (or as close as we can match it)
  • Code fences are formatted properly
  • Inline HTML is preserved
  • Markdown frontmatter is preserved
  • Local image references will be uploaded from relative URLs
  • Image alts are loaded as captions instead of as TextBlocks
  • Handles nested lists properly
  • Among other improvements...

Usage with Python 3.6+

  • pip install md2notion

  • From the command link you can run python -m md2notion.upload [token_v2] [page-url] [...markdown_path_glob_or_url]

  • OR In your Python file:

from notion.client import NotionClient
from md2notion.upload import upload

# Follow the instructions at https://github.com/jamalex/notion-py#quickstart to setup Notion.py
client = NotionClient(token_v2="<token_v2>")
page = client.get_block("https://www.notion.so/myorg/Test-c0d20a71c0944985ae96e661ccc99821")

with open("TestMarkdown.md", "r", encoding="utf-8") as mdFile:
    newPage = page.children.add_new(PageBlock, title="TestMarkdown Upload")
    upload(mdFile, newPage) #Appends the converted contents of TestMarkdown.md to newPage

If you need to process notion-py block descriptors after parsing from Markdown but before uploading, consider using convert and uploadBlock separately. Take a look at upload.py#upload() for more.

from md2notion.upload import convert, uploadBlock

rendered = convert(mdFile)

# Process the rendered array of `notion-py` block descriptors here
# (just dicts with some properties to pass to `notion-py`)

# Upload all the blocks
for blockDescriptor in rendered:
    uploadBlock(blockDescriptor, page, mdFile.name)

If you need to parse Markdown differently from the default, consider subclassing NotionPyRenderer (a BaseRenderer for mistletoe). You can then pass it to upload(..., notionPyRendererCls=NotionPyRenderer) as a parameter.

Example, Custom Hexo Importer

Here's an example that imports a Hexo blog (slghtly hacky).

import io
import os.path
import glob
from pathlib import Path
from notion.block import PageBlock
from notion.client import NotionClient
from md2notion.upload import upload

client = NotionClient(token_v2="<token_v2>")
page = client.get_block("https://www.notion.so/myorg/Test-c0d20a71c0944985ae96e661ccc99821")

for fp in glob.glob("../source/_posts/*.md", recursive=True):
    with open(fp, "r", encoding="utf-8") as mdFile:
        #Preprocess the Markdown frontmatter into yaml code fences
        mdStr = mdFile.read()
        mdChunks = mdStr.split("---")
        mdStr = \
f"""```yaml
{mdChunks[1]}
`` `

{'---'.join(mdChunks[2:])}
"""
        mdFile = io.StringIO(mdStr)
        mdFile.__dict__["name"] = fp #Set this so we can resolve images later

        pageName = os.path.basename(fp)[:40]
        newPage = page.children.add_new(PageBlock, title=pageName)
        print(f"Uploading {fp} to Notion.so at page {pageName}")
        #Get the image relative to the markdown file in the flavor that Hexo
        #stores its images (in a folder with the same name as the md file)
        def convertImagePath(imagePath, mdFilePath):
            return Path(mdFilePath).parent / Path(mdFilePath).stem / Path(imagePath)
        upload(mdFile, newPage, imagePathFunc=convertImagePath)

Contributing

See CONTRIBUTING.md

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

md2notion-1.0.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

md2notion-1.0.0-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file md2notion-1.0.0.tar.gz.

File metadata

  • Download URL: md2notion-1.0.0.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.6.8

File hashes

Hashes for md2notion-1.0.0.tar.gz
Algorithm Hash digest
SHA256 20e4a7ce593f5d94610ee93c8a3cc204103b7c6c1cce9c4c24703aeb8a1050ae
MD5 f5f0a3f1c1ad2d274b7ef138efa559ad
BLAKE2b-256 54d5d15863b2770619e65b9cbaeb18d3d300cfe4fd196f5b9dece6fc102fe144

See more details on using hashes here.

File details

Details for the file md2notion-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: md2notion-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.6.8

File hashes

Hashes for md2notion-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4c53331a2c1489fdb75b816e41fc2d8b543331c6e4e084d00b5b64f7fe5ab5b5
MD5 64ab66982ed8842452e0442755fb541f
BLAKE2b-256 b6123a8874ce774442731425364a42843adb8e2000684aa2c2399912a5e263e6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page