Hugoブログ環境の移行

はじめに

新たなPCを購入し、それに伴って、これまでの環境の移行をしている。これまでIT技術に関して、自分が体験、学習したことをhugo(テーマはbeautifulhugoを使って)GitHub Pagesにまとめていた。その移行に予想外に時間が掛かって、半日程度要したので、経緯を簡単にまとめる。

関連情報

  1. ゼロからHugoでWebページをつくって,GitHub Pagesで公開するまで - hugoのインストールなど参考になる。
  2. インストールからサーバ起動までやってみた(Mac) - 手順が簡潔にまとめてある。
  3. GitHub Pagesにブログを公開 - 自分が5年前に公開したページ

前提と方針

前提

新たなPCはMacBook Air、既にhomebrewはインストール済みで、gitも使える。またマークダウン文書を作成、閲覧できるマークダウンエディタは導入済み。自分は以前から使っているtyporaを使用している。

方針

新しいPCにhugoをインストールし、使えるようにした後、記事データ等を以前の環境から移行することにした。

手順

Hugoとテーマをインストール

 hugo new site blog
% git init
% git submodule add https://github.com/halogenica/beautifulhugo.git themes/beautifulhugo
% cp themes/beautifulhugo/exampleSite/hugo.toml .

Hugo.tomlはhugo v0.110以降に正式サポートされた設定ファイルである。以前のconfig.tomlと同じ働きをする。

最初は、hugo.tomlに設定すべき内容を追記・修正して定義ファイルにしようとしたが、以前のconfig.tomlとは記述方法が若干変わっていた。少し試してみたが、日本語と英語を切り替えた際のページタイトルなどが納得いくものが出来ず、以前の環境のconfig.tomlを移行することにした。

dosqis.html起因のHugo server起動時エラー

_default/single.html内でのエラー
% hugo server
Error: html/template:single.html:75:24: no such template "_internal/disqus.html"

次のように対応した。

 % mkdir -p layouts/_default
 % cp themes/beautifulhugo/layouts/_default/single.html layouts/_default
 ※ vi で single.html中のコメント機能を削除
            {{/* template "_internal/disqus.html" . */ }}
partials/disqus.html内でのエラー
% hugo server
Error: html/template:_partials/disqus.html:4:18: no such template "_internal/disqus.html"
% cp themes/beautifulhugo/layouts/partials/disqus.html layouts/partials/
 ※ viで、次のようにコメントアウト。
      {{/* template "_internal/disqus.html" . */}}

mdファイルでのエラー

今回の移行中に、記事(mdファイル)のURLに不正な文字列(エンコードエラーなど)により2つの記事がエラーとなって、ローカルにサーバが起動(% hugo server)できなかった。

今回、ChatGPTに記事のURLエンコードをチェックをさせた結果、2つの記事で、URLエンコード結果の文字列が長くなったことが原因のようだ。

ローカルリポジトリとリモート(GitHub Pages)を関連つける

関連情報3.で示した、以前の自分が投稿した記事の通り、次のように関連付けを行う。

% git remote add origin https://github.com/akenji3/blog
% git submodule add -b master https://github.com/akenji3/akenji3.github.io public

ここまでの手順を踏んで、ようやく新環境で、deploy.shが動くようになった。

#!/bin/sh

# If a command fails then the deploy stops
set -e

printf "\033[0;32mDeploying updates to GitHub...\033[0m\n"

# Build the project.
hugo # if using a theme, replace with `hugo -t <YOURTHEME>`

# Go To Public folder
cd public

# Add changes to git.
git add .

# Commit changes.
msg="rebuilding site $(date)"
if [ -n "$*" ]; then
	msg="$*"
fi
git commit -m "$msg"

# Push source and build repos.
git push origin master