基于Github的博客和笔记系统
[TOC]
打算把正儿八经的笔记集体迁移到文件系统中,结合GitHub备份和在线浏览。同时也方便把好的笔记直接转换为博客。
为此,建立了一个私有仓库Notes,博客依旧放在GitHub Pages。
总体思路
使用 GitHub Workflow 自动从Notes构建Blog:
监听资源仓库的commit,发现特定文件夹(BlogSource)更新后推送到blog构建区域(blog仓库的分支:blog_factory)
构建blog,推送到GitHub Pages(即log仓库的分支:master)
实现
笔记方法简述
使用markdown作为笔记格式,图片、附件等资源放在同名的.res后缀文件夹下(部分老版本仍使用.src后缀)。配合 Typora 使用可以自动将拖动到文中的图片拷贝到同名.res后缀文件夹。
尽量不要在文件名中出现空格和一些英文标点
本地整理 Blog
blog存放的原则
blog源文件和各种资源文件放置在Notes/BlogSource,以下均以上述文件夹为当前目录。
由笔记直接整理出的,应当软链接回笔记。变动较大的,也应该在笔记版本上注明存在blog版本。
添加blog头
使用 ./header_blog.sh $filename 来快捷添加blog头,并启动vim来编辑。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 header_blog.sh filename="$*" if [ ! -n "$filename " ]then echo "File not found!" exit 1 fi echo "--- title: ${filename%.*} date: $(date '+%Y-%m-%d %H:%M:%S') categories: tags: --- <!-- more --> $(cat "$filename " ) " > "$filename " vim "$filename "
移动文件到相应文件夹
将md文件移动到_posts文件夹中,图片等资源文件移动到image文件夹中,并更新md文件中的图片等链接。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 file="$*" if [ -f "$file " ] && [ -n "$file " ]then if [ -d "${file%.*} .res" ] then sed -e "s/\!\[\](/\!\[\](\/image\//g" "$file " >"$file .temp" mv "$file " "${file%.*} .source.md" mv "$file .temp" "$file " mv "${file%.*} .res" image/ fi mv "$file " _posts/ else echo "File not found!" fi
关于图片,参考了https://yanyinhong.github.io/2017/05/02/How-to-insert-image-in-hexo-post/
Blog 仓库推送到 blog_factory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 name: Send Blog on: push: paths: - BlogSource/** jobs: push-blog: runs-on: ubuntu-latest steps: - name: Checkout Notes repo uses: actions/checkout@v2 - name: Update Blog Factory env: UPDATE_SECRET: ${{ secrets.BLOG_UPDATE_KEY }} GIT_USER: GIT_EMAIL: run: | sudo timedatectl set-timezone "Asia/Shanghai" mkdir -p ~/.ssh echo "$UPDATE_SECRET" > ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa ssh-keyscan github.com >> ~/.ssh/known_hosts git clone "GitHub Pages 仓库地址" --single-branch --branch blog_factory blog_factory --depth=1 rsync -av --delete BlogSource/_posts blog_factory/source rsync -av --delete BlogSource/image blog_factory/source cd blog_factory git add --all if [ -n "`git status -s`" ] then git status git config --global user.name $GIT_USER git config --global user.email $GIT_EMAIL git commit -m "Automatically pushed from Notes: $(date '+%Y-%m-%d %H:%M:%S')" git push else echo "Nothing to Commit" fi
blog_factory 自动构建并推送到GitHub Pages
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 name: Hexo Deploy on: push: branches: - blog_factory paths: - source/** env: GIT_USER: GIT_EMAIL: jobs: build-blog: runs-on: ubuntu-latest steps: - name: Checkout source repo uses: actions/checkout@v2 with: ref: blog_factory - name: Set up Node.js uses: actions/setup-node@v1 with: node-version: '14' - name: Install Dependencies run: | wget https://github.com/jgm/pandoc/releases/download/2.10.1/pandoc-2.10.1-1-amd64.deb sudo dpkg -i pandoc-2.10.1-1-amd64.deb npm uninstall hexo-renderer-marked npm install hexo-renderer-pandoc npm install - name: Setup SSH Keys and known_hosts env: DEPLOY_SECRET: ${{ secrets.BLOG_DEPLOY_SECRET }} run: | sudo timedatectl set-timezone "Asia/Shanghai" mkdir -p ~/.ssh echo "$DEPLOY_SECRET" > ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa ssh-keyscan github.com >> ~/.ssh/known_hosts git config --global user.name $GIT_USER git config --global user.email $GIT_EMAIL - name: Deploy run: | npx hexo clean && npx hexo g -d
Tips:
不要在source/_posts中放其他类型的文件,可能导致无法hexo g。(笔者经历:两个sh文件导致无法读取整个文件夹)
参考:
https://note.junyangz.com/2019/GitHub-Actions-depoly-Hexo/
https://umm.js.org/p/3d7401da/