語雀自動同步到hexo博客


hexo+github pages+yuque-hexo插件+github actions+serverless雲函數+語雀node

實現語雀寫完文章可以自動同步到 hexo 博客git


本文針對已經搭建好 hexo 博客的,若是沒有搭好正常的 hexo 博客的能夠去網上找一下,很方便github


hexo同步語雀內容


用到了這個項目:web

https://github.com/x-cold/yuque-hexonpm

安裝:npm i -g yuque-hexojson


而後把 package.json 的內容添加上下面這些ubuntu

  "yuqueConfig": {
    "postPath""source/_posts",
    "cachePath""yuque.json",
    "mdNameFormat""slug",
    "adapter""hexo",
    "concurrency"5,
    "baseUrl""https://www.yuque.com/api/v2",
    "login""hxfqg9",
    "repo""web",
    "token""語雀token",
    "onlyPublished"true,
    "onlyPublic"true
  },
  "devDependencies": {
    "yuque-hexo""^1.6.0"
  },
  "hexo": {
    "version""4.2.1"
  },

這裏說一下里面的 baseurl 是固定的api

login 和 repo 是以下圖這樣對應的,我的界面和團隊界面均可以微信



token 是在右上角頭像 -> 帳戶設置 -> Token 添加的,權限的話只給讀取就能夠hexo

ps.公開的知識庫也要設置 Token



在 "scripts" 中添加


    "sync""yuque-hexo sync",
    "clean:yuque""yuque-hexo clean",

這樣總體下來個人 package.json 內容以下

{
  "name""hexo-site",
  "version""0.0.0",
  "private"true,
  "scripts": {
    "build""hexo generate",
    "clean""hexo clean",
    "deploy""hexo deploy",
    "server""hexo server",
    "sync""yuque-hexo sync",
    "clean:yuque""yuque-hexo clean"
  },
  "yuqueConfig": {
    "postPath""source/_posts",
    "cachePath""yuque.json",
    "mdNameFormat""slug",
    "adapter""hexo",
    "concurrency"5,
    "baseUrl""https://www.yuque.com/api/v2",
    "login""hxfqg9",
    "repo""web",
    "token""語雀token",
    "onlyPublished"true,
    "onlyPublic"true
  },
  "devDependencies": {
    "yuque-hexo""^1.6.0"
  },
  "hexo": {
    "version""4.2.1"
  },
  "dependencies": {
    "hexo""^4.2.1",
    "hexo-deployer-git""^2.1.0",
    "hexo-generator-archive""^1.0.0",
    "hexo-generator-baidu-sitemap""^0.1.6",
    "hexo-generator-category""^1.0.0",
    "hexo-generator-feed""^2.2.0",
    "hexo-generator-index""^1.0.0",
    "hexo-generator-json-content""^4.2.3",
    "hexo-generator-searchdb""^1.3.1",
    "hexo-generator-sitemap""^2.0.0",
    "hexo-generator-tag""^1.0.0",
    "hexo-renderer-ejs""^1.0.0",
    "hexo-renderer-marked""^2.0.0",
    "hexo-renderer-stylus""^1.1.0",
    "hexo-server""^1.0.0",
    "hexo-wordcount""^6.0.1"
  }
}

這時候用 yuque-hexo sync 就會把語雀的文章給下載下來,下載到 \source\_posts


而後 hexo g && hexo s 就能夠訪問 127.0.0.1:4000 本地看一下了

手動發佈是 hexo g && hexo d


針對語雀圖片沒法正常顯示的解決辦法

在主題的 layout 文件夾中的 post.ejs 文件中加上一句


<meta name="referrer" content="no-referrer" />


github actions自動更新


在 github 上建立一個私有倉庫(由於會涉及到一些 token 啥的)倉庫名字無所謂

注意:在倉庫裏面再放一個倉庫是無法把裏面那個倉庫 push 到 github 的,只會傳一個空文件夾,致使後期博客成了空白頁面,最簡單粗暴的辦法就是把你 git clone 的 hexo 主題裏的 .git 文件夾給刪掉


而後在 hexo 的目錄下運行以下命令,把 hexo 的源碼傳到 github 遠程倉庫中


git init
git add .
git commit -m "first commit"
git remote add origin https://github.com/yichen115/blog.git
git push -u origin master

去 github 的 settings 建立一個 token



只勾上這一個便可




生成了 token 以後必定要記下來,再回來就無法看了


而後來到剛纔建立的私有倉庫的 settings



添加兩個 secret

GH_REF 是你博客的倉庫地址 github.com/yichen115/yichen115.github.io

注意去掉前面 https://


GE_TOKEN 是剛纔生成的 token


而後來到 actions,點擊 set up a workflow yourself



編輯內容以下:


name: Blog CI/CD

on: [push, repository_dispatch]

jobs:
  blog-cicd:
    name: Hexo blog build & deploy
    runs-on: ubuntu-latest
    env:
      TZ: Asia/Shanghai
    steps:
    - name: Checkout codes
      uses: actions/checkout@v2

    - name: Setup node
      uses: actions/setup-node@v1
      with:
        node-version: '12.x'
    - name: Cache node modules
      uses: actions/cache@v1
      with:
        path: ~/.npm
        key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}

    - name: Install dependencies
      run: |
        npm install hexo-cli -g
        npm install yuque-hexo -g
        npm install
        yuque-hexo sync
    - name: Generate files
      run: hexo generate
      
    - name: Deploy blog
      run: |
        git clone "https://${{ secrets.GH_REF }}" deploy_git
        mv ./deploy_git/.git ./public/
        cd ./public
        git config user.name "yichen"
        git config user.email "1097179511@qq.com"
        git add .
        git commit -m "GitHub Actions Auto Builder at $(date +'%Y-%m-%d %H:%M:%S')"
        git push --force --quiet "https://${{ secrets.GH_TOKEN }}@${{ secrets.GH_REF }}" master:master


下面那個 user.name 和 user.email 根據本身的狀況改一下,注意對齊


弄完以後每當 push 或 repository_dispatch 的時候都會自動的進行更新


配置serverless雲函數


來這裏註冊個帳號

https://console.cloud.tencent.com/scf/

新建一個函數服務




內容寫

# -*- coding: utf8 -*-
import requests
def main_handler(event, context):
    r = requests.post("https://api.github.com/repos/yichen115/blog/dispatches",
    json = {"event_type""run-it"},
    headers = {"User-Agent":'curl/7.52.1',
              'Content-Type''application/json',
              'Accept''application/vnd.github.everest-preview+json',
              'Authorization''token 你的GH_TOKEN'})
    if r.status_code == 204:
        return "This's OK!" 
    else:
        return r.status_code

post 請求裏只須要改用戶名和倉庫名(yichen115/blog)後面是固定的

那個 token 是帶着的,完整的就是 'Authorization': 'token xxxxxxxxxxxxxx'


點下面那個測試,返回 This's OK!



同時 github actions 也會收到指令,去執行以前在 main.yml 設定好的



過一陣就成下面那個綠色的對號了,而後去訪問一下博客,看看是否正常。能夠的話就證實雲函數能夠了


建立一個觸發器




他會給你一個訪問路徑,記下來



配置語雀webhook


在知識庫中選擇設置



觸發規則本身定就好啦



這篇文章更新的時候發現有失敗的可能



個人博客地址:

https://yichen115.github.io

本文分享自微信公衆號 - 陳冠男的遊戲人生(CGN-115)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。

相關文章
相關標籤/搜索