Initial commit
[haskell-blog.git] / posts / hakyll.md
1 ---
2 title: ⌨️ MMark and Hakyll
3 date: 2018-07-29
4 ---
5
6 I've moved over the blog to Hakyll, so things are a bit less makeshfit.
7
8 I really the syntax highlighting, but one problem I noticed was that the code blocks didn't show up on Safari Reader.
9 After doing a bit of digging it turned out that some time around Pandoc 2.0.4, skylighting
10 started adding line numbers that had an `<a>` tag, which seemed to throw Safari off.
11
12 ```html
13 <code class="sourceCode haskell">
14     <a class="sourceLine" id="cb1-1" data-line-number="1">
15         <span class="kw">data</span>
16         <span class="dt">RequestMessage</span> m req resp <span class="fu">=</span>
17         <span class="fu">...</span>
18     </a>
19 </code>
20 ```
21
22 Unfortunately there doesn't seem to be a way to turn this off, and rather than try to strip off those tags myself,
23 I decided just to use another compiler other than Pandoc.
24
25 Since I was only going to be compiling markdown, I landed on [MMark](https://hackage.haskell.org/package/mmark).
26 Unlike other compilers, MMark is strict - I plugged it into hakyll:
27
28 ```haskell
29 mmarkCompiler :: Compiler (Item String)
30 mmarkCompiler = do
31   fp <- getResourceFilePath
32   getResourceLBS >>= withItemBody (\lbs ->
33     let text = T.toStrict $ T.decodeUtf8 lbs
34     in case MMark.parse fp text of
35         Left e -> error (MMark.parseErrorsPretty text e)
36         Right doc -> return $ T.unpack $ Lucid.renderText $ MMark.render doc
37 ```
38
39 And right off the bat it picked up a bunch of mistakes in my existing blog posts:
40
41 ```
42 Initialising...
43   Creating store...
44   Creating provider...
45   Running rules...
46 Checking for out-of-date items
47 Compiling
48   updated templates/default.html
49   updated about.rst
50   updated templates/post.html
51   updated posts/hakyll.md
52   updated posts/haskellMake.md
53   updated posts/hello.md
54   [ERROR] ./posts/lens.md:177:66:
55     |
56 177 | In `haskell-lsp` these are also generated via (Template Haskell)[https://artyom.me/lens-over-tea-6].
57     |                                                                  ^
58 could not find a matching reference definition for "https://artyom.me/lens-over-tea-6"
59 ```
60
61 It also comes with an extension for [ghc-syntax-highlighter](https://markkarpov.com/post/announcing-ghc-syntax-highlighter.html),
62 which was what I came for in the first place.
63 Setting it up was super easy:
64
65 ```haskell
66 MMark.render $ MMark.useExtensions extensions doc
67   where extensions = [ MMark.ghcSyntaxHighlighter
68                      , MMark.skylighting
69                      ]
70 ```
71
72 And now I have clean, Safari-readable tags:
73
74 ```html
75 <code class="sourceCode haskell">
76     <span class="kw">data</span>
77     <span class="dt">RequestMessage</span> m req resp <span class="fu">=</span>
78     <span class="fu">...</span>
79 </code>
80 ```
81