数日前よりCal-Heatmapを使って、トップページにGitHub風のアクティビティカレンダーを設置してみた。完全に管理人の自己満であり、飽きたら消す可能性はある。ちなみに、GitHubのはContribution Calendarと呼ぶらしい。

執筆時点でのアクティビティ

執筆時点でのアクティビティ

このヒートマップの指標はページ文字数でHugoのWordCountをそのまま呼び出したんだけど、どうも計算がおかしかったので、ソースを確認する。

if isCJKLanguage {
	result.wordCount = 0
	for _, word := range result.plainWords {
		runeCount := utf8.RuneCountInString(word)
		if len(word) == runeCount {
			result.wordCount++
		} else {
			result.wordCount += runeCount
		}
	}
} else {
	result.wordCount = helpers.TotalWords(result.plain)
}

どうやら、 isCJKLanguage に入ってなくて、 helpers.TotalWords を呼び出しているらしい。そちらのコードはというと

// TotalWords counts instance of one or more consecutive white space
// characters, as defined by unicode.IsSpace, in s.
// This is a cheaper way of word counting than the obvious len(strings.Fields(s)).
func TotalWords(s string) int {
	n := 0
	inWord := false
	for _, r := range s {
		wasInWord := inWord
		inWord = !unicode.IsSpace(r)
		if inWord && !wasInWord {
			n++
		}
	}
	return n
}

なるほど、スペースあるいは改行区切りでカウントアップしているので、日本語記事には都合が悪い。

つまり、 isCJKLanguage = true となれば良いだけ。Hugoの設定ファイルに

hasCJKLanguage: true

を加える。解決。本当は日本語に強いトークナイザーとか使うのがより直感的なんだろうけど。