How to not include Table of Contents state tiddlers when saving a single file wiki?

I have a single file wiki which has a big table of contents generated by the toc-selective-expandable macro.

I have noticed it has a lot of tiddlers named like $:/state/toc/PATH-FROM-MY-TOC (about 500 so far, but the wiki keeps growing). Judging by their created and modified fields, these tiddlers seem to be persistent across saves. As I understand, the purpose of them is to store the open/close state of certain items from the ToC tree. I don’t really need this info to persist across saves, I’m fine with having the ToC fully collapsed whenever I load the wiki.

I can select all these tiddlers with a filter like this:

<$vars regex="^\$:/state/toc">

{{{[is[system]regexp<regex>]}}}
</$vars>

Is it possible to exclude these tiddlers when saving the wiki?

The filter that determines what gets saved is defined in $:/core/save/all, which has this contents:

\import [subfilter{$:/core/config/GlobalImportFilter}]
\define saveTiddlerFilter()
[is[tiddler]] -[prefix[$:/state/popup/]] -[prefix[$:/temp/]] -[prefix[$:/HistoryList]]
-[status[pending]plugin-type[import]] -[[$:/boot/boot.css]]
-[is[system]type[application/javascript]library[yes]] -[[$:/boot/boot.js]]
-[[$:/boot/bootprefix.js]] +[sort[title]] $(publishFilter)$
\end
{{$:/core/templates/tiddlywiki5.html}}

The saveTiddlerFilter() macro defines the list of tiddlers that will be saved. Take note of the last bit, where it says $(publishFilter)$. This allows you to easily modify the list of tiddlers without directly editing $:/core/save/all. Instead, define a separate global macro named “publishFilter”, listing which tiddlers you do (or don’t) want saved.

For your use-case, you want to discard all tiddlers that start with $:/state/toc. To do this, just create a tiddler (e.g., “MyPublishFilter”), tagged with $:/tags/Global, containing the following filter run:

\define publishFilter() -[prefix[$:/state/toc]]

Note the leading minus sign on the filter run. This indicates that the matching tiddlers will be automatically excluded whenever your TiddlyWiki is saved.

enjoy,
-e

3 Likes

This is so much better than the modest mention of publishFilter in https://tiddlywiki.com/#SavingMechanism , which I found by searching for the keyword, after I’ve read your answer.

I also notice that you used prefix filter operator instead of regexp. It is, of course a more lightweight tool. Not to mention that it excludes the need to use temporary variables to hold regular expressions, for avoiding clashes when certain regexp characters also have a special role in filter syntax. I assume it’s because you did it many times and just remember all filter operators. While I had to skim through https://tiddlywiki.com/#Filter%20Operators and stop on regexp because “it’s good enough to do what I need” and also appears in that big list before prefix. Having that big list split into a couple subgroups helps to a certain point, not in this case though. Which makes me wonder if there are approaches of looking for the needed filter operator that are more effective than brute-forcing items of that big list one by one every time.