File tree Expand file tree Collapse file tree 2 files changed +14
-8
lines changed
Expand file tree Collapse file tree 2 files changed +14
-8
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,8 @@ type TocConfig = {
1616 maxHeaders ?: number ;
1717 header ?: boolean ;
1818 headerText ?: string ;
19+ maxLevel ?: number ;
20+ minLevel ?: number ;
1921} ;
2022
2123export async function widget (
@@ -65,16 +67,18 @@ export async function widget(
6567 }
6668 // console.log("Headers", headers);
6769 // Adjust level down if only sub-headers are used
68- const minLevel = headers . reduce (
70+
71+ let minLevel = headers . reduce (
6972 ( min , header ) => Math . min ( min , header . level ) ,
7073 6 ,
7174 ) ;
72- const renderedMd = headerText +
73- headers . map ( ( header ) =>
74- `${
75- " " . repeat ( ( header . level - minLevel ) * 2 )
76- } * [[${ page } @${ header . pos } |${ header . name } ]]`
77- ) . join ( "\n" ) ;
75+ if ( config . minLevel && config . minLevel > minLevel ) { minLevel = config . minLevel ; }
76+ let renderedMd = headerText + "\n" ;
77+ for ( const header of headers ) {
78+ if ( config . maxLevel && header . level > config . maxLevel || ( config . minLevel && header . level < config . minLevel ) ) { continue ; }
79+ renderedMd = renderedMd + " " . repeat ( ( header . level - minLevel ) * 2 ) + "[[" + page + "@" + header . pos + "|" + header . name + "]]\n" ;
80+ }
81+
7882 // console.log("Markdown", renderedMd);
7983 return {
8084 markdown : renderedMd ,
Original file line number Diff line number Diff line change @@ -19,11 +19,13 @@ In the body of the `toc` code widget you can configure a few options:
1919* ` headerText ` : by default "# Table of Contents\n". Change it to change the rendering of this header
2020* ` minHeaders ` : only renders a ToC if the number of headers in the current page exceeds this number, otherwise renders an empty widget
2121* ` maxHeaders ` : only renders a ToC if the number of headers in the current page is below this number, otherwise renders an empty widget
22+ * ` minLevel ` : only renders a ToC for headers below that level (if you do not want to display e.g. the first header)
23+ * ` maxLevel ` : only renders a ToC down to this level (e.g. if you have too many sublevels.)
2224
2325Example:
2426``` toc
2527header: false
2628minHeaders: 1
2729```
2830
29- Want to add a table of contents to all your pages automatically? You can — that’s functionality available via the [[ Library/Core]] library.
31+ Want to add a table of contents to all your pages automatically? You can — that’s functionality available via the [[ Library/Core]] library.
You can’t perform that action at this time.
0 commit comments