yichen的信安知识库/web方向/语雀自动同步到hexo博客/

语雀自动同步到hexo博客

hexo+github pages+yuque-hexo插件+github actions+serverless云函数+语雀 实现语雀写完文章能够自动同步到 hexo 博客

本文针对已经搭建好 hexo 博客的,如果没有搭好正常的 hexo 博客的可以去网上找一下,很方便

hexo同步语雀内容

用到了这个项目:https://github.com/x-cold/yuque-hexo
安装:npm i -g yuque-hexo

然后把 package.json 的内容添加上下面这些

  1. "yuqueConfig": {
  2. "postPath": "source/_posts",
  3. "cachePath": "yuque.json",
  4. "mdNameFormat": "slug",
  5. "adapter": "hexo",
  6. "concurrency": 5,
  7. "baseUrl": "https://www.yuque.com/api/v2",
  8. "login": "hxfqg9",
  9. "repo": "web",
  10. "token": "语雀token",
  11. "onlyPublished": true,
  12. "onlyPublic": true
  13. },
  14. "devDependencies": {
  15. "yuque-hexo": "^1.6.0"
  16. },
  17. "hexo": {
  18. "version": "4.2.1"
  19. },

这里说一下里面的 baseurl 是固定的
login 和 repo 是如下图这样对应的,个人界面和团队界面都可以

image.png

token 是在右上角头像 -> 账户设置 -> Token 添加的,权限的话只给读取就可以
ps.公开的知识库也要设置 Token

image.png

在 “scripts” 中添加

  1. "sync": "yuque-hexo sync",
  2. "clean:yuque": "yuque-hexo clean",

这样整体下来我的 package.json 内容如下

  1. {
  2. "name": "hexo-site",
  3. "version": "0.0.0",
  4. "private": true,
  5. "scripts": {
  6. "build": "hexo generate",
  7. "clean": "hexo clean",
  8. "deploy": "hexo deploy",
  9. "server": "hexo server",
  10. "sync": "yuque-hexo sync",
  11. "clean:yuque": "yuque-hexo clean"
  12. },
  13. "yuqueConfig": {
  14. "postPath": "source/_posts",
  15. "cachePath": "yuque.json",
  16. "mdNameFormat": "slug",
  17. "adapter": "hexo",
  18. "concurrency": 5,
  19. "baseUrl": "https://www.yuque.com/api/v2",
  20. "login": "hxfqg9",
  21. "repo": "web",
  22. "token": "语雀token",
  23. "onlyPublished": true,
  24. "onlyPublic": true
  25. },
  26. "devDependencies": {
  27. "yuque-hexo": "^1.6.0"
  28. },
  29. "hexo": {
  30. "version": "4.2.1"
  31. },
  32. "dependencies": {
  33. "hexo": "^4.2.1",
  34. "hexo-deployer-git": "^2.1.0",
  35. "hexo-generator-archive": "^1.0.0",
  36. "hexo-generator-baidu-sitemap": "^0.1.6",
  37. "hexo-generator-category": "^1.0.0",
  38. "hexo-generator-feed": "^2.2.0",
  39. "hexo-generator-index": "^1.0.0",
  40. "hexo-generator-json-content": "^4.2.3",
  41. "hexo-generator-searchdb": "^1.3.1",
  42. "hexo-generator-sitemap": "^2.0.0",
  43. "hexo-generator-tag": "^1.0.0",
  44. "hexo-renderer-ejs": "^1.0.0",
  45. "hexo-renderer-marked": "^2.0.0",
  46. "hexo-renderer-stylus": "^1.1.0",
  47. "hexo-server": "^1.0.0",
  48. "hexo-wordcount": "^6.0.1"
  49. }
  50. }

这时候用 yuque-hexo sync 就会把语雀的文章给下载下来,下载到 \source_posts

然后 hexo g && hexo s 就可以访问 127.0.0.1:4000 本地看一下了
手动发布是 hexo g && hexo d

针对语雀图片无法正常显示的解决办法
在主题的 layout 文件夹中的 post.ejs 文件中加上一句

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

image.png

github actions自动更新

在 github 上创建一个私有仓库(因为会涉及到一些 token 啥的)仓库名字无所谓
注意:在仓库里面再放一个仓库是没法把里面那个仓库 push 到 github 的,只会传一个空文件夹,导致后期博客成了空白页面,最简单粗暴的办法就是把你 git clone 的 hexo 主题里的 .git 文件夹给删掉

然后在 hexo 的目录下运行如下命令

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

去 github 的 settings 创建一个 token

image.png

只勾上这一个即可

image.png

image.png

生成了 token 之后一定要记下来,再回来就没法看了

然后来到刚才创建的私有仓库的 settings

image.png

添加两个 secret
GH_REF 是你博客的仓库地址 github.com/yichen115/yichen115.github.io
注意去掉前面 https://

GE_TOKEN 是刚才生成的 token

然后来到 actions,点击 set up a workflow yourself

image.png

编辑内容如下:

  1. name: Blog CI/CD
  2. on: [push, repository_dispatch]
  3. jobs:
  4. blog-cicd:
  5. name: Hexo blog build & deploy
  6. runs-on: ubuntu-latest
  7. env:
  8. TZ: Asia/Shanghai
  9. steps:
  10. - name: Checkout codes
  11. uses: actions/checkout@v2
  12. - name: Setup node
  13. uses: actions/setup-node@v1
  14. with:
  15. node-version: '12.x'
  16. - name: Cache node modules
  17. uses: actions/cache@v1
  18. with:
  19. path: ~/.npm
  20. key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
  21. - name: Install dependencies
  22. run: |
  23. npm install hexo-cli -g
  24. npm install yuque-hexo -g
  25. npm install
  26. yuque-hexo sync
  27. - name: Generate files
  28. run: hexo generate
  29. - name: Deploy blog
  30. run: |
  31. git clone "https://${{ secrets.GH_REF }}" deploy_git
  32. mv ./deploy_git/.git ./public/
  33. cd ./public
  34. git config user.name "yichen"
  35. git config user.email "1097179511@qq.com"
  36. git add .
  37. git commit -m "GitHub Actions Auto Builder at $(date +'%Y-%m-%d %H:%M:%S')"
  38. 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/ 注册个账号
新建一个函数服务

image.png

image.png

内容写

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

post 请求里只需要改用户名和仓库名(yichen115/blog)后面是固定的
那个 token 是带着的,完整的就是 ‘Authorization’: ‘token xxxxxxxxxxxxxx’

点下面那个测试,返回 This’s OK!

image.png

同时 github actions 也会收到指令,去执行之前在 main.yml 设定好的

image.png

过一阵就成下面那个绿色的对号了,然后去访问一下博客,看看是否正常。可以的话就证明云函数可以了

创建一个触发器

image.png

image.png

他会给你一个访问路径,记下来

image.png

配置语雀webhook

在知识库中选择设置

image.png

触发规则自己定就好啦

image.png


这篇文章更新的时候发现有失败的可能

image.png

我的博客地址:[https://yichen115.github.io**](https://yichen115.github.io/)