本地预览
这个仓库是 Jekyll 页面。当前项目使用 Ruby 3.2.6 和锁定的 GitHub Pages 依赖。
启动步骤
在仓库根目录执行:
# 使用项目当前的 Ruby 版本
export PATH="$HOME/.rbenv/versions/3.2.6/bin:$PATH"
# 首次启动时安装依赖;依赖会放在 vendor/bundle/
bundle install
# 如果镜像索引出现 checksum 或 416 错误,改用完整索引:
# bundle install --full-index
# Jekyll 3.9 / Liquid 4 在 Ruby 3.2 上仍会调用已移除的 tainted? API。
# 下面的兼容文件只写入 /tmp,不会修改页面源码。
cat >/tmp/myhomepage-ruby-compat.rb <<'RUBY'
class Object
def tainted?; false; end unless method_defined?(:tainted?)
def taint; self; end unless method_defined?(:taint)
def untaint; self; end unless method_defined?(:untaint)
end
RUBY
# 离线构建,避免 Jekyll GitHub Metadata 在本地预览时访问 GitHub API。
cat >/tmp/myhomepage-build.rb <<'RUBY'
require '/tmp/myhomepage-ruby-compat'
require 'bundler/setup'
require 'jekyll'
require 'jekyll-github-metadata'
module Jekyll::GitHubMetadata
class Client
def internet_connected?; false; end
end
end
load Gem.bin_path('jekyll', 'jekyll')
RUBY
ruby /tmp/myhomepage-build.rb build --config _config.yml,_config_local.yml
# 在另一个终端启动静态预览服务
python3 -m http.server 4000 --bind 127.0.0.1 --directory _site
浏览器打开 http://127.0.0.1:4000。
修改页面后重新执行构建命令即可刷新 _site/。如果 4000 端口已被占用,先停止旧的 python3 -m http.server 进程,或改用其他端口并同步修改访问地址。
本地文件说明
_config_local.yml提供本地url和baseurl。_plugins/jekyll-github-metadata-stub.rb避免私有仓库预览时请求 GitHub API。vendor/bundle/是本机依赖目录,不应提交到远程仓库。