Skip to content

Commit 60dea56

Browse files
authored
added ToC min+maxLevel (#1258)
Added option to ToC to define minimum and maximum displayed levels.
1 parent 58556f8 commit 60dea56

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

plugs/index/toc.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ type TocConfig = {
1616
maxHeaders?: number;
1717
header?: boolean;
1818
headerText?: string;
19+
maxLevel?: number;
20+
minLevel?: number;
1921
};
2022

2123
export async function widget(
@@ -65,16 +67,18 @@ export async function widget(
6567
}
6668
// console.log("Headers", headers);
6769
// Adjust level down if only sub-headers are used
68-
const minLevel = headers.reduce(
70+
71+
let minLevel = headers.reduce(
6972
(min, header) => Math.min(min, header.level),
7073
6,
7174
);
72-
const renderedMd = headerText +
73-
headers.map((header) =>
74-
`${
75-
" ".repeat((header.level - minLevel) * 2)
76-
}* [[${page}@${header.pos}|${header.name}]]`
77-
).join("\n");
75+
if (config.minLevel && config.minLevel > minLevel) { minLevel = config.minLevel ;}
76+
let renderedMd = headerText + "\n";
77+
for (const header of headers) {
78+
if (config.maxLevel && header.level > config.maxLevel || (config.minLevel && header.level < config.minLevel)) { continue; }
79+
renderedMd = renderedMd + " ".repeat((header.level - minLevel) * 2) + "[[" + page + "@" + header.pos + "|" + header.name + "]]\n";
80+
}
81+
7882
// console.log("Markdown", renderedMd);
7983
return {
8084
markdown: renderedMd,

website/Table of Contents.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ In the body of the `toc` code widget you can configure a few options:
1919
* `headerText`: by default "# Table of Contents\n". Change it to change the rendering of this header
2020
* `minHeaders`: only renders a ToC if the number of headers in the current page exceeds this number, otherwise renders an empty widget
2121
* `maxHeaders`: only renders a ToC if the number of headers in the current page is below this number, otherwise renders an empty widget
22+
* `minLevel`: only renders a ToC for headers below that level (if you do not want to display e.g. the first header)
23+
* `maxLevel`: only renders a ToC down to this level (e.g. if you have too many sublevels.)
2224

2325
Example:
2426
```toc
2527
header: false
2628
minHeaders: 1
2729
```
2830

29-
Want to add a table of contents to all your pages automatically? You can — that’s functionality available via the [[Library/Core]] library.
31+
Want to add a table of contents to all your pages automatically? You can — that’s functionality available via the [[Library/Core]] library.

0 commit comments

Comments
 (0)