<p align="center">
<a href="https://github.com/Cobertos/md2notion/actions" target="_blank"><img alt="build status" src="https://github.com/Cobertos/md2notion/workflows/Package%20Tests/badge.svg"></a>
<a href="https://pypi.org/project/md2notion/" target="_blank"><img alt="pypi python versions" src="https://img.shields.io/pypi/pyversions/md2notion.svg"></a>
<a href="https://twitter.com/cobertos" target="_blank"><img alt="twitter" src="https://img.shields.io/badge/twitter-%40cobertos-0084b4.svg"></a>
<a href="https://cobertos.com" target="_blank"><img alt="twitter" src="https://img.shields.io/badge/website-cobertos.com-888888.svg"></a>
</p>
# Modification
fork from [md2notion](https://github.com/Cobertos/md2notion), add following features
- add `block_sleep_time` to indicate sleep time(s) after each block is uploaed
- add `file_sleep_time` to indicate sleep time(s) after each file is uploaded
# Notion.so Markdown Importer
An importer for Markdown files to [Notion.so](https://notion.so) using [`notion-py`](https://github.com/jamalex/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
* (Optionally) Upload images that are memtioned in the HTML `<img>` tags.
* Markdown frontmatter is preserved
* Local image references will be uploaded from relative URLs
* Image alts are loaded as captions instead of as `TextBlock`s
* Handles nested lists properly
* Among other improvements...
Supports Python 3.6+
## Usage from CLI
* `pip install md2notion`
* Then run like `python -m md2notion [token_v2] [page-url] [...markdown_path_glob_or_url]`
* The markdown at the given path will be added as a new child to the Notion.so note at `page-url`
There are also some configuration options:
* `--clear-previous`: If a child of the note at `page-url` has the same name as what you're uploading, it will first be removed.
* `--append`: Instead of making a new child, it will append the markdown contents to the note at `page-url`
* `--html-img`: Upload images that are memtioned in the HTML `<img>` tags.
## Usage from script
* `pip install md2notion`
* In your Python file:
```python
from notion.client import NotionClient
from notion.block import PageBlock
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()`](https://github.com/Cobertos/md2notion/blob/master/md2notion/upload.py) for more.
```python
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`](https://github.com/Cobertos/md2notion/blob/master/md2notion/NotionPyRenderer.py) (a [`BaseRenderer` for `mistletoe`](https://github.com/miyuchina/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).
```python
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](https://github.com/Cobertos/md2notion/blob/master/CONTRIBUTING.md)
Raw data
{
"_id": null,
"home_page": "https://github.com/Cobertos/md2notion/",
"name": "md2notion-aaron-fork",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "notion notion.so notion-py markdown md converter",
"author": "Cobertos",
"author_email": "me+python@cobertos.com",
"download_url": "https://files.pythonhosted.org/packages/3f/85/1cafd299884e6317412aa93695e2cbdb43022ef5bc3e64b45c425f050053/md2notion-aaron-fork-2.4.1.tar.gz",
"platform": null,
"description": "<p align=\"center\">\n <a href=\"https://github.com/Cobertos/md2notion/actions\" target=\"_blank\"><img alt=\"build status\" src=\"https://github.com/Cobertos/md2notion/workflows/Package%20Tests/badge.svg\"></a>\n <a href=\"https://pypi.org/project/md2notion/\" target=\"_blank\"><img alt=\"pypi python versions\" src=\"https://img.shields.io/pypi/pyversions/md2notion.svg\"></a>\n <a href=\"https://twitter.com/cobertos\" target=\"_blank\"><img alt=\"twitter\" src=\"https://img.shields.io/badge/twitter-%40cobertos-0084b4.svg\"></a>\n <a href=\"https://cobertos.com\" target=\"_blank\"><img alt=\"twitter\" src=\"https://img.shields.io/badge/website-cobertos.com-888888.svg\"></a>\n</p>\n\n# Modification\nfork from [md2notion](https://github.com/Cobertos/md2notion), add following features\n- add `block_sleep_time` to indicate sleep time(s) after each block is uploaed\n- add `file_sleep_time` to indicate sleep time(s) after each file is uploaded\n\n# Notion.so Markdown Importer\n\nAn importer for Markdown files to [Notion.so](https://notion.so) using [`notion-py`](https://github.com/jamalex/notion-py)\n\nIt provides these features over Notion.so's Markdown importer:\n\n* Picking a Notion.so page to upload to (instead of them all uploading to the root)\n* Code fences keep their original language (or as close as we can match it)\n* Code fences are formatted properly\n* Inline HTML is preserved\n* (Optionally) Upload images that are memtioned in the HTML `<img>` tags.\n* Markdown frontmatter is preserved\n* Local image references will be uploaded from relative URLs\n* Image alts are loaded as captions instead of as `TextBlock`s\n* Handles nested lists properly\n* Among other improvements...\n\nSupports Python 3.6+\n\n## Usage from CLI\n\n* `pip install md2notion`\n* Then run like `python -m md2notion [token_v2] [page-url] [...markdown_path_glob_or_url]`\n* The markdown at the given path will be added as a new child to the Notion.so note at `page-url`\n\nThere are also some configuration options:\n\n* `--clear-previous`: If a child of the note at `page-url` has the same name as what you're uploading, it will first be removed.\n* `--append`: Instead of making a new child, it will append the markdown contents to the note at `page-url`\n* `--html-img`: Upload images that are memtioned in the HTML `<img>` tags.\n\n## Usage from script\n\n* `pip install md2notion`\n* In your Python file:\n```python\nfrom notion.client import NotionClient\nfrom notion.block import PageBlock\nfrom md2notion.upload import upload\n\n# Follow the instructions at https://github.com/jamalex/notion-py#quickstart to setup Notion.py\nclient = NotionClient(token_v2=\"<token_v2>\")\npage = client.get_block(\"https://www.notion.so/myorg/Test-c0d20a71c0944985ae96e661ccc99821\")\n\nwith open(\"TestMarkdown.md\", \"r\", encoding=\"utf-8\") as mdFile:\n newPage = page.children.add_new(PageBlock, title=\"TestMarkdown Upload\")\n upload(mdFile, newPage) #Appends the converted contents of TestMarkdown.md to newPage\n```\n\nIf 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()`](https://github.com/Cobertos/md2notion/blob/master/md2notion/upload.py) for more.\n\n```python\nfrom md2notion.upload import convert, uploadBlock\n\nrendered = convert(mdFile)\n\n# Process the rendered array of `notion-py` block descriptors here\n# (just dicts with some properties to pass to `notion-py`)\n\n# Upload all the blocks\nfor blockDescriptor in rendered:\n uploadBlock(blockDescriptor, page, mdFile.name)\n```\n\nIf you need to parse Markdown differently from the default, consider subclassing [`NotionPyRenderer`](https://github.com/Cobertos/md2notion/blob/master/md2notion/NotionPyRenderer.py) (a [`BaseRenderer` for `mistletoe`](https://github.com/miyuchina/mistletoe)). You can then pass it to `upload(..., notionPyRendererCls=NotionPyRenderer)` as a parameter.\n\n## Example, Custom Hexo Importer\n\nHere's an example that imports a Hexo blog (slghtly hacky).\n\n```python\nimport io\nimport os.path\nimport glob\nfrom pathlib import Path\nfrom notion.block import PageBlock\nfrom notion.client import NotionClient\nfrom md2notion.upload import upload\n\nclient = NotionClient(token_v2=\"<token_v2>\")\npage = client.get_block(\"https://www.notion.so/myorg/Test-c0d20a71c0944985ae96e661ccc99821\")\n\nfor fp in glob.glob(\"../source/_posts/*.md\", recursive=True):\n with open(fp, \"r\", encoding=\"utf-8\") as mdFile:\n #Preprocess the Markdown frontmatter into yaml code fences\n mdStr = mdFile.read()\n mdChunks = mdStr.split(\"---\")\n mdStr = \\\nf\"\"\"```yaml\n{mdChunks[1]}\n`` `\n\n{'---'.join(mdChunks[2:])}\n\"\"\"\n mdFile = io.StringIO(mdStr)\n mdFile.__dict__[\"name\"] = fp #Set this so we can resolve images later\n\n pageName = os.path.basename(fp)[:40]\n newPage = page.children.add_new(PageBlock, title=pageName)\n print(f\"Uploading {fp} to Notion.so at page {pageName}\")\n #Get the image relative to the markdown file in the flavor that Hexo\n #stores its images (in a folder with the same name as the md file)\n def convertImagePath(imagePath, mdFilePath):\n return Path(mdFilePath).parent / Path(mdFilePath).stem / Path(imagePath)\n upload(mdFile, newPage, imagePathFunc=convertImagePath)\n```\n\n## Contributing\nSee [CONTRIBUTING.md](https://github.com/Cobertos/md2notion/blob/master/CONTRIBUTING.md)\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Utilities for importing Markdown files to Notion.so",
"version": "2.4.1",
"split_keywords": [
"notion",
"notion.so",
"notion-py",
"markdown",
"md",
"converter"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "3f851cafd299884e6317412aa93695e2cbdb43022ef5bc3e64b45c425f050053",
"md5": "e9fef7af9a22df9b560bf9cb42c95160",
"sha256": "2c2638c298d5e811440ea5309abc34b02dfc733720bae7183de80f7986960e0b"
},
"downloads": -1,
"filename": "md2notion-aaron-fork-2.4.1.tar.gz",
"has_sig": false,
"md5_digest": "e9fef7af9a22df9b560bf9cb42c95160",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 381474,
"upload_time": "2023-01-29T05:59:59",
"upload_time_iso_8601": "2023-01-29T05:59:59.299051Z",
"url": "https://files.pythonhosted.org/packages/3f/85/1cafd299884e6317412aa93695e2cbdb43022ef5bc3e64b45c425f050053/md2notion-aaron-fork-2.4.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-01-29 05:59:59",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "Cobertos",
"github_project": "md2notion",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"lcname": "md2notion-aaron-fork"
}