Package: md2notion
Notion.so Markdown ImporterAn importer for Markdown files toNotion.sousingnotion-pyIt 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 properlyInline HTML is preserved(Optionally) Upload images that are memtioned in the HTML<img>tags.Markdown frontmatter is preservedLocal image references will be uploaded from relative URLsImage alts are loaded as captions instead of asTextBlocksHandles nested lists properlyAmong other improvements...Supports Python 3.6+Usage from CLIpip install md2notionThen run likepython -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 atpage-urlThere are also some configuration options:--clear-previous: If a child of the note atpage-urlhas 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 atpage-url--html-img: Upload images that are memtioned in the HTML<img>tags.Usage from scriptpip install md2notionIn your Python file:fromnotion.clientimportNotionClientfromnotion.blockimportPageBlockfrommd2notion.uploadimportupload# Follow the instructions at https://github.com/jamalex/notion-py#quickstart to setup Notion.pyclient=NotionClient(token_v2="<token_v2>")page=client.get_block("https://www.notion.so/myorg/Test-c0d20a71c0944985ae96e661ccc99821")withopen("TestMarkdown.md","r",encoding="utf-8")asmdFile:newPage=page.children.add_new(PageBlock,title="TestMarkdown Upload")upload(mdFile,newPage)#Appends the converted contents of TestMarkdown.md to newPageIf you need to processnotion-pyblock descriptors after parsing from Markdown but before uploading, consider usingconvertanduploadBlockseparately. Take a look atupload.py#upload()for more.frommd2notion.uploadimportconvert,uploadBlockrendered=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 blocksforblockDescriptorinrendered:uploadBlock(blockDescriptor,page,mdFile.name)If you need to parse Markdown differently from the default, consider subclassingNotionPyRenderer(aBaseRendererformistletoe). You can then pass it toupload(..., notionPyRendererCls=NotionPyRenderer)as a parameter.Example, Custom Hexo ImporterHere's an example that imports a Hexo blog (slghtly hacky).importioimportos.pathimportglobfrompathlibimportPathfromnotion.blockimportPageBlockfromnotion.clientimportNotionClientfrommd2notion.uploadimportuploadclient=NotionClient(token_v2="<token_v2>")page=client.get_block("https://www.notion.so/myorg/Test-c0d20a71c0944985ae96e661ccc99821")forfpinglob.glob("../source/_posts/*.md",recursive=True):withopen(fp,"r",encoding="utf-8")asmdFile:#Preprocess the Markdown frontmatter into yaml code fencesmdStr=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 laterpageName=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)defconvertImagePath(imagePath,mdFilePath):returnPath(mdFilePath).parent/Path(mdFilePath).stem/Path(imagePath)upload(mdFile,newPage,imagePathFunc=convertImagePath)ContributingSeeCONTRIBUTING.md