1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
2// See the LICENCE file in the repository root for full licence text.
3
4import 'jquery.tocify';
5
6const selectors = 'h1,h2';
7
8export default class Tocify {
9 constructor() {
10 this.setHeaderIds();
11
12 $('#toc').tocify({
13 extendPage: false,
14 hashGenerator(_text: unknown, $element: JQuery<Element>) {
15 return $element.attr('id');
16 },
17 hideEffectSpeed: 180,
18 highlightOffset: 60,
19 ignoreSelector: '.toc-ignore',
20 scrollHistory: true,
21 scrollTo: -1,
22 selectors,
23 showEffectSpeed: 0,
24 smoothScroll: false,
25 theme: 'none',
26 });
27 }
28
29 private setHeaderIds() {
30 for (const header of document.querySelectorAll(selectors)) {
31 if (header instanceof HTMLElement) {
32 header.id = header
33 .innerText
34 .toLowerCase()
35 .replace(/\s+/g, '-')
36 .replace(/[^\w-]+/g, '')
37 .replace(/--+/g, '-')
38 .replace(/^-+/, '')
39 .replace(/-+$/, '');
40 }
41 }
42 }
43}