maxHeaders for toc

pull/624/head
Zef Hemel 2024-01-09 14:02:08 +01:00
parent 9a07c4c90a
commit a115c71381
2 changed files with 10 additions and 8 deletions

View File

@ -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 = "";

View File

@ -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