From 60dea5602005ecc177e8dbee9c8ecb6b196ed2c6 Mon Sep 17 00:00:00 2001 From: "Nico S." <38787491+zeus-web@users.noreply.github.com> Date: Sun, 23 Feb 2025 20:45:58 +0100 Subject: [PATCH] added ToC min+maxLevel (#1258) Added option to ToC to define minimum and maximum displayed levels. --- plugs/index/toc.ts | 18 +++++++++++------- website/Table of Contents.md | 4 +++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/plugs/index/toc.ts b/plugs/index/toc.ts index 9b92c37b..82e970b3 100644 --- a/plugs/index/toc.ts +++ b/plugs/index/toc.ts @@ -16,6 +16,8 @@ type TocConfig = { maxHeaders?: number; header?: boolean; headerText?: string; + maxLevel?: number; + minLevel?: number; }; export async function widget( @@ -65,16 +67,18 @@ export async function widget( } // console.log("Headers", headers); // Adjust level down if only sub-headers are used - const minLevel = headers.reduce( + + let minLevel = headers.reduce( (min, header) => Math.min(min, header.level), 6, ); - const renderedMd = headerText + - headers.map((header) => - `${ - " ".repeat((header.level - minLevel) * 2) - }* [[${page}@${header.pos}|${header.name}]]` - ).join("\n"); + if (config.minLevel && config.minLevel > minLevel) { minLevel = config.minLevel ;} + let renderedMd = headerText + "\n"; + for (const header of headers) { + if (config.maxLevel && header.level > config.maxLevel || (config.minLevel && header.level < config.minLevel)) { continue; } + renderedMd = renderedMd + " ".repeat((header.level - minLevel) * 2) + "[[" + page + "@" + header.pos + "|" + header.name + "]]\n"; + } + // console.log("Markdown", renderedMd); return { markdown: renderedMd, diff --git a/website/Table of Contents.md b/website/Table of Contents.md index f9a88cc2..4061a8eb 100644 --- a/website/Table of Contents.md +++ b/website/Table of Contents.md @@ -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 * `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 +* `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: ```toc @@ -26,4 +28,4 @@ header: false 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. \ No newline at end of file +Want to add a table of contents to all your pages automatically? You can — that’s functionality available via the [[Library/Core]] library.