静的サイト生成ツールHakyllの強みは,Pandocを使ってテキスト処理をしていること。PandocはMathJax出力ができるので,Hakyllを使って生成するサイトにもMathJaxが使えるはずである。
hakyll-initした既定のままでは,MathJaxは利用できない。Hakyllの設定ファイルは,Haskellソース。Qnikst blogのポストを参考にsite.hsを修正する。
まず,ライブラリを追加する。
import qualified Data.Map as M import Text.Pandoc
Pandocにオプションを渡すために,pandocCompilerではなく,pandocCompilerWithを使う。オプションと言ってもコマンドラインオプションではなく,PandocのAPIの引数として渡す。
match "posts/*" $ do
route $ setExtension "html"
compile $ pandocCompilerWith defaultHakyllReaderOptions pandocOptions
>>= saveSnapshot "content"
>>= return . fmap demoteHeaders
>>= loadAndApplyTemplate "templates/post.html" postCtx
>>= loadAndApplyTemplate "templates/default.html" (mathCtx `mappend` postCtx)
ここでは,template/default.htmlでMathJaxを使う。このテンプレートを適用する際に,mathCtxによりタグ$mathjax$を調べる。数式を使う場合にのみ,MathJaxのJavaScriptを読むようにする。このテンプレートは,数箇所使われているのでmappendを使い適宜mathCtxを追加する。 mathCtxの定義はbloggerでうまく表示できないので上記ポスト参照。
pandocOptions :: WriterOptions
pandocOptions = defaultHakyllWriterOptions{ writerHTMLMathMethod = MathJax "" }
$mathjax$が空のときに空行が入らないようにcssと同じ行にタグを付けた。
$mathjax$
---
title: MathJax
mathjax: on
---
$sqrt{frac{1}{2}}$はinline math.
$$sqrt{frac{1}{2}}$$はdisplay math.