王云子

王云子

此路山高路远....
twitter
telegram
github

Hexo and Notion's divine synergy

Goal Achieved#

  • Convert Notion articles to Markdown format
  • Write blogs on Notion
  • Publish Notion articles to Hexo

Project Origin#

  • First Generation: The project was initially created and used by Mo Huishou, but it only supported Aliyun picture bed and did not support article modifications.
  • Current: The project has been transformed and now supports multiple picture beds and article modifications, thanks to Doradx.

Project Tutorials#

My Personal Workflow#

The above tutorials are already quite comprehensive, but I like to write about my own mistakes and experiences during this process. You can choose to browse them selectively.

  1. Open the link below to create a Notion integration and obtain the Secrets.

bookmark

  1. Copy the template blog-template.
  2. Authorize the database. Open the copied template, click the three dots in the upper right corner, and click "Add Connection" below to add the previously created integration.
  3. Add environment variables in the blog repository's settings/secrets/actions, namely NOTION_DATABASE_ID, NOTION_TOKEN, and PICBED_CONFIG. The following is the original text from the expert:

image

  1. In the blog's directory .github/workflows, create two files: deploy.yml and notion_sync.yml. Here are my code snippets for these two files:
name: Automatic deployment

on:
  push:
    branches: master
    paths:
      - "source/**"
      - "**.yml"
  workflow_dispatch:
  workflow_call:

env:
  GIT_USER: wangyunzi
  GIT_EMAIL: xueq695@gmail.com

jobs:
  build:
    name: Ubuntu and node ${{ matrix.node_version }} and ${{ matrix.os }}
    runs-on: ubuntu-latest
    strategy:
      matrix:
        os: [ubuntu-latest]
        node_version: [16.19.1]

    steps:
      - name: Checkout blog and theme
        uses: actions/checkout@v3
        with:
          submodules: "recursive"
      - name: Use Node.js ${{ matrix.node_version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node_version }}
      - name: Install dependencies
        run: |
          git pull
      - name: Depoly
        run: |
          git ls-files -z | while read -d '' path; do touch -d "$(git log -1 --format="@%ct" "$path")" "$path"; done
          npm install
          npx hexo clean
          npx hexo douban
          npx hexo g
      - name: Commit & Push
        uses: stefanzweifel/git-auto-commit-action@v4
        with:
          commit_message: Automatic deployment.
name: Automatic sync pages from notion

on:
  #   push:
  #     branches: master
  workflow_dispatch:
  schedule:
    - cron: "5 */1 * * *"

jobs:
  notionSyncTask:
    name: Ubuntu and node ${{ matrix.node_version }} and ${{ matrix.os }}
    runs-on: ubuntu-latest
    strategy:
      matrix:
        os: [ubuntu-latest]
        node_version: [16.x]
    outputs:
      HAS_CHANGES: ${{ steps.checkStep.outputs.HAS_CHANGES }}

    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Use Node.js ${{ matrix.node_version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node_version }}
      - name: Convert notion to markdown
        uses: Doradx/notion2markdown-action@latest
        with:
          notion_secret: ${{ secrets.NOTION_TOKEN }} # NOTION authorization code
          database_id: ${{ secrets.NOTION_DATABASE_ID }} # Dataset ID
          migrate_image: true
          picBedConfig: ${{ secrets.PICBED_CONFIG}} # picBed configuration in JSON format, it is recommended to use minify JSON, otherwise errors may occur. For configuration of different picture beds, please refer to: https://picgo.github.io/PicGo-Core-Doc/zh/guide/config.html#picbed
          page_output_dir: "source" # Output directory for page-type articles, such as "about".
          post_output_dir: "source/_posts/notion" # Output directory for post-type articles. Be careful not to set it as 'source/_posts' when clean_unpublished_post is true, as it may delete the articles in your original directory!!!
          clean_unpublished_post: true # Whether to clean unpublished articles. For example, if you published an article before and then moved it to the draft box in Notion.
      - name: Check if there is anything changed
        id: checkStep
        run: |
          if [[ -n "$(git status --porcelain)" ]]; then
            echo "HAS_CHANGES=true" >> $GITHUB_OUTPUT;
            echo "Updates available & redeployment required."
          else
            echo "HAS_CHANGES=false" >> $GITHUB_OUTPUT;
            echo "Nothing to commit & deploy."
          fi
      - name: Commit & Push
        uses: stefanzweifel/git-auto-commit-action@v4
        with:
          commit_message: Automatic sync from notion.
  callDepolyTask:
    name: Call the depoly workflow
    needs: notionSyncTask
    if: ${{ needs.notionSyncTask.outputs.HAS_CHANGES=='true' }}
    uses: wangyunzi/Annie/.github/workflows/hexo_deploy.yml@master # Modify according to your own Github address
  1. Check the node version in the above two code snippets and change it to match your own version. (PS: If you don't change it, you may encounter some small problems, like me)
  2. Place the articles that need to be published in the "To Be Published" section. If you need to see the effect immediately, manually run the action. By default, the code checks for articles to be published every hour.

The successful completion of this project is entirely thanks to the help of Doradx. Writing this article is not only to thank him for transforming this convenient project, but also to express my gratitude for his hard work and guidance in helping me complete these configurations.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.