Skip to content

Commit

Permalink
Toc sidebar (alshedivat#1366)
Browse files Browse the repository at this point in the history
Implemented alshedivat#966.


![image](https://user-images.githubusercontent.com/31376482/234411523-b02087d9-a437-4f77-9f54-de5eef122439.png)

---------

Signed-off-by: George Araujo <george.gcac@gmail.com>
  • Loading branch information
george-gca authored and adityarauniyar committed Jan 17, 2024
1 parent 522ba2c commit 94d2aac
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 6 deletions.
7 changes: 6 additions & 1 deletion _includes/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
<!-- Code Syntax Highlighting -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/jwarby/jekyll-pygments-themes@master/{{ site.highlight_theme_light | append: '.css' }}" media="" id="highlight_theme_light" />

{% if page.toc and page.toc.sidebar %}
<!-- Sidebar Table of Contents -->
<link href="https://cdn.rawgit.com/afeld/bootstrap-toc/v1.0.1/dist/bootstrap-toc.min.css" rel="stylesheet" />
{% endif %}

<!-- Styles -->
{% if site.icon.size <= 4 %}
<link rel="shortcut icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>{{ site.icon }}</text></svg>">
Expand All @@ -21,7 +26,7 @@
{% endif %}
<link rel="stylesheet" href="{{ '/assets/css/main.css' | relative_url }}">
<link rel="canonical" href="{{ page.url | replace:'index.html','' | absolute_url }}">

<!-- Dark Mode -->
{% if site.enable_darkmode %}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/jwarby/jekyll-pygments-themes@master/{{ site.highlight_theme_dark | append: '.css' }}" media="none" id="highlight_theme_dark" />
Expand Down
5 changes: 5 additions & 0 deletions _includes/scripts/misc.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
<script defer src="{{ '/assets/js/zoom.js' | relative_url }}"></script>
{%- endif -%}

{% if page.toc and page.toc.sidebar %}
<!-- Sidebar Table of Contents -->
<script defer src="https://cdn.rawgit.com/afeld/bootstrap-toc/v1.0.1/dist/bootstrap-toc.min.js"></script>
{% endif %}

<!-- Load Common JS -->
<script defer src="{{ '/assets/js/common.js' | relative_url }}"></script>
<script defer src="{{ '/assets/js/copy_code.js' | relative_url }}" type="text/javascript"></script>
28 changes: 27 additions & 1 deletion _layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,33 @@

<!-- Content -->
<div class="container mt-5">
{{ content }}
{% if page.toc and page.toc.sidebar %}
{% if page.toc.sidebar == "right" %}
<div class="row">
<!-- main content area -->
<div class="col-sm-9">
{{ content }}
</div>
<!-- sidebar, which will move to the top on a small screen -->
<div class="col-sm-3">
<nav id="toc-sidebar" class="sticky-top"></nav>
</div>
</div>
{% else %}
<div class="row">
<!-- sidebar, which will move to the top on a small screen -->
<div class="col-sm-3">
<nav id="toc-sidebar" class="sticky-top"></nav>
</div>
<!-- main content area -->
<div class="col-sm-9">
{{ content }}
</div>
</div>
{% endif %}
{% else %}
{{ content }}
{% endif %}
</div>

<!-- Footer -->
Expand Down
2 changes: 1 addition & 1 deletion _layouts/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ <h1 class="post-title">{{ page.title }}</h1>
</header>

<article class="post-content">
{% if page.toc %}
{% if page.toc and page.toc.beginning %}
<div id="table-of-contents">
{% toc %}
</div>
Expand Down
12 changes: 9 additions & 3 deletions _posts/2023-03-20-table-of-contents.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ description: an example of a blog post with table of contents
categories: sample-posts toc
giscus_comments: true
related_posts: false
toc: true
toc:
beginning: true
---
This post shows how to add a table of contents.
This post shows how to add a table of contents in the beginning of the post.

## Adding a Table of Contents

To add a table of contents to a post, simply add `toc: true` to the front matter of the post. The table of contents will be automatically generated from the headings in the post.
To add a table of contents to a post, simply add
```yml
toc:
beginning: true
```
to the front matter of the post. The table of contents will be automatically generated from the headings in the post.
### Example of Sub-Heading 1
Expand Down
42 changes: 42 additions & 0 deletions _posts/2023-04-25-sidebar-table-of-contents.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
layout: post
title: a post with table of contents on a sidebar
date: 2023-04-25 10:14:00-0400
description: an example of a blog post with table of contents on a sidebar
categories: sample-posts toc sidebar
giscus_comments: true
related_posts: false
toc:
sidebar: left
---
This post shows how to add a table of contents as a sidebar.

## Adding a Table of Contents

To add a table of contents to a post as a sidebar, simply add
```yml
toc:
sidebar: left
```
to the front matter of the post. The table of contents will be automatically generated from the headings in the post. If you wish to display the sidebar to the right, simply change `left` to `right`.

### Example of Sub-Heading 1

Jean shorts raw denim Vice normcore, art party High Life PBR skateboard stumptown vinyl kitsch. Four loko meh 8-bit, tousled banh mi tilde forage Schlitz dreamcatcher twee 3 wolf moon. Chambray asymmetrical paleo salvia, sartorial umami four loko master cleanse drinking vinegar brunch. <a href="https://www.pinterest.com">Pinterest</a> DIY authentic Schlitz, hoodie Intelligentsia butcher trust fund brunch shabby chic Kickstarter forage flexitarian. Direct trade <a href="https://en.wikipedia.org/wiki/Cold-pressed_juice">cold-pressed</a> meggings stumptown plaid, pop-up taxidermy. Hoodie XOXO fingerstache scenester Echo Park. Plaid ugh Wes Anderson, freegan pug selvage fanny pack leggings pickled food truck DIY irony Banksy.

### Example of another Sub-Heading 1

Jean shorts raw denim Vice normcore, art party High Life PBR skateboard stumptown vinyl kitsch. Four loko meh 8-bit, tousled banh mi tilde forage Schlitz dreamcatcher twee 3 wolf moon. Chambray asymmetrical paleo salvia, sartorial umami four loko master cleanse drinking vinegar brunch. <a href="https://www.pinterest.com">Pinterest</a> DIY authentic Schlitz, hoodie Intelligentsia butcher trust fund brunch shabby chic Kickstarter forage flexitarian. Direct trade <a href="https://en.wikipedia.org/wiki/Cold-pressed_juice">cold-pressed</a> meggings stumptown plaid, pop-up taxidermy. Hoodie XOXO fingerstache scenester Echo Park. Plaid ugh Wes Anderson, freegan pug selvage fanny pack leggings pickled food truck DIY irony Banksy.

## Customizing Your Table of Contents
{:data-toc-text="Customizing"}

If you want to learn more about how to customize the table of contents of your sidebar, you can check the [bootstrap-toc](https://afeld.github.io/bootstrap-toc/) documentation. Notice that you can even customize the text of the heading that will be displayed on the sidebar.

### Example of Sub-Heading 2

Jean shorts raw denim Vice normcore, art party High Life PBR skateboard stumptown vinyl kitsch. Four loko meh 8-bit, tousled banh mi tilde forage Schlitz dreamcatcher twee 3 wolf moon. Chambray asymmetrical paleo salvia, sartorial umami four loko master cleanse drinking vinegar brunch. <a href="https://www.pinterest.com">Pinterest</a> DIY authentic Schlitz, hoodie Intelligentsia butcher trust fund brunch shabby chic Kickstarter forage flexitarian. Direct trade <a href="https://en.wikipedia.org/wiki/Cold-pressed_juice">cold-pressed</a> meggings stumptown plaid, pop-up taxidermy. Hoodie XOXO fingerstache scenester Echo Park. Plaid ugh Wes Anderson, freegan pug selvage fanny pack leggings pickled food truck DIY irony Banksy.

### Example of another Sub-Heading 2

Jean shorts raw denim Vice normcore, art party High Life PBR skateboard stumptown vinyl kitsch. Four loko meh 8-bit, tousled banh mi tilde forage Schlitz dreamcatcher twee 3 wolf moon. Chambray asymmetrical paleo salvia, sartorial umami four loko master cleanse drinking vinegar brunch. <a href="https://www.pinterest.com">Pinterest</a> DIY authentic Schlitz, hoodie Intelligentsia butcher trust fund brunch shabby chic Kickstarter forage flexitarian. Direct trade <a href="https://en.wikipedia.org/wiki/Cold-pressed_juice">cold-pressed</a> meggings stumptown plaid, pop-up taxidermy. Hoodie XOXO fingerstache scenester Echo Park. Plaid ugh Wes Anderson, freegan pug selvage fanny pack leggings pickled food truck DIY irony Banksy.
14 changes: 14 additions & 0 deletions _sass/_base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -860,3 +860,17 @@ progress::-moz-progress-bar {
opacity: 1;
}
}

nav[data-toggle="toc"] {
top: 5rem;
}

/* small screens */
@media (max-width: 576px) {
/* override stickyness so that the navigation does not follow scrolling */
nav[data-toggle="toc"] {
visibility: hidden;
height: 0;
top: 0;
}
}
12 changes: 12 additions & 0 deletions assets/js/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,15 @@ $(document).ready(function() {
});
$('a').removeClass('waves-effect waves-light');
});

// bootstrap-toc
$(function () {
if($('#toc-sidebar').length){
var navSelector = "#toc-sidebar";
var $myNav = $(navSelector);
Toc.init($myNav);
$("body").scrollspy({
target: navSelector,
});
}
});

0 comments on commit 94d2aac

Please sign in to comment.