Skip to content

Commit fe79548

Browse files
committed
Merge pull request #321 from lqez/master
Add anchor links to headings
2 parents 811dde5 + 64b29bb commit fe79548

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ To be released.
3030
[[#283], [#305] by GyuYong Jung]
3131
- Added syntax highlighting.
3232
[[#310], [#311] by MinJune Kim]
33+
- Added anchor links to types.
34+
[[#225], [#321] by Park Hyunwoo]
3335

3436
### Python target
3537

@@ -55,6 +57,7 @@ To be released.
5557
(from [spoqa/nirum](https://hub.docker.com/r/spoqa/nirum/)).
5658

5759
[#126]: https://github.com/nirum-lang/nirum/issues/126
60+
[#225]: https://github.com/nirum-lang/nirum/issues/225
5861
[#281]: https://github.com/nirum-lang/nirum/pull/281
5962
[#283]: https://github.com/spoqa/nirum/pull/283
6063
[#297]: https://github.com/nirum-lang/nirum/issues/297
@@ -64,6 +67,7 @@ To be released.
6467
[#310]: https://github.com/nirum-lang/nirum/issues/310
6568
[#311]: https://github.com/nirum-lang/nirum/pull/311
6669
[#318]: https://github.com/nirum-lang/nirum/pull/318
70+
[#321]: https://github.com/nirum-lang/nirum/pull/321
6771
[CommonMark]: http://commonmark.org/
6872
[table syntax extension]: https://github.github.com/gfm/#tables-extension-
6973
[special attributes extension]: https://michelf.ca/projects/php-markdown/extra/#spe-attr

app/nirum-docs.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ main = do
6565

6666
style :: Text
6767
style = [q|
68+
h2 a {
69+
visibility: hidden;
70+
}
71+
h2:hover a {
72+
visibility: visible;
73+
}
6874
.github-corner:hover .octo-arm {
6975
animation: octocat-wave 560ms ease-in-out;
7076
}

src/Nirum/Targets/Docs.hs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import Text.Blaze (ToMarkup (preEscapedToMarkup))
2424
import Text.Blaze.Html.Renderer.Utf8 (renderHtml)
2525
import Text.Cassius
2626
import Text.Hamlet (Html, shamlet)
27+
import Text.InterpolatedString.Perl6 (q)
2728

2829
import Nirum.Constructs (Construct (toCode))
2930
import Nirum.Constructs.Declaration (Documented (docsBlock))
@@ -114,6 +115,7 @@ $doctype 5
114115
<meta property="#{ogTag}" content="#{ogContent}">
115116
<link rel="stylesheet" href="#{root}style.css">
116117
<link rel="stylesheet" href="#{hljsCss}">
118+
<script src="#{root}nirum.js"></script>
117119
<script src="#{hljsJs}"></script>
118120
<script>hljs.initHighlightingOnLoad();</script>
119121
<body>
@@ -261,7 +263,7 @@ blockToHtml =
261263
typeDecl :: BoundModule Docs -> Identifier -> TD.TypeDeclaration -> Html
262264
typeDecl mod' ident
263265
tc@TD.TypeDeclaration { TD.type' = TD.Alias cname } = [shamlet|
264-
<h2>
266+
<h2 id="#{toNormalizedText ident}">
265267
type <dfn><code>#{toNormalizedText ident}</code></dfn> = #
266268
<code.type>#{typeExpression mod' cname}</code>
267269
$maybe d <- docsBlock tc
@@ -270,7 +272,7 @@ typeDecl mod' ident
270272
typeDecl mod' ident
271273
tc@TD.TypeDeclaration { TD.type' = TD.UnboxedType innerType } =
272274
[shamlet|
273-
<h2>
275+
<h2 id="#{toNormalizedText ident}">
274276
unboxed
275277
<dfn><code>#{toNormalizedText ident}</code>
276278
(<code>#{typeExpression mod' innerType}</code>)
@@ -279,7 +281,8 @@ typeDecl mod' ident
279281
|]
280282
typeDecl _ ident
281283
tc@TD.TypeDeclaration { TD.type' = TD.EnumType members } = [shamlet|
282-
<h2>enum <dfn><code>#{toNormalizedText ident}</code></dfn>
284+
<h2 id="#{toNormalizedText ident}">
285+
enum <dfn><code>#{toNormalizedText ident}</code></dfn>
283286
$maybe d <- docsBlock tc
284287
#{blockToHtml d}
285288
<dl class="members">
@@ -292,7 +295,8 @@ typeDecl _ ident
292295
|]
293296
typeDecl mod' ident
294297
tc@TD.TypeDeclaration { TD.type' = TD.RecordType fields } = [shamlet|
295-
<h2>record <dfn><code>#{toNormalizedText ident}</code></dfn>
298+
<h2 id="#{toNormalizedText ident}">
299+
record <dfn><code>#{toNormalizedText ident}</code></dfn>
296300
$maybe d <- docsBlock tc
297301
#{blockToHtml d}
298302
<dl.fields>
@@ -311,7 +315,8 @@ typeDecl mod' ident
311315
}
312316
} =
313317
[shamlet|
314-
<h2>union <dfn><code>#{toNormalizedText ident}</code></dfn>
318+
<h2 id="#{toNormalizedText ident}">
319+
union <dfn><code>#{toNormalizedText ident}</code></dfn>
315320
$maybe d <- docsBlock tc
316321
#{blockToHtml d}
317322
$forall (default_, tagDecl@(TD.Tag _ fields _)) <- tagList
@@ -340,12 +345,14 @@ typeDecl mod' ident
340345
]
341346
typeDecl _ ident
342347
TD.TypeDeclaration { TD.type' = TD.PrimitiveType {} } = [shamlet|
343-
<h2>primitive <code>#{toNormalizedText ident}</code>
348+
<h2 id="#{toNormalizedText ident}">
349+
primitive <code>#{toNormalizedText ident}</code>
344350
|]
345351
typeDecl mod' ident
346352
tc@TD.ServiceDeclaration { TD.service = S.Service methods } =
347353
[shamlet|
348-
<h2>service <dfn><code>#{toNormalizedText ident}</code></dfn>
354+
<h2 id="#{toNormalizedText ident}">
355+
service <dfn><code>#{toNormalizedText ident}</code></dfn>
349356
$maybe d <- docsBlock tc
350357
#{blockToHtml d}
351358
$forall md@(S.Method _ ps ret err _) <- DES.toList methods
@@ -520,6 +527,11 @@ h1, h2, h3, h4, h5, h6, dt
520527
font-weight: bold
521528
code
522529
font-weight: 400
530+
h2 a.pilcrow
531+
visibility: hidden
532+
margin-left: 10px
533+
h2:hover a.pilcrow
534+
visibility: visible
523535
a
524536
text-decoration: none
525537
a:link
@@ -610,10 +622,24 @@ footer
610622
navWidth :: PixelSize
611623
navWidth = PixelSize 300
612624

625+
javascript :: T.Text
626+
javascript = [q|
627+
window.addEventListener('load', function () {
628+
document.querySelectorAll('h2[id]').forEach(function(node) {
629+
var anchor = document.createElement("a");
630+
anchor.className = "pilcrow";
631+
anchor.text = "\xb6";
632+
anchor.href = "#" + node.id;
633+
node.appendChild(anchor);
634+
});
635+
});
636+
|]
637+
613638
compilePackage' :: Package Docs -> Map FilePath (Either Error BS.ByteString)
614639
compilePackage' pkg = unions
615640
[ fromList
616641
[ ("style.css", Right $ encodeUtf8 css)
642+
, ("nirum.js", Right $ encodeUtf8 javascript)
617643
, ( "index.html"
618644
, Right $ toStrict $ renderHtml $ case readme of
619645
Nothing -> contents pkg

0 commit comments

Comments
 (0)