Skip to main content

将 Markdown 文件导入 Notion.so 的实用程序

项目描述

构建状态 pypi python版本 推特 推特

Notion.so Markdown 导入器

Markdown 文件到Notion.so的导入器,使用notion-py

它通过 Notion.so 的 Markdown 导入器提供以下功能:

  • 选择要上传到的 Notion.so 页面(而不是全部上传到根目录)
  • 代码围栏保持其原始语言(或尽可能接近)
  • 代码围栏格式正确
  • 内联 HTML 被保留
  • (可选)上传 HTML<img>标记中提及的图像。
  • Markdown frontmatter 被保留
  • 本地图片参考将从相对 URL 上传
  • 图像 alt 加载为标题而不是TextBlocks
  • 正确处理嵌套列表
  • 在其他改进中...

支持 Python 3.6+

从 CLI 使用

  • pip install md2notion
  • 然后像这样运行python -m md2notion [token_v2] [page-url] [...markdown_path_glob_or_url]
  • 给定路径的降价将作为新子项添加到 Notion.so 注释中page-url

还有一些配置选项:

  • --clear-previous:如果笔记的子项与page-url您上传的内容同名,它将首先被删除。
  • --append:而不是创建一个新的孩子,它会将降价内容附加到注释中page-url
  • --html-img: 上传 HTML<img>标签中提到的图片。

脚本中的用法

  • pip install md2notion
  • 在您的 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

如果您需要notion-py在从 Markdown 解析之后但在上传之前处理块描述符,请考虑单独使用convertuploadBlock。看看upload.py#upload()更多。

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)

如果您需要以不同于默认的方式解析 Markdown,请考虑子类化NotionPyRenderer(a BaseRendererformistletoe)。然后,您可以将其upload(..., notionPyRendererCls=NotionPyRenderer)作为参数传递给。

示例,自定义 Hexo 导入器

这是一个导入 Hexo 博客的示例(有点 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.md

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

md2notion-2.4.1.tar.gz (13.3 kB 查看哈希

已上传 source

内置分布

md2notion-2.4.1-py3-none-any.whl (12.8 kB 查看哈希

已上传 py3