Generate nested table of contents

This commit is contained in:
thiloho
2024-08-25 14:35:57 +02:00
parent 329927f126
commit 1c1aaf28e6
2 changed files with 34 additions and 16 deletions

View File

@@ -164,9 +164,6 @@ in
alias = "/var/www/archtika-websites/previews/"; alias = "/var/www/archtika-websites/previews/";
index = "index.html"; index = "index.html";
tryFiles = "$uri $uri/ $uri/index.html =404"; tryFiles = "$uri $uri/ $uri/index.html =404";
extraConfig = ''
autoindex on;
'';
}; };
"/api/" = { "/api/" = {
proxyPass = "http://localhost:${toString cfg.port}/"; proxyPass = "http://localhost:${toString cfg.port}/";

View File

@@ -94,20 +94,41 @@ const createMarkdownParser = (showToc = true) => {
postprocess(html: string) { postprocess(html: string) {
const closingRemainingSection = "</section>".repeat(sectionStack.length); const closingRemainingSection = "</section>".repeat(sectionStack.length);
const tableOfContents = let tableOfContents = "";
showToc && headings.length > 0 if (showToc && headings.length > 0) {
? `<details class="table-of-contents"> const tocItems = [];
let currentLevel = 0;
for (const { id, text, level } of headings) {
while (currentLevel < level - 1) {
tocItems.push("<ul>");
currentLevel++;
}
while (currentLevel > level - 1) {
tocItems.push("</ul>");
currentLevel--;
}
tocItems.push(`<li><a href="#${id}">${text}</a>`);
if (level > currentLevel) {
tocItems.push("<ul>");
currentLevel = level;
} else {
tocItems.push("</li>");
}
}
while (currentLevel > 0) {
tocItems.push("</ul></li>");
currentLevel--;
}
tableOfContents = `
<details class="table-of-contents">
<summary>Table of contents</summary> <summary>Table of contents</summary>
<ul> ${tocItems.join("")}
${headings </details>
.map( `;
({ id, text, level }) => ` }
<li><a href="#${id}" class="h${level}">${text}</a></li>`
)
.join("")}
</ul>
</details>`
: "";
return ` return `
${tableOfContents} ${tableOfContents}