instructions

My previous blog uses Hexo + Github, because I prefer the flexibility of Notion, so I reversed and ran into migration issues.

Notion itself supports Markdown imports.

  • The head of theyamlWhich is a minor problem, given the dates andtagsIf you don’t write a script, you have to redo it by hand
  • codeFormat problem is more serious, resulting in all becomeplaintextType, and the newline is gone, it’s impossible to read

Google search solutions, find a Github repository (see link at the end of this article), optimize rendering of Notion Markdown, but write python script imports, and provide examples of Hexo import scripts (awesome).

Here is the specific import operation:

Install python support libraries

pip install md2notion
Copy the code

Python scripts

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 in your cookies")
page = client.get_block(
    "https://www.notion.so/your-page-path")

for fp in glob.glob("your-hexo-path/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'
            f'{mdChunks[1]}'
            f'```'
            f''
            f"{The '-'.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)
Copy the code

Nobelium blog

Run the above script and wait for the import to complete.

If you want to continue building a Hexo-like blog based on Notion, you can use Nobelium, an open source Notion blogging system. See the link below for more on how to build.

reference

GitHub – Cobertos/md2notion: A better Notion.so Markdown importer

Use Nobelium + Vercel to turn your Notion into a blogosphere – minority