maxHeaders for toc
This commit is contained in:
parent
9a07c4c90a
commit
a115c71381
@ -2,8 +2,6 @@ import { editor, markdown, YAML } from "$sb/syscalls.ts";
|
|||||||
import { CodeWidgetContent } from "$sb/types.ts";
|
import { CodeWidgetContent } from "$sb/types.ts";
|
||||||
import { renderToText, traverseTree } from "$sb/lib/tree.ts";
|
import { renderToText, traverseTree } from "$sb/lib/tree.ts";
|
||||||
|
|
||||||
const defaultHeaderThreshold = 0;
|
|
||||||
|
|
||||||
type Header = {
|
type Header = {
|
||||||
name: string;
|
name: string;
|
||||||
pos: number;
|
pos: number;
|
||||||
@ -11,7 +9,10 @@ type Header = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
type TocConfig = {
|
type TocConfig = {
|
||||||
|
// Only show the TOC if there are at least this many headers
|
||||||
minHeaders?: number;
|
minHeaders?: number;
|
||||||
|
// Don't show the TOC if there are more than this many headers
|
||||||
|
maxHeaders?: number;
|
||||||
header?: boolean;
|
header?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -40,14 +41,14 @@ export async function widget(
|
|||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
|
||||||
let headerThreshold = defaultHeaderThreshold;
|
if (config.minHeaders && headers.length < config.minHeaders) {
|
||||||
if (config.minHeaders) {
|
|
||||||
headerThreshold = config.minHeaders;
|
|
||||||
}
|
|
||||||
if (headers.length < headerThreshold) {
|
|
||||||
// Not enough headers, not showing TOC
|
// Not enough headers, not showing TOC
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
if (config.maxHeaders && headers.length > config.maxHeaders) {
|
||||||
|
// Too many headers, not showing TOC
|
||||||
|
return null;
|
||||||
|
}
|
||||||
let headerText = "# Table of Contents\n";
|
let headerText = "# Table of Contents\n";
|
||||||
if (config.header === false) {
|
if (config.header === false) {
|
||||||
headerText = "";
|
headerText = "";
|
||||||
|
@ -21,7 +21,8 @@ federation:
|
|||||||
In the body of the `toc` code widget you can configure a few options:
|
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
|
* `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:
|
Example:
|
||||||
```toc
|
```toc
|
||||||
|
Loading…
Reference in New Issue
Block a user