Skip to content

Commit

Permalink
Add an extra level of rhs nav to the api docs
Browse files Browse the repository at this point in the history
  • Loading branch information
superstructor committed Aug 27, 2020
1 parent e9f536b commit 6996500
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 2 deletions.
5 changes: 4 additions & 1 deletion docs/src/ns_to_markdown.clj
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@

(defn ns->markdown
[m]
(format "# %s\n\n" (:name m)))
;; The 3 dashes and apidocs:true is a markdown meta extension header used by
;; the themes/partials/toc.html and themes/partials/toc-item.html templates
;; to render an extra navigation level in the rhs table of contents.
(format "---\napidocs: true\n---\n\n# %s\n\n" (:name m)))

(defn arglist->markdown
[arglist]
Expand Down
52 changes: 52 additions & 0 deletions docs/theme/partials/toc-item.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!--
Copyright (c) 2016-2020 Martin Donath <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->

<!-- Table of contents item -->
<li class="md-nav__item">
<a href="{{ toc_item.url }}" class="md-nav__link">
{{ toc_item.title }}
</a>

<!-- Render nested item list -->

<!--
CUSTOM: re-frame customization so that we can have one extra level in the
rhs table of contents *only* on the apidocs page.
See toc.html comment for introduction.
This code works by:
- incrementing the current level
- checking if the current level is equal or less than the maximum depth
- if true, resursively rendering this template
- if false, doing nothing
- decrementing the current level
-->
{% set re_frame_navigation_level = re_frame_navigation_level + 1 %}
{% if re_frame_navigation_level <= re_frame_navigation_depth and toc_item.children %}
<nav class="md-nav" aria-label="{{ toc_item.title }}">
<ul class="md-nav__list">
{% for toc_item in toc_item.children %}
{% include "partials/toc-item.html" %}
{% endfor %}
</ul>
</nav>
{% endif %}
{% set re_frame_navigation_level = re_frame_navigation_level - 1 %}
</li>
75 changes: 75 additions & 0 deletions docs/theme/partials/toc.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<!--
Copyright (c) 2016-2020 Martin Donath <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->

{% import "partials/language.html" as lang with context %}

<!-- Table of contents -->
<nav class="md-nav md-nav--secondary" aria-label="{{ lang.t('toc.title') }}">
{% set toc = page.toc %}

<!--
Hack: check whether the content contains a h1 headline. If it does, the
top-level anchor must be skipped, since it would be redundant to the link
to the current page that is located just above the anchor. Therefore we
directly continue with the children of the anchor.
-->
{% if toc | first is defined and "\x3ch1 id=" in page.content %}
{% set toc = (toc | first).children %}
{% endif %}

<!-- Render item list -->


<!--
CUSTOM: re-frame customization so that we can have one extra level in the
rhs table of contents *only* on the apidocs page.
Globally in mkdocs.yml we set the table of contents extension depth to the
maximum that we want to use site-wide, then filter it down depending on
the markdown meta extension on a page-per-page basis.
First, we need to initialize some variables that are used in toc-item.html
re_frame_navigation_depth is set depending on the markdown meta extension.
It is the maximum depth for the rhs table of contents.
re_frame_navigation_level is the current recursive rendering depth of
toc-item.html It must be both incremented and decremented on each recur.
-->
{% if page and page.meta and page.meta.apidocs %}
{% set re_frame_navigation_depth = 2 %}
{% else %}
{% set re_frame_navigation_depth = 1 %}
{% endif %}
{% set re_frame_navigation_level = 1 %}

{% if toc | first is defined %}
<label class="md-nav__title" for="__toc">
<span class="md-nav__icon md-icon">
{% include ".icons/material/arrow-left.svg" %}
</span>
{{ lang.t("toc.title") }}
</label>
<ul class="md-nav__list" data-md-scrollfix>
{% for toc_item in toc %}
{% include "partials/toc-item.html" %}
{% endfor %}
</ul>
{% endif %}
</nav>
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ markdown_extensions:
- pymdownx.inlinehilite
- toc:
permalink: true
toc_depth: 2
toc_depth: 3
- codehilite:
guess_lang: false

Expand Down

0 comments on commit 6996500

Please sign in to comment.