在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):Cobertos/md2notion开源软件地址(OpenSource Url):https://github.com/Cobertos/md2notion开源编程语言(OpenSource Language):Python 100.0%开源软件介绍(OpenSource Introduction):Notion.so Markdown ImporterAn importer for Markdown files to Notion.so using It provides these features over Notion.so's Markdown importer:
Supports Python 3.6+ Usage from CLI
There are also some configuration options:
Usage from script
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 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 Example, Custom Hexo ImporterHere'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) ContributingSee CONTRIBUTING.md |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论