1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-01-09 17:00:27 +00:00
TiddlyWiki5/core/wiki/macros/toc.tid

423 lines
18 KiB
Plaintext
Raw Normal View History

title: $:/core/macros/toc
tags: $:/tags/Macro
2024-02-07 13:32:22 +00:00
<!--
Naming rules
- functions eg: tf.toc-variableName, "tf." ... function prefix reservered for TW, "toc-" ... global macro prefix, "variableName"
2024-02-07 13:32:22 +00:00
- procedures eg: toc-procedureName,
- macro parameters: path, itemTemplate
- derived macro parameters: _path, _itemTemplate ... derived variables start with an underscore
- some \function definitions are near their first use in the middle of the code, to find them easily
2024-02-09 11:23:24 +00:00
The toc-macro code is heavily reused from top to bottom.
toc-tabbed-internal-nav calls toc-tabbed-external-nav, which calls toc-selective-expandable, which calls toc-expandable
2024-02-09 11:23:24 +00:00
The very first toc macro is _not_ reused. So it can be used as a simple reference implementation, because it is relatively easy to debug.
2024-02-07 13:32:22 +00:00
-->
\whitespace trim
\procedure toc-defaultItemTemplate()
<span class="tc-toc-caption tc-tiny-gap-left" title={{!!tooltip}}>
2024-02-06 17:39:46 +00:00
<$let tv-wikilinks="no">
<$transclude $tiddler={{{ [<itemTemplate>!is[blank]then<itemTemplate>else<currentTiddler>] }}}
$field={{{ [<itemTemplate>!is[blank]then[text]] :else[<captionField>!is[blank]then<captionField>else[caption]] }}}
>
<$view field="title"/>
</$transclude>
</$let>
</span>
\end
2024-02-07 13:32:22 +00:00
<!-- returns the title of a tiddler -->
\function tf.toc-getItemTemplate() [<itemTemplate>!is[blank]then<itemTemplate>] :else[<currentTiddler>get[toc-itemTemplate]]
2024-02-07 13:32:22 +00:00
<!-- Use macro itemTemplate or one defined as a toc-itemTemplate field -->
\procedure _itemTemplate()
<$transclude $tiddler=<<tf.toc-getItemTemplate>> >
<<toc-defaultItemTemplate>>
</$transclude>
\end
<!-- Next 2 allow custom icons (TW v5.2.4). Keep names for backwards compatibility -->
\procedure toc-open-icon() $:/core/images/down-arrow
\procedure toc-closed-icon() $:/core/images/right-arrow
<!-- Default field names -->
\procedure toc-hideField() toc-hide
\procedure toc-includeField() toc-include
\procedure toc-filterField() toc-filter
2024-02-05 20:14:44 +00:00
<!-- helper functions for li class definitions -->
\function tf.toc-itemClassFilter() [<selectedTiddler>get[text]match<currentTiddler>then[toc-item-selected]else[toc-item]]
\function tf.toc-includeClass() [subfilter<tf.toc-include>match<currentTiddler>then[toc-item-include]]
\function tf.toc-filterClass() [subfilter<tf.toc-filter>match<currentTiddler>then[toc-item-filter]]
\function tf.toc-hideClass() [<tf.toc-hide>!is[blank]then[toc-item-hide]]
<!-- join the CSS definitions from above -->
\function tf.toc-itemClass() [<tf.toc-itemClassFilter>] [<tf.toc-includeClass>] [<tf.toc-filterClass>] [<tf.toc-hideClass>] +[join[ ]]
<!-- Special case: if toc-hide field is "yes", we need a currentTiddler *titlelist* - otherwise return the content as text -->
<!-- the following functions need to use <currentTiddler> -->
\function tf.toc-getHideText() [<currentTiddler>get<toc-hideField>]
\function tf.toc-hideIsYes() [<currentTiddler>format:titlelist[]]
\function tf.toc-hide() [<tf.toc-getHideText>match[yes]then<tf.toc-hideIsYes>] :else[<tf.toc-getHideText>]
2024-02-07 13:32:22 +00:00
<!-- Read tf.toc-include and tf.toc-filter from fields. Need to use <tag> -->
\function tf.toc-include() [<tag>get<toc-includeField>]
\function tf.toc-filter() [<tag>get<toc-filterField>]
2024-02-09 11:23:24 +00:00
<!-- Define default filter strings for different usecases: tag (default), parentField or tagField for using a tag-like field -->
\procedure toc-filterDefault() [all[shadows+tiddlers]tag<tag>!has[draft.of]]
\procedure toc-filterParent() [has<parentField>!has[draft.of]] :filter[get<parentField>match<tag>]
\procedure toc-filterTagLike() [<tag>get<tagField>enlist-input[]!has[draft.of]]
2024-02-09 11:23:24 +00:00
<!-- TODO tiddlers with parentField MAY need caching -->
<!-- activate the filterString depending on tagField, parentField or default. tagField takes precedence -->
\function tf.toc-filterString() [<tagField>!is[blank]then<toc-filterTagLike>] :else[<parentField>!is[blank]then<toc-filterParent>else<toc-filterDefault>]
<!-- define unique path based state title -->
\function tf.toc-newPath() [<path>addsuffix[/]addsuffix<tag>]
2024-02-09 11:23:24 +00:00
<!-- ====================================================================================
Simple toc macro which shows the full tree
This macro is _not_ used by other toc-macros
v5.3.4 itemClassFilter has been replaced by tf.toc-itemClassFilter see: tf.toc-itemClass()
========================================================================================== -->
\procedure toc(tag,sort:"",itemClassFilter,exclude,captionField,tagField,parentField,itemTemplate)
<$macrocall $name="toc-body" tag=<<tag>> sort=<<sort>> exclude=<<exclude>>/>
\end
2024-02-07 13:32:22 +00:00
<!-- simple toc helper body -->
\procedure toc-body(tag,sort:"",itemClassFilter,exclude,path)
<!-- Be aware: if parentField is set, the "tag" parameter contains the currentTiddler -->
<$set name=currentTiddler filter="[<parentField>!is[blank]then<tag>else<currentTiddler>]">
2014-08-31 13:05:43 +00:00
<ol class="tc-toc">
2024-02-09 11:23:24 +00:00
<!-- text substitution for $(sort)$ parameter backwards compatibility -->
2024-02-05 19:07:10 +00:00
<$list filter=` [subfilter<tf.toc-filter>]
:else[subfilter<tf.toc-filterString>$(sort)$]
[subfilter<tf.toc-include>] -[<tag>] -[subfilter<exclude>]`>
<%if [<currentTiddler>] -[subfilter<tf.toc-hide>] %>
2024-02-07 13:32:22 +00:00
<$let _path=<<tf.toc-newPath>> >
<!-- adding tf.toc-hide, excludes blocked elements from all visible sub-nodes -->
<$set name="_excluded" filter="[subfilter<exclude>] [<tag>] [subfilter<tf.toc-hide>]">
<li class=<<tf.toc-itemClass>>>
<% if [all[current]toc-link[no]] %>
<<_itemTemplate>>
<% else %>
<!-- v5.1.23 use target-field if present in tiddler -->
<$link to={{{ [<currentTiddler>get[target]else<currentTiddler>] }}}>
<<_itemTemplate>>
</$link>
<% endif %>
<!-- toc-body only calls itself, so only modified variables have to be passed on. -->
2024-02-07 13:32:22 +00:00
<!-- The rest can be reused from toc parameters -->
<$macrocall $name="toc-body" tag=<<currentTiddler>> exclude=<<_excluded>> path=<<_path>>
/>
</li>
</$set>
</$let>
<% endif %>
</$list>
</ol>
</$set>
\end
<!-- -->
<!-- <$log tag=<<tag>> parentField=<<parentField>> toc-filter=<<toc-filter>> ct=<<currentTiddler>> captionField=<<captionField>>/> -->
<!-- -->
2024-02-09 11:23:24 +00:00
<!-- Helper functions for toc-openBranch and toc-closeBranch procedures -->
\procedure toc-tagLikeChildFilter() [<currentTiddler>get<tagField>enlist-input[]]
\procedure toc-parentChildFilter() [has<parentField>] :filter[get<parentField>match<..currentTiddler>]
\procedure toc-tagChildFilter() [tag<currentTiddler>]
<!-- this function uses the variables NOT the tiddler fields -->
\function tf.toc-hasChildren()
[<tagField>!is[blank]then<toc-tagLikeChildFilter>]
:else[<parentField>!is[blank]then<toc-parentChildFilter>else<toc-tagChildFilter>]
\end
\function tf.toc-stateTitle() [[$:/state/toc]] [<path>] "/" [<currentTiddler>] +[join[]]
2024-02-09 11:23:24 +00:00
<!-- toc-openBranch internally uses a recursiveOpen() procedure -->
\procedure toc-openBranch(path,currentTiddler,manual:"no")
\procedure recursiveOpen(exclude, path)
<!-- tf-toc-stateTitle needs the path variable! -->
<$let path={{{ [<path>] "/" [<currentTiddler>] +[join[]] }}}>
<!-- It's common that users tag the <currentTiddler> with itself, so avoid redundant state tiddlers with -[<currentTiddler>] -->
<$list filter="[subfilter<tf.toc-hasChildren>] [subfilter<tf.toc-curTidInclude>] -[<currentTiddler>] -[subfilter<exclude>]" >
<% if [subfilter<tf.toc-hasChildren>] [subfilter<tf.toc-curTidInclude>] -[<currentTiddler>] -[subfilter<exclude>] %>
<$set name="_excluded" filter="[subfilter<exclude>] [<currentTiddler>]">
<!-- tf-toc-stateTitle needs the path variable! -->
<$qualify name="toc-state" title=<<tf.toc-stateTitle>> >
<$action-setfield $tiddler=<<toc-state>> text="open"/>
<$macrocall $name="recursiveOpen" exclude=<<_excluded>> path=<<path>> />
</$qualify>
</$set>
<% endif %>
</$list>
</$let>
\end recursiveOpen
2024-02-09 11:23:24 +00:00
<% if [<manual>match[yes]] %>
<$qualify name="toc-state" title=<<tf.toc-stateTitle>> >
<$action-setfield $tiddler=<<toc-state>> text="open"/>
<$macrocall $name="recursiveOpen" exclude=<<exclude>> path=<<path>> />
</$qualify>
<% elseif [<modifier>match[ctrl]] %>
<$macrocall $name="recursiveOpen" exclude=<<exclude>> path=<<path>> />
<% endif %>
\end
<!-- <$action-log/> -->
<!-- <$action-log ct=<<currentTiddler>> hasChildren=<<tf.toc-hasChildren>> path=<<path>> exclude=<<exclude>>/> -->
<!-- <$action-log toc-state=<<toc-state>> path=<<path>> exclude=<<exclude>> ct=<<currentTiddler>> /> -->
<!-- <$action-log ct=<<currentTiddler>> prefix=<<prefix>> toc-state=<<toc-state>>/> -->
\procedure toc-closeBranch(path,currentTiddler,manual:"no")
<% if [<modifier>match[ctrl]] :else[<manual>match[yes]] %>
<$let prefix={{{ [<tf.toc-stateTitle>] }}} >
<$action-deletetiddler $filter="[prefix<prefix>]"/>
</$let>
<% endif %>
\end
<!--
2024-02-07 13:32:22 +00:00
If a toc-item shows a link. Clicking the caption will open the tiddler
If toc-item is linked, the _itemTemplate is managed in the parent procedure
If a toc-item should not show a link, the _itemTemplate is part of the button. So it can be clicked to open / close one branch
The buttons also call toc-stateOpen and toc-stateClose, which by default allow us to CTRL-click to expand / fold the whole branch
-->
\procedure toc-item(isUnlinked:"")
<% if [<toc-state>get[text]else[close]match[close]] %>
<$button actions=<<toc-openBranch>> setTitle=<<toc-state>> setTo="open" class="tc-btn-invisible" tooltip=<<toc-state>>>
<$transclude tiddler=<<toc-closed-icon>> />
<% if [<isUnlinked>match[yes]] %>
<<_itemTemplate>>
<% endif %>
</$button>
<% elseif [<toc-state>get[text]match[open]] %>
<$button actions=<<toc-closeBranch>> setTitle=<<toc-state>> setTo="close" class="tc-btn-invisible" tooltip=<<toc-state>>>
<$transclude tiddler=<<toc-open-icon>> />
<% if [<isUnlinked>match[yes]] %>
<<_itemTemplate>>
<% endif %>
</$button>
<% endif %>
\end
2024-02-07 12:17:38 +00:00
\procedure toc-linked-expandable-body(tag,sort:"",itemClassFilter,exclude,path,captionField,tagField,parentField,itemTemplate)
<$qualify name="toc-state" title=<<tf.toc-stateTitle>> >
<li class=<<tf.toc-itemClass>>>
<!-- v5.1.23 use target-field if present in tiddler -->
<$link to={{{ [<currentTiddler>get[target]else<currentTiddler>] }}}>
<<toc-item>>
<<_itemTemplate>>
</$link>
<!-- TODO auto expand -->
<% if [<toc-state>get[text]match[open]] %>
<$macrocall $name="toc-expandable"
tag=<<currentTiddler>> sort=<<sort>> exclude=<<exclude>> path=<<path>>
2024-02-07 12:17:38 +00:00
captionField=<<captionField>> tagField=<<tagField>> parentField=<<parentField>>
itemTemplate=<<itemTemplate>>
/>
<% endif %>
</li>
</$qualify>
\end
2024-02-07 12:17:38 +00:00
\procedure toc-unlinked-expandable-body(tag,sort:"",itemClassFilter,exclude,path,captionField,tagField,parentField,itemTemplate)
<$qualify name="toc-state" title=<<tf.toc-stateTitle>>>
<li class=<<tf.toc-itemClass>>>
<<toc-item isUnlinked:"yes">>
<!-- TODO auto expand -->
<% if [<toc-state>get[text]match[open]] %>
<$macrocall $name="toc-expandable"
tag=<<currentTiddler>> sort=<<sort>> exclude=<<exclude>> path=<<path>>
2024-02-07 12:17:38 +00:00
captionField=<<captionField>> tagField=<<tagField>> parentField=<<parentField>>
itemTemplate=<<itemTemplate>>
/>
<% endif %>
</li>
</$qualify>
\end
<!-- ==========================================================
Shows an expandable toc. Item always have an open/close chevron
=============================================================== -->
2024-02-07 12:17:38 +00:00
\procedure toc-expandable(tag,sort:"",itemClassFilter:"",exclude,path,captionField,tagField,parentField,itemTemplate)
<$set name=currentTiddler filter="[<parentField>!is[blank]then<tag>else<currentTiddler>]">
<ol class="tc-toc toc-expandable">
<!-- text substitution is needed for backwards compatibility -->
2024-02-05 19:07:10 +00:00
<$list filter=` [subfilter<tf.toc-filter>]
:else[subfilter<tf.toc-filterString>$(sort)$]
[subfilter<tf.toc-include>] -[<tag>] -[subfilter<exclude>]`>
<%if [<currentTiddler>] -[subfilter<tf.toc-hide>] %>
<$let _path=<<tf.toc-newPath>>>
<$set name="_excluded" filter="[subfilter<exclude>] [<tag>] [subfilter<tf.toc-hide>]">
<% if [all[current]toc-link[no]] %>
<$macrocall $name="toc-unlinked-expandable-body"
tag=<<tag>> sort=<<sort>> exclude=<<_excluded>> path=<<_path>>
2024-02-07 12:17:38 +00:00
captionField=<<captionField>> tagField=<<tagField>> parentField=<<parentField>>
itemTemplate=<<itemTemplate>>
/>
<% else %>
<$macrocall $name="toc-linked-expandable-body"
tag=<<tag>> sort=<<sort>> exclude=<<_excluded>> path=<<_path>>
2024-02-07 12:17:38 +00:00
captionField=<<captionField>> tagField=<<tagField>> parentField=<<parentField>>
itemTemplate=<<itemTemplate>>
/>
<% endif %>
</$set>
</$let>
<% endif %>
</$list>
</ol>
</$set>
\end
\function tf.toc-curTidInclude() [<currentTiddler>get<toc-includeField>] [<currentTiddler>get<toc-filterField>]
2024-02-07 12:17:38 +00:00
\procedure toc-linked-selective-expandable-body(tag,sort:"",itemClassFilter,exclude,path,captionField,tagField,parentField,itemTemplate)
<$qualify name="toc-state" title=<<tf.toc-stateTitle>>>
<li class=<<tf.toc-itemClass>>>
<!-- v5.1.23 use target-field if present in tiddler -->
<$link to={{{ [<currentTiddler>get[target]else<currentTiddler>] }}}>
<!-- The subfilter<exclude> needs to be the last filter run -->
<% if [subfilter<tf.toc-hasChildren>] [subfilter<tf.toc-curTidInclude>] -[<currentTiddler>] -[subfilter<exclude>] +[limit[1]] %>
<<toc-item>>
<% else %>
<$button class='tc-btn-invisible'>
{{$:/core/images/blank}}
</$button>
<% endif %>
<<_itemTemplate>>
</$link>
<!-- TODO auto expand -->
<% if [<toc-state>get[text]match[open]] %>
<$macrocall $name="toc-selective-expandable"
tag=<<currentTiddler>> sort=<<sort>> exclude=<<exclude>> path=<<path>>
2024-02-07 12:17:38 +00:00
captionField=<<captionField>> tagField=<<tagField>> parentField=<<parentField>>
itemTemplate=<<itemTemplate>>
/>
<% endif %>
</li>
</$qualify>
\end
<!-- <$log ct=<<currentTiddler>>
includeList={{{ [subfilter<tf.toc-hasChildren>] [subfilter<tf.toc-curTidInclude>] -[subfilter<exclude>] +[limit[1]] }}}
hasChildren={{{ [subfilter<tf.toc-hasChildren>] }}} path=<<path>> exclude=<<exclude>>/> -->
2024-02-07 12:17:38 +00:00
\procedure toc-unlinked-selective-expandable-body(tag,sort:"",itemClassFilter,exclude,path,captionField,tagField,parentField,itemTemplate)
<$qualify name="toc-state" title=<<tf.toc-stateTitle>> >
<li class=<<tf.toc-itemClass>>>
<% if [subfilter<tf.toc-hasChildren>] [subfilter<tf.toc-curTidInclude>] -[<currentTiddler>] -[subfilter<exclude>] +[limit[1]] %>
<<toc-item isUnlinked:"yes">>
<% else %>
<$button class="tc-btn-invisible">
{{$:/core/images/blank}}
</$button>
<span class="toc-item-muted">
<<_itemTemplate>>
</span>
<% endif %>
<!-- TODO auto expand -->
<% if [<toc-state>get[text]match[open]] %>
<$macrocall $name="toc-selective-expandable"
tag=<<currentTiddler>> sort=<<sort>> exclude=<<exclude>> path=<<path>
2024-02-07 12:17:38 +00:00
captionField=<<captionField>> tagField=<<tagField>> parentField=<<parentField>>
itemTemplate=<<itemTemplate>>
/>
<% endif %>
</li>
</$qualify>
\end
<!-- =====================================================
Shows a selctive expandable toc.
If an item has no children, there is no open/close chevron
========================================================== -->
2024-02-07 12:17:38 +00:00
\procedure toc-selective-expandable(tag,sort:"",itemClassFilter,exclude,path,captionField,tagField,parentField,itemTemplate)
<$set name=currentTiddler filter="[<parentField>!is[blank]then<tag>else<currentTiddler>]">
<ol class="tc-toc toc-selective-expandable">
<!-- text substitution is needed for backwards compatibility -->
2024-02-05 19:07:10 +00:00
<$list filter=` [subfilter<tf.toc-filter>]
:else[subfilter<tf.toc-filterString>$(sort)$]
[subfilter<tf.toc-include>] -[<tag>] -[subfilter<exclude>]`>
<%if [<currentTiddler>] -[subfilter<tf.toc-hide>] %>
<$let _path=<<tf.toc-newPath>>>
<$set name="_excluded" filter="[subfilter<exclude>] [<tag>] [subfilter<tf.toc-hide>]">
<% if [all[current]toc-link[no]] %>
<$macrocall $name="toc-unlinked-selective-expandable-body"
tag=<<tag>> sort=<<sort>> exclude=<<_excluded>> path=<<_path>>
2024-02-07 12:17:38 +00:00
captionField=<<captionField>> tagField=<<tagField>> parentField=<<parentField>>
itemTemplate=<<itemTemplate>>
/>
<% else %>
<$macrocall $name="toc-linked-selective-expandable-body"
tag=<<tag>> sort=<<sort>> exclude=<<_excluded>> path=<<_path>>
2024-02-07 12:17:38 +00:00
captionField=<<captionField>> tagField=<<tagField>> parentField=<<parentField>>
itemTemplate=<<itemTemplate>>
/>
<% endif %>
</$set>
</$let>
<% endif %>
</$list>
</ol>
</$set>
\end
2014-11-13 21:39:28 +00:00
<!-- ==============================================================
Shows a tabbed toc.
If a toc-item link is clicked it will open the tiddler in the story
=================================================================== -->
2024-02-07 12:17:38 +00:00
\procedure toc-tabbed-external-nav(tag,sort:"",selectedTiddler:"$:/temp/toc/selectedTiddler",unselectedText,missingText,template:"",exclude,captionField,tagField,parentField,itemTemplate)
<$tiddler tiddler={{{ [<selectedTiddler>get[text]] }}}>
<div class="tc-tabbed-table-of-contents">
<$linkcatcher to=<<selectedTiddler>>>
<div class="tc-table-of-contents">
<!-- v5.3.4 itemClassFilter has been replaced by tf.toc-itemClassFilter -->
<$macrocall $name="toc-selective-expandable"
tag=<<tag>> sort=<<sort>> exclude=<<exclude>>
2024-02-07 12:17:38 +00:00
captionField=<<captionField>> tagField=<<tagField>> parentField=<<parentField>>
itemTemplate=<<itemTemplate>>
/>
</div>
</$linkcatcher>
<div class="tc-tabbed-table-of-contents-content">
<$reveal stateTitle=<<selectedTiddler>> type="nomatch" text="">
<$transclude mode="block" tiddler=<<template>>>
<h1><<_itemTemplate>></h1>
<$transclude mode="block">$missingText$</$transclude>
</$transclude>
</$reveal>
<$reveal stateTitle=<<selectedTiddler>> type="match" text="">
<<unselectedText>>
</$reveal>
</div>
</div>
2014-11-13 21:39:28 +00:00
</$tiddler>
\end
<!-- ========================================================
Shows a tabbed toc. If a toc-item link is clicked
it will open the tiddler in the view area of the same tiddler
============================================================= -->
2024-02-07 12:17:38 +00:00
\procedure toc-tabbed-internal-nav(tag,sort:"",selectedTiddler:"$:/temp/toc/selectedTiddler",unselectedText,missingText,template:"",exclude,captionField,tagField,parentField,itemTemplate)
<$linkcatcher to=<<selectedTiddler>>>
<$macrocall $name="toc-tabbed-external-nav"
tag=<<tag>>
sort=<<sort>>
selectedTiddler=<<selectedTiddler>>
unselectedText=<<unselectedText>>
missingText=<<missingText>>
template=<<template>>
exclude=<<exclude>>
2024-02-07 12:17:38 +00:00
captionField=<<captionField>>
tagField=<<tagField>>
parentField=<<parentField>>
itemTemplate=<<itemTemplate>>
/>
2014-11-13 21:39:28 +00:00
</$linkcatcher>
\end