Skip to content

Commit

Permalink
WIP Add table of contents to articles (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkudrna committed Jun 8, 2022
1 parent 31305d7 commit ee04035
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
},
"globals": {
"gtag": false,
"snippetsData": false
"snippetsData": false,
"tocbot": false
},
"parserOptions": {
"ecmaVersion": 10,
Expand Down
38 changes: 38 additions & 0 deletions assets/js/table-of-contents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const TOC_ID = 'obsah';

const generateToc = () => {
tocbot.init({
activeLinkClass: 'is-active',
collapsibleClass: 'is-collapsible',
contentSelector: '.js-table-of-contents-source',
headingSelector: 'h1, h2, h3',
isCollapsedClass: 'is-collapsed',
linkClass: 'table-of-contents__link',
listClass: 'table-of-contents__list',
listItemClass: 'table-of-contents__list__item',
scrollSmooth: false,
tocSelector: `#${TOC_ID}`,
});

document.getElementById(TOC_ID).classList.add('table-of-contents--active');
};

const destroyToc = () => {
document.getElementById(TOC_ID).classList.remove('table-of-contents--active');
tocbot.destroy();
};

const updateToc = () => {
const tocEl = document.getElementById(TOC_ID);

if (window.getComputedStyle(tocEl).display !== 'none') {
if (!tocEl.innerHTML) {
generateToc();
}
} else if (tocEl.innerHTML) {
destroyToc();
}
};

window.addEventListener('load', () => updateToc());
window.addEventListener('resize', () => updateToc());
1 change: 1 addition & 0 deletions assets/scss/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
@use "styles/05-components/members-form";
@use "styles/05-components/navigation";
@use "styles/05-components/post-card";
@use "styles/05-components/table-of-contents";
@use "styles/05-components/text-field";

// 6. Editor
Expand Down
42 changes: 42 additions & 0 deletions assets/scss/styles/05-components/_table-of-contents.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
@use '../../settings/container';
@use '../../tools/breakpoint';
@use '../../tools/spacing';
@use '../../tools/transition';
@use '../../tools/typography';

$_breakpoint: xl;

.table-of-contents {
@include breakpoint.up($_breakpoint) {
@include transition.add('visibility, opacity');

position: sticky;
top: 0;
right: 0;
display: block;
//visibility: hidden;
//opacity: 0;
width: calc((100vw - #{container.$article-width}) / 2);
height: 0;
padding: spacing.of(2) container.$padding-sm;
line-height: typography.line-height(dense);
will-change: visibility, opacity;
}
}

.table-of-contents__list {
//@include breakpoint.up($_breakpoint) {
@include typography.style(small);
//}
}

.table-of-contents__list .table-of-contents__list {
font-size: inherit;
}

.table-of-contents--active {
//@include breakpoint.up($_breakpoint) {
// visibility: visible;
// opacity: 1;
//}
}
1 change: 1 addition & 0 deletions partials/external/tocbot.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/tocbot/4.12.0/tocbot.min.js"></script>
12 changes: 11 additions & 1 deletion post.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,16 @@

</header>

{{^has tag="galerie"}}
<div> {{!-- TOC holder for `position: sticky` --}}
<div id="obsah" class="table-of-contents" hidden></div>
</div>
{{/has}}

<div class="section section--roomy-bottom">
<div class="container container--article">

<div class="editor editor--long-text js-nbsp js-external-links js-heading-anchors">
<div class="editor editor--long-text js-nbsp js-external-links js-heading-anchors js-table-of-contents-source">
{{content}}
</div>

Expand Down Expand Up @@ -168,6 +174,10 @@
{{/contentFor}}

{{#contentFor "scripts"}}
{{> "external/tocbot"}}

<script src="{{asset "js/table-of-contents.js"}}"></script>

{{> "external/prism-js"}}
{{/contentFor}}

Expand Down

0 comments on commit ee04035

Please sign in to comment.