Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Add table of contents to articles (#5) #64

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": 2020,
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 @@ -63,6 +63,7 @@
@forward "styles/06-components/members-form";
@forward "styles/06-components/navigation";
@forward "styles/06-components/post-card";
@forward "styles/06-components/table-of-contents";
@forward "styles/06-components/text-field";

// 7. Editor
Expand Down
42 changes: 42 additions & 0 deletions assets/scss/styles/06-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 @@ -86,10 +86,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 @@ -179,6 +185,10 @@
{{/contentFor}}

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

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

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

Expand Down
Loading