Auto down-adjust sub-headers in TOC

This commit is contained in:
Zef Hemel
2024-01-02 11:40:59 +01:00
parent ba90397c02
commit 11693fbd00
2 changed files with 10 additions and 5 deletions
+6 -1
View File
@@ -52,10 +52,15 @@ export async function renderTOC(): Promise<CodeWidgetContent | null> {
return null;
}
// console.log("Headers", headers);
// Adjust level down if only sub-headers are used
const minLevel = headers.reduce(
(min, header) => Math.min(min, header.level),
6,
);
const renderedMd = "# Table of Contents\n" +
headers.map((header) =>
`${
" ".repeat((header.level - 1) * 2)
" ".repeat((header.level - minLevel) * 2)
}* [[${page}@${header.pos}|${header.name}]]`
).join("\n");
// console.log("Markdown", renderedMd);