Skip to content

Commit d15398f

Browse files
authored
Simple TOC level parameter (#9612)
* Add toc level parameter * Update TOC documentation * Add toc level release note * Update releasenote number and GH links * Update releasenote number and GH links
1 parent 6bc77cf commit d15398f

File tree

4 files changed

+86
-10
lines changed

4 files changed

+86
-10
lines changed

core/wiki/macros/toc.tid

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,45 @@ tags: $:/tags/Macro
1515
</span>
1616
\end
1717

18-
\define toc-body(tag,sort:"",itemClassFilter,exclude,path)
18+
\define toc-level-indicator()
19+
\whitespace trim
20+
<%if [<__level__>compare:number:gt[0]]%>
21+
<%if [<currentTiddler>tagging[]] %>
22+
<span class="tc-tiny-gap-left">{{$:/core/images/new-button}}</span>
23+
<%else%>
24+
<span class="tc-tiny-gap-left">{{$:/core/images/blank}}</span>
25+
<%endif%>
26+
<% endif %>
27+
\end
28+
29+
\define toc-body(tag,sort:"",itemClassFilter,exclude,path,level)
1930
\whitespace trim
2031
<ol class="tc-toc">
2132
<$list filter="""[all[shadows+tiddlers]tag<__tag__>!has[draft.of]$sort$] -[<__tag__>] -[subfilter<__exclude__>]""">
2233
<$let item=<<currentTiddler>> path={{{ [<__path__>addsuffix[/]addsuffix<__tag__>] }}}>
2334
<$set name="excluded" filter="[subfilter<__exclude__>] [<__tag__>]">
2435
<$set name="toc-item-class" filter=<<__itemClassFilter__>> emptyValue="toc-item-selected" value="toc-item">
2536
<li class=<<toc-item-class>>>
26-
<$list filter="[all[current]toc-link[no]]" emptyMessage="<$link to={{{ [<currentTiddler>get[target]else<currentTiddler>] }}}><<toc-caption>></$link>">
37+
<$list filter="[all[current]toc-link[no]]" >
38+
<$list-empty>
39+
<!-- link to target-field or currentTiddler -->
40+
<$link to={{{ [<currentTiddler>get[target]else<currentTiddler>] }}}>
41+
<<toc-level-indicator>>
42+
<<toc-caption>>
43+
</$link>
44+
</$list-empty>
45+
<!-- toc-link = no -->
46+
<<toc-level-indicator>>
2747
<<toc-caption>>
2848
</$list>
29-
<$macrocall $name="toc-body" tag=<<item>> sort=<<__sort__>> itemClassFilter=<<__itemClassFilter__>> exclude=<<excluded>> path=<<path>>/>
49+
<$let _level={{{ [<__level__>subtract[1]] }}}>
50+
<%if [<_level>compare:number:gt[0]]%>
51+
<$macrocall $name="toc-body" tag=<<item>> sort=<<__sort__>> itemClassFilter=<<__itemClassFilter__>> exclude=<<excluded>> path=<<path>> level=<<_level>>/>
52+
<%elseif [<_level>match[-1]]%>
53+
<!-- show full toc, no level defined -->
54+
<$macrocall $name="toc-body" tag=<<item>> sort=<<__sort__>> itemClassFilter=<<__itemClassFilter__>> exclude=<<excluded>> path=<<path>>/>
55+
<%endif%>
56+
</$let>
3057
</li>
3158
</$set>
3259
</$set>
@@ -35,10 +62,10 @@ tags: $:/tags/Macro
3562
</ol>
3663
\end
3764

38-
\define toc(tag,sort:"",itemClassFilter:"", exclude)
65+
\define toc(tag,sort:"",itemClassFilter:"",exclude,level)
3966
\whitespace trim
4067
<$let __tag__={{{ [<__tag__>is[blank]then<currentTiddler>else<__tag__>] }}} >
41-
<$macrocall $name="toc-body" tag=<<__tag__>> sort=<<__sort__>> itemClassFilter=<<__itemClassFilter__>> exclude=<<__exclude__>>/>
68+
<$macrocall $name="toc-body" tag=<<__tag__>> sort=<<__sort__>> itemClassFilter=<<__itemClassFilter__>> exclude=<<__exclude__>> level=<<__level__>>/>
4269
</$let>
4370
\end
4471

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,43 @@
1-
created: 20150221201838000
2-
modified: 20150221203742000
3-
title: Example Table of Contents: Simple
41
caption: Simple
5-
tags: table-of-contents-example
2+
created: 20150221201838000
3+
modified: 20260124125915331
64
order: 1
5+
tags: table-of-contents-example
6+
title: Example Table of Contents: Simple
7+
type: text/vnd.tiddlywiki
78

89
<$macrocall $name=".example" n="1"
910
eg="""<div class="tc-table-of-contents">
1011
<<toc "Contents">>
1112
</div>"""/>
13+
14+
---
15+
16+
<<.tip"""If you use several parameters in a macro call, it is advised to use ''named parameters'' for all of them""">>
17+
18+
<$macrocall $name=".example" n="2"
19+
eg="""<div class="tc-table-of-contents">
20+
<<toc tag:"Contents" level:"1">>
21+
</div>"""/>
22+
23+
<$macrocall $name=".example" n="3"
24+
eg="""<div class="tc-table-of-contents">
25+
<<toc tag:"Contents" level:"2">>
26+
</div>"""/>
27+
28+
<$macrocall $name=".example" n="4"
29+
eg="""<div class="tc-table-of-contents">
30+
<<toc tag:"Contents" level:"4">>
31+
</div>"""/>
32+
33+
* If you want to ''change'' or ''remove'' the indicator, you can locally overwrite the `toc-level-indicator` macro.
34+
* It is globally defined at: $:/core/macros/toc tiddler. Eg:
35+
36+
<$macrocall $name=".example" n="5"
37+
eg="""\define toc-level-indicator()
38+
<!-- remove the indicator -->
39+
\end
40+
41+
<div class="tc-table-of-contents">
42+
<<toc tag:"Contents" level:"1">>
43+
</div>"""/>

editions/tw5.com/tiddlers/macros/TableOfContentsMacro.tid

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
created: 20140919155729620
2-
modified: 20240624102502089
2+
modified: 20260124130054271
33
tags: Macros [[Core Macros]]
44
title: Table-of-Contents Macros
55
type: text/vnd.tiddlywiki
@@ -74,6 +74,9 @@ These two parameters are combined into a single [[filter expression|Filter Expre
7474
; exclude <<.from-version "5.3.0">>
7575
: This optional parameter can be used to exclude tiddlers from the TOC list. It allows a [[Title List]] or a <<.olink subfilter>>. Eg: `exclude:"HelloThere [[Title with spaces]]"` or `exclude:"[has[excludeTOC]]"`. Where the former will exclude two tiddlers and the later would exclude every tiddler that has a field <<.field excludeTOC>> independent of its value.<br>''Be aware'' that eg: `[prefix[H]]` is a shortcut for `[all[tiddlers]prefix[H]]`, which can have a performance impact, if used carelessly. So use $:/AdvancedSearch -> ''Filters'' tab to test the <<.param exclude>> parameter
7676

77+
; level <<.from-version "5.4.0">>
78+
: This optional parameter can be used to define how many toc levels are shown by the toc-macro. By default all levels are shown.
79+
7780
!! Custom Icons
7881

7982
<<.from-version "5.2.4">>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
change-category: usability
2+
change-type: enhancement
3+
created: 20260124121646761
4+
description: The simple toc macro now supports a level parameter
5+
github-contributors: pmario
6+
github-links: https://github.com/TiddlyWiki/TiddlyWiki5/pull/9612
7+
modified: 20260124131646761
8+
release: 5.4.0
9+
tags: $:/tags/ChangeNote
10+
title: $:/changenotes/5.4.0/#9612
11+
type: text/vnd.tiddlywiki
12+
13+
* The [[toc-macro|Table-of-Contents Macros]] now supports a ''level'' parameter, that allows us to limit the [[toc-levels|Table-of-Contents Macros (Examples)]], that are listed.
14+
* If the level parameter is active, levels, which have children will show a level indicator at the front. By default it uses the core "+" icon

0 commit comments

Comments
 (0)