Skip to content

Commit af56dc4

Browse files
author
Igor Alexey
committed
toc links
1 parent 4dcfb39 commit af56dc4

4 files changed

Lines changed: 78 additions & 5 deletions

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@ In order to configure TinyDocs you will need to upload the files to your web ser
3939
5. Add your Markdown files to the pages/ directory, organizing them into folders if you wish to create categories.
4040

4141
## Dependencies
42-
* [PHP](https://secure.php.net/) >= 5.3.0
42+
* [PHP](https://secure.php.net/) >= 5.3.0
43+
44+
## Contributions
45+
Any contributions are welcome!, if you have any questions or suggestions, open an issue.

index.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function create_page_contents()
4747
if ($GLOBALS['searchQuery']) {
4848
$startTime = microtime(true);
4949

50-
echo "<h1>Search results for " . $GLOBALS['searchQuery'] . "</h1>";
50+
echo "<h1>Search results for \"" . $GLOBALS['searchQuery'] . "\"</h1>";
5151

5252
// Get all the files and subdirectories in the content directory
5353
$files = new RecursiveIteratorIterator(
@@ -155,6 +155,7 @@ function formatFileSize($bytes) {
155155
<link rel="icon" type="image/x-icon" href="<?php echo $favicon; ?>">
156156
<link rel="stylesheet" href="styles/main.css">
157157
<link rel="stylesheet" href="styles/themes/<?php echo $defaultTheme; ?>">
158+
<script src="js/cms.js" defer></script>
158159
<meta name="keywords" content="<?php echo $description; ?>">
159160
<?php
160161
echo $additionalHead;
@@ -168,8 +169,19 @@ function formatFileSize($bytes) {
168169
<body>
169170
<!-- Left column -->
170171
<div>
171-
<h1><?php echo $title ?></h1>
172-
172+
<div class="navigation">
173+
<div class="blank">
174+
</div>
175+
</div>
176+
<table id="toc" style="<?php echo $GLOBALS['searchQuery'] ? "display:none" : "" ?>">
177+
<tbody>
178+
<tr>
179+
<td id="toc-content">
180+
<h2>Contents</h2>
181+
</td>
182+
</tr>
183+
</tbody>
184+
</table>
173185
</div>
174186
<!-- Middle column -->
175187
<div>

js/cms.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const toSnakeCase = str =>
2+
str &&
3+
str
4+
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
5+
.map(x => x.toLowerCase())
6+
.join('_');
7+
8+
const contentDiv = document.getElementsByClassName('content')[0];
9+
const headings = contentDiv.querySelectorAll('h1, h2, h3, h4, h5, h6');
10+
let tocHTML = '';
11+
let currentLevel = 0;
12+
headings.forEach((heading) => {
13+
const level = parseInt(heading.tagName[1], 10);
14+
// Adjust indentation levels based on the heading hierarchy
15+
if (level > currentLevel) {
16+
tocHTML += '<ul>';
17+
} else if (level < currentLevel) {
18+
tocHTML += '</ul>'.repeat(currentLevel - level) + '</li>';
19+
} else {
20+
tocHTML += '</li>';
21+
}
22+
tocHTML += `<li><a href="#${toSnakeCase(heading.innerText)}">${heading.innerText}</a>`;
23+
currentLevel = level;
24+
});
25+
tocHTML += '</li>'.repeat(currentLevel);
26+
const firstDiv = document.getElementById('toc-content');
27+
firstDiv.insertAdjacentHTML('beforeend', tocHTML);

styles/themes/Legacy.css

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ a:hover {
2323
text-decoration: underline;
2424
}
2525

26+
.breadcrumbs {
27+
margin-bottom: 0;
28+
}
29+
2630
.content, .navigation {
2731
display: flow-root;
2832
}
2933

30-
.content {
34+
.content, #toc {
3135
background-color: #fff;
3236
padding-left: 1em;
3337
padding-right: 1em;
@@ -37,6 +41,22 @@ a:hover {
3741
border-radius: 2px;
3842
}
3943

44+
#toc h2 {
45+
display: inline-block;
46+
width: 100%;
47+
border: none;
48+
padding: 0;
49+
margin: 0;
50+
font-size: 100%;
51+
font-weight: bold;
52+
text-align: center;
53+
}
54+
55+
ul {
56+
padding-left: 10px;
57+
margin: 0;
58+
}
59+
4060
.navigation .right {
4161
float:right;
4262
margin-bottom: 0.5em;
@@ -73,6 +93,17 @@ a:hover {
7393
background-color: #ccc;
7494
}
7595

96+
.navigation .blank {
97+
background-image: repeating-linear-gradient(45deg,
98+
#eee 10px,
99+
#eee 12px,
100+
transparent 12px,
101+
transparent 20px);
102+
padding: 1.22em;
103+
margin-bottom: 0.5em;
104+
border: 1px solid var(--border-color);
105+
}
106+
76107
/* Page Content */
77108

78109
blockquote {

0 commit comments

Comments
 (0)