A renderer and uploader for Markdown files to notion
Project description
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
TextBlock
s - Among other improvements...
Limitations
- Currently does not support tables/
CollectionViewBlocks
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_globs]
-
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 dislike the way this package implements a specific Markdown to Notion.so conversion, you can subclass NotionPyRenderer
(a BaseRenderer
for mistletoe
) and pass it to upload(..., notionPyRendererCls=NotionPyRenderer)
.
If you need to post process your converted Markdown before uploading the blocks to Notion.so, you can convert your file before uploading. Take a look at upload.py 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)
Example, Hexo Importer
Here's an example that imports a Hexo blog
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:
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file md2notion-0.1.1.tar.gz
.
File metadata
- Download URL: md2notion-0.1.1.tar.gz
- Upload date:
- Size: 7.3 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff6257b1b6e6d76a3a6a725684a80137237b847c50df2acedd42267d23b3708e |
|
MD5 | 2757c5b5d6fee1c5bb10f100f5c0a99d |
|
BLAKE2b-256 | 2e967d038a6fcc44d7b5a3d0f6813740187d93636f27d0da53bdc95ad87da752 |
File details
Details for the file md2notion-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: md2notion-0.1.1-py3-none-any.whl
- Upload date:
- Size: 9.6 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b9df9ec955c45e2cbbd1c5e050e1987355e7267bed45819a00ba368045e9c4b |
|
MD5 | 6047f2bd03fa028f417295e9077d92b8 |
|
BLAKE2b-256 | 1c70f01b0708794c7bd990e48b42b7da26bbc103633dfec953c2c2a4f6487d18 |