diff --git a/plugs/index/toc.ts b/plugs/index/toc.ts index 2007719..8dc7d8c 100644 --- a/plugs/index/toc.ts +++ b/plugs/index/toc.ts @@ -2,8 +2,6 @@ import { editor, markdown, YAML } from "$sb/syscalls.ts"; import { CodeWidgetContent } from "$sb/types.ts"; import { renderToText, traverseTree } from "$sb/lib/tree.ts"; -const defaultHeaderThreshold = 0; - type Header = { name: string; pos: number; @@ -11,7 +9,10 @@ type Header = { }; type TocConfig = { + // Only show the TOC if there are at least this many headers minHeaders?: number; + // Don't show the TOC if there are more than this many headers + maxHeaders?: number; header?: boolean; }; @@ -40,14 +41,14 @@ export async function widget( return false; }); - let headerThreshold = defaultHeaderThreshold; - if (config.minHeaders) { - headerThreshold = config.minHeaders; - } - if (headers.length < headerThreshold) { + if (config.minHeaders && headers.length < config.minHeaders) { // Not enough headers, not showing TOC return null; } + if (config.maxHeaders && headers.length > config.maxHeaders) { + // Too many headers, not showing TOC + return null; + } let headerText = "# Table of Contents\n"; if (config.header === false) { headerText = ""; diff --git a/website/Table of Contents.md b/website/Table of Contents.md index f5196ec..3a4c039 100644 --- a/website/Table of Contents.md +++ b/website/Table of Contents.md @@ -21,7 +21,8 @@ federation: In the body of the `toc` code widget you can configure a few options: * `header`: by default a “Table of Contents” header is added to the ToC, set this to `false` to disable rendering this header -* `minHeaders`: only renders a ToC if the number of headers in the current page exceeds this number, otherwise render 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 Example: ```toc