View English Version

给这玩意加点花里胡哨的东西。

Hexo 真是太丢人了。

i18n(双语)

我没有采用 hexo 带有的 i18n 功能,感觉那不够。

将 hexo site 的文件夹克隆一份出来。我把嘤文的叫做 en 中文的叫做 zh, 放在同一层目录下。

在复制的文件夹里重新安装环境,比如 npm install.

构建时,把嘤文站的静态文件复制到 zh/public/en 中。而在 Cloudflare Pages 的设置里面把 root directory 设为 zh/public.

随后需要修改嘤文站的 root directory(根目录):

# en/_config.yml
- url: https://blog.h3a.moe/
+ url: https://blog.h3a.moe/en
- root: /
+ root: /en

RSS

安装 hexo-generator-feed.

npm install hexo-generator-feed --save

_config.yml 中添加

# _config.yml
feed:
  type: atom
  path: atom.xml
  limit: false

生成时,便会在 public 中生成一个全文 rss 文件,atom.xml.

Log

在站点目录下用一个脚本,把 git log 写进一个自定义页面,充当编辑历史。

# export-history.sh
#!/bin/bash

mkdir -p source/history
touch source/history/index.md 

cat << EOT > source/history/index.md 
---
title: History
---
EOT

echo '```' >> source/history/index.md 

git log --show-signature >> source/history/index.md 

echo '```' >> source/history/index.md 

导航栏

将 Archive(归档)、Category(分类)、tag(标签)和 history(编辑历史)都收到一个二级菜单里。yaml 的语法好折腾。

同时增加了多语言页面的切换和 RSS 入口。

# zh/_config.fluid.yml
  menu:
    - { key: "home", link: "/", icon: "iconfont icon-home-fill" }
    - {
      key: "Index",
      icon: "iconfont icon-books",
      submenu: [ 
           { key: "archive", link: "/archives/", icon: "iconfont icon-archive-fill" },
           { key: "category", link: "/categories/", icon: "iconfont icon-category-fill" },
           { key: "tag", link: "/tags/", icon: "iconfont icon-tags-fill" },
           { key: 'history', link: '/history/', icon: 'iconfont icon-plan' }
      ]
    }
    - { key: "about", link: "/about/", icon: "iconfont icon-user-fill" }
    - { key: "links", link: "/links/", icon: "iconfont icon-link-fill" }
    - { key: "English", link: "/en", icon: "iconfont icon-bug" }
    - { key: "RSS", link: "/atom.xml", icon: "iconfont icon-rss" } # key: "RSS"
# en/_config.fluid.yml
  menu:
    - { key: "home", link: "/", icon: "iconfont icon-home-fill" }
    - {
      key: 'Index',
      icon: 'iconfont icon-books',
      submenu: [ 
           { key: 'archive', link: '/archives/', icon: 'iconfont icon-archive-fill' },
           { key: 'category', link: '/categories/', icon: 'iconfont icon-category-fill' },
           { key: 'tag', link: '/tags/', icon: 'iconfont icon-tags-fill' },
           { key: 'history', link: '/history/', icon: 'iconfont icon-plan' }
      ]
    }
    #- { key: "archive", link: "/archives/", icon: "iconfont icon-archive-fill" }
    #- { key: "category", link: "/categories/", icon: "iconfont icon-category-fill" }
    #- { key: "tag", link: "/tags/", icon: "iconfont icon-tags-fill" }
    - { key: "about", link: "/about/", icon: "iconfont icon-user-fill" }
    - { key: "links", link: "/links/", icon: "iconfont icon-link-fill" }
    - { key: "中文", link: "..", icon: "iconfont icon-bug" }
    - { key: "RSS", link: "/atom.xml", icon: "iconfont icon-rss" } # key: "RSS"

构建脚本

Cloudflare Pages 在 clone 时只保留最近一次 commit 的 ref.

为了获取完整历史,参考 * 队 (cubercsl) 的 解决方法 ,用 git fetch --unshallow 获取所有 ref.

然后获取我的 PGP Key, 安装依赖,两个站点分别生成后,合并静态文件。


2022 年 7 月 31 日, * 队 (cubercsl) 亲自检阅了我的文章以后,提出了如下建议:

可以考虑在构建的脚本最开头加上 set -e 这样中间有返回值非 0 的语句就能直接让整个构建失败,否则中间如果有语句出了什么问题后脚本会继续执行

然后如果碰巧最后一句没问题,他整个返回值是 0, cf不会认为你构建有问题

对 在shebang后面


在构建站点的脚本里这样写:

#!/bin/bash

set -e

git fetch --unshallow

wget https://github.com/h3arn.gpg 
git config --global gpg.program gpg2
gpg2 --import h3arn.gpg

npm i hexo -g

cd zh
npm install --force
#ls -al
./export-history.sh
./mod-build.sh
mkdir -p public/en

cd ../en
npm install --force
#ls -al
./export-history.sh
./mod-build.sh
cp -r public/. ../zh/public/en/

cd ..