-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoc.ts
42 lines (33 loc) · 869 Bytes
/
toc.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// deno-lint-ignore no-explicit-any
export function renderTOC(toc: string | any[]) {
if (!toc || toc.length === 0) {
return "";
}
let tocHtml = '<ol class="toc">';
for (const item of toc) {
tocHtml += `<li><a href="#${item.slug}">${item.text}</a>`;
if (item.children && item.children.length > 0) {
tocHtml += "<ul>";
for (const child of item.children) {
tocHtml += `<li><a href="#${child.slug}">${child.text}</a></li>`;
}
tocHtml += "</ul>";
}
tocHtml += "</li>";
}
tocHtml += "</ol>";
return tocHtml;
}
/*
WIP
<!-- Add TOC -->
<!--
{% if page.data.toc.length > 1 %}
<div class="toc_container">
<a id="top_link" href="javascript: void(0);">To the top</a>
<h6>TABLE OF CONTENT</h6>
<!-- TOC -->
</div>
{% endif %}
-->
*/