added ToC min+maxLevel (#1258)
Added option to ToC to define minimum and maximum displayed levels.pull/1263/head
parent
58556f8e6d
commit
60dea56020
|
@ -16,6 +16,8 @@ type TocConfig = {
|
||||||
maxHeaders?: number;
|
maxHeaders?: number;
|
||||||
header?: boolean;
|
header?: boolean;
|
||||||
headerText?: string;
|
headerText?: string;
|
||||||
|
maxLevel?: number;
|
||||||
|
minLevel?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function widget(
|
export async function widget(
|
||||||
|
@ -65,16 +67,18 @@ export async function widget(
|
||||||
}
|
}
|
||||||
// console.log("Headers", headers);
|
// console.log("Headers", headers);
|
||||||
// Adjust level down if only sub-headers are used
|
// Adjust level down if only sub-headers are used
|
||||||
const minLevel = headers.reduce(
|
|
||||||
|
let minLevel = headers.reduce(
|
||||||
(min, header) => Math.min(min, header.level),
|
(min, header) => Math.min(min, header.level),
|
||||||
6,
|
6,
|
||||||
);
|
);
|
||||||
const renderedMd = headerText +
|
if (config.minLevel && config.minLevel > minLevel) { minLevel = config.minLevel ;}
|
||||||
headers.map((header) =>
|
let renderedMd = headerText + "\n";
|
||||||
`${
|
for (const header of headers) {
|
||||||
" ".repeat((header.level - minLevel) * 2)
|
if (config.maxLevel && header.level > config.maxLevel || (config.minLevel && header.level < config.minLevel)) { continue; }
|
||||||
}* [[${page}@${header.pos}|${header.name}]]`
|
renderedMd = renderedMd + " ".repeat((header.level - minLevel) * 2) + "[[" + page + "@" + header.pos + "|" + header.name + "]]\n";
|
||||||
).join("\n");
|
}
|
||||||
|
|
||||||
// console.log("Markdown", renderedMd);
|
// console.log("Markdown", renderedMd);
|
||||||
return {
|
return {
|
||||||
markdown: renderedMd,
|
markdown: renderedMd,
|
||||||
|
|
|
@ -19,6 +19,8 @@ In the body of the `toc` code widget you can configure a few options:
|
||||||
* `headerText`: by default "# Table of Contents\n". Change it to change the rendering of this header
|
* `headerText`: by default "# Table of Contents\n". Change it to change the rendering of this header
|
||||||
* `minHeaders`: only renders a ToC if the number of headers in the current page exceeds this number, otherwise renders an empty widget
|
* `minHeaders`: only renders a ToC if the number of headers in the current page exceeds this number, otherwise renders an empty widget
|
||||||
* `maxHeaders`: only renders a ToC if the number of headers in the current page is below this number, otherwise renders an empty widget
|
* `maxHeaders`: only renders a ToC if the number of headers in the current page is below this number, otherwise renders an empty widget
|
||||||
|
* `minLevel`: only renders a ToC for headers below that level (if you do not want to display e.g. the first header)
|
||||||
|
* `maxLevel`: only renders a ToC down to this level (e.g. if you have too many sublevels.)
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
```toc
|
```toc
|
||||||
|
@ -26,4 +28,4 @@ header: false
|
||||||
minHeaders: 1
|
minHeaders: 1
|
||||||
```
|
```
|
||||||
|
|
||||||
Want to add a table of contents to all your pages automatically? You can — that’s functionality available via the [[Library/Core]] library.
|
Want to add a table of contents to all your pages automatically? You can — that’s functionality available via the [[Library/Core]] library.
|
||||||
|
|
Loading…
Reference in New Issue