Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
Adds table of contents when width permits
Browse files Browse the repository at this point in the history
  • Loading branch information
aetter committed Jan 1, 2020
1 parent db2c669 commit bfcf72e
Show file tree
Hide file tree
Showing 7 changed files with 333 additions and 165 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ source "https://rubygems.org"
gem "jekyll", "~> 3.8.5"

# This is the default theme for new Jekyll sites. You may change this to anything you like.
gem "just-the-docs", "~> 0.2.5"
gem "just-the-docs", "~> 0.2.7"

# If you want to use GitHub Pages, remove the "gem "jekyll"" above and
# uncomment the line below. To upgrade, run `bundle update github-pages`.
Expand Down
4 changes: 4 additions & 0 deletions THIRD-PARTY
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Copyright (c) 2016 Patrick Marsceill

** (MIT License) Jekyll Pure Liquid Table of Contents 1.0.8 - https://github.com/allejo/jekyll-toc

Copyright (c) 2017 Vladimir Jimenez

** (Apache License, Version 2.0) Elasticsearch 7.4.2 - https://github.com/elastic/elasticsearch

Elasticsearch
Expand Down
96 changes: 96 additions & 0 deletions _includes/toc.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{% capture tocWorkspace %}
{% comment %}
Version 1.0.8
https://github.com/allejo/jekyll-toc

"...like all things liquid - where there's a will, and ~36 hours to spare, there's usually a/some way" ~jaybe

Usage:
{% include toc.html html=content sanitize=true class="inline_toc" id="my_toc" h_min=2 h_max=3 %}

Parameters:
* html (string) - the HTML of compiled markdown generated by kramdown in Jekyll

Optional Parameters:
* sanitize (bool) : false - when set to true, the headers will be stripped of any HTML in the TOC
* class (string) : '' - a CSS class assigned to the TOC
* id (string) : '' - an ID to assigned to the TOC
* h_min (int) : 1 - the minimum TOC header level to use; any header lower than this value will be ignored
* h_max (int) : 6 - the maximum TOC header level to use; any header greater than this value will be ignored
* ordered (bool) : false - when set to true, an ordered list will be outputted instead of an unordered list
* item_class (string) : '' - add custom class(es) for each list item; has support for '%level%' placeholder, which is the current heading level
* baseurl (string) : '' - add a base url to the TOC links for when your TOC is on another page than the actual content
* anchor_class (string) : '' - add custom class(es) for each anchor element

Output:
An ordered or unordered list representing the table of contents of a markdown block. This snippet will only
generate the table of contents and will NOT output the markdown given to it
{% endcomment %}

{% capture my_toc %}{% endcapture %}
{% assign orderedList = include.ordered | default: false %}
{% assign minHeader = include.h_min | default: 1 %}
{% assign maxHeader = include.h_max | default: 6 %}
{% assign nodes = include.html | split: '<h' %}
{% assign firstHeader = true %}

{% capture listModifier %}{% if orderedList %}1.{% else %}-{% endif %}{% endcapture %}

{% for node in nodes %}
{% if node == "" %}
{% continue %}
{% endif %}

{% assign headerLevel = node | replace: '"', '' | slice: 0, 1 | times: 1 %}

{% if headerLevel < minHeader or headerLevel > maxHeader %}
{% continue %}
{% endif %}

{% if firstHeader %}
{% assign firstHeader = false %}
{% assign minHeader = headerLevel %}
{% endif %}

{% assign indentAmount = headerLevel | minus: minHeader %}
{% assign _workspace = node | split: '</h' %}

{% assign _idWorkspace = _workspace[0] | split: 'id="' %}
{% assign _idWorkspace = _idWorkspace[1] | split: '"' %}
{% assign html_id = _idWorkspace[0] %}

{% assign _classWorkspace = _workspace[0] | split: 'class="' %}
{% assign _classWorkspace = _classWorkspace[1] | split: '"' %}
{% assign html_class = _classWorkspace[0] %}

{% if html_class contains "no_toc" %}
{% continue %}
{% endif %}

{% capture _hAttrToStrip %}{{ _workspace[0] | split: '>' | first }}>{% endcapture %}
{% assign header = _workspace[0] | replace: _hAttrToStrip, '' %}

{% assign space = '' %}
{% for i in (1..indentAmount) %}
{% assign space = space | prepend: ' ' %}
{% endfor %}

{% unless include.item_class == blank %}
{% capture listItemClass %}{:.{{ include.item_class | replace: '%level%', headerLevel }}}{% endcapture %}
{% endunless %}

{% capture heading_body %}{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}{% endcapture %}
{% capture my_toc %}{{ my_toc }}
{{ space }}{{ listModifier }} {{ listItemClass }} [{{ heading_body | replace: "|", "\|" }}]({% if include.baseurl %}{{ include.baseurl }}{% endif %}#{{ html_id }}){% if include.anchor_class %}{:.{{ include.anchor_class }}}{% endif %}{% endcapture %}
{% endfor %}

{% if include.class %}
{% capture my_toc %}{:.{{ include.class }}}
{{ my_toc | lstrip }}{% endcapture %}
{% endif %}

{% if include.id %}
{% capture my_toc %}{: #{{ include.id }}}
{{ my_toc | lstrip }}{% endcapture %}
{% endif %}
{% endcapture %}{% assign tocWorkspace = '' %}{{ my_toc | markdownify | strip }}
3 changes: 3 additions & 0 deletions _layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ <h2 class="text-delta">Table of contents</h2>
</div>
</div>
</div>
<div class="toc">
{% include toc.html html=content h_min=2 h_max=3 class="toc-list" item_class="toc-item" sanitize=true %}
</div>
{% if site.anchor_links != nil %}
<script>
anchors.add();
Expand Down
156 changes: 1 addition & 155 deletions _sass/custom/custom.scss
Original file line number Diff line number Diff line change
@@ -1,155 +1 @@
@import url('https://fonts.googleapis.com/css?family=Open+Sans:400,400i,600,700');

body {
padding-bottom: 6rem;
font-family: 'Open Sans', sans-serif;
@include mq(md) {
padding-bottom: 0;
}
}

.page-content {
ol {
> li {
&:before {
color: $grey-dk-100;
}
}
}
ul {
> li {
&:before {
color: $grey-dk-100;
}
}
}
}

.site-title {
@include mq(md) {
padding-top: 0;
padding-bottom: 0;
padding-left: $sp-5;
}
img {
height: 100%;
max-width: 208px;
}
}

.navigation-list-toggle {
top: $sp-5;
}

h1, h2, h3, h4, h5, h6 {
margin-top: 2.4rem;
}

.img-border {
border: 1px solid $grey-lt-200;
}

// Note, tip, and warning blocks

%callout {
border: 1px solid $grey-lt-200;
border-radius: 5px;
margin: 1rem 0;
padding: 1rem;
position: relative;
}

.note {
@extend %callout;
border-left: 5px solid $blue-300;
}

.tip {
@extend %callout;
border-left: 5px solid $green-300;
}

.warning {
@extend %callout;
border-left: 5px solid $red-300;
}

// Labels

.label,
.label-blue {
background-color: $blue-300;
}

.label-green {
background-color: $green-300;
}

.label-purple {
background-color: $purple-300;
}

.label-red {
background-color: $red-300;
}

.label-yellow {
color: $grey-dk-200;
background-color: $yellow-300;
}

// Buttons

.btn-primary {
@include btn-color($white, $btn-primary-color);
}

.btn-purple {
@include btn-color($white, $purple-dk-100);
}

.btn-blue {
@include btn-color($white, $blue-300);
}

.btn-green {
@include btn-color($white, $green-300);
}

// Tables

th,
td {
border-bottom: $border rgba($table-border-color, 0.5);
border-left: $border $table-border-color;
}

thead {
th {
border-bottom: 1px solid $table-border-color;
}
}
td {
pre {
margin-bottom: 0;
}
}

// Keeps labels high and tight next to headers

h1 + p.label {
margin: -46px 0 0 0;
}
h2 + p.label {
margin: -30px 0 0 0;
}
h3 + p.label {
margin: -20px 0 0 0;
}
h4 + p.label,
h6 + p.label {
margin: -14px 0 0 0;
}
h5 + p.label {
margin: -15px 0 0 0;
}
$toc-width: 200px !default;
Loading

0 comments on commit bfcf72e

Please sign in to comment.