How can I filter the $:/HistoryList in reverse order

I have a history tab in the sidebar, and I would like the most recent tab to appear at the top of the list. I was wondering how I could do this without cheating.

By cheating, I mean using CSS to make it work, like this:

.tc-sidebar-tab-open {
	display: block;
	overflow-y: scroll;
	transform: rotate(180deg);
	direction: rtl;
}
.tc-droppable {
	transform: rotate(180deg);
	direction: ltr;
}

Here’s the filter I use in my TiddlyTools History tiddler toolbar popup:

[{$:/HistoryList}split["title": "]splitbefore["]removesuffix["]reverse[]is[tiddler]]

Notes:

  • Start by getting the text contents of the $:/HistoryList tiddler. This is a JSON array structure.
  • Use split["title": "]splitbefore["]removesuffix["] to get the titles from the JSON array.
  • reverse the list (so the most recently viewed title is first)
  • is[tiddler] removes any previously viewed tiddlers that no longer exist (i.e., were deleted during the current session)

Also, take note that the $:/HistoryList maintained by the TWCore does NOT include any tiddlers that were opened at startup using a URL permalink or permaview or listed in the $:/DefaultTiddlers. To have a really “complete” history list, I use this filter:

[{$:/HistoryList}split["title": "]splitbefore["]removesuffix["]reverse[]is[tiddler]]
[enlist{$:/StoryList!!list}]
[subfilter{$:/DefaultTiddlers}]

ref: Inside TiddlyWiki — A Guide for Users, Authors, Designers and Developers

enjoy,
-e

2 Likes

You might also want to read this topic:
https://talk.tiddlywiki.org/t/how-to-open-the-tiddler-just-before-the-current-one-in-the-history/3409
Especially towards the bottom.
Also in that topic, @Ittayd has posted a very useful filter for parsing the HistoryList, which is a bit more reliable than parsing JSON with WikiText.
Have a nice day
Yaisog

Parsing the titles from the $:/HistoryList contents by using split["title": "]splitbefore["]removesuffix["] is 100% completely reliable, because the structure of the JSON in the $:/HistoryList is very well-defined and is automatically generated by the TWCore, so it won’t vary in it’s internal formatting.

Also, while @ittayd’s history[] filter is very nice for general use, it also creates a dependency on a Javascript module, which requires a save-and-reload for it to be used. Using a non-JS method of parsing the $:/HistoryList enables “installing” the code without needing to save-and-reload. This makes it possible to use the functionality with ANY TiddlyWiki, even if it is online and owned by someone else.

For example, my TiddlyTools History Toolbar button https://tiddlytools.com/InsideTW/#TiddlyTools%2FHistory can be dragged directly to https://TiddlyWiki.com and used immediately without having to download anything.

-e

1 Like

@EricShulman: Could we agree on 99.5% reliable? I did run into problems when the title contained quotation marks (").
It may not be good practice to do that, but still… :wink:
Have a nice day
Yaisog

@Yaisog you can make tiddlywiki robustly handle double quotes by ensuring you use tripple double quotes where ever possible, i see singe quotes in use in some core tiddlers, however that is not easy. If you want you can find some higher unicode equivalent double quoutes for use in titles if needed but otherwise I avoid most quotes in titles.

If you can illustrate some situations where you want use them do share and we can explore some alternatives.

I’ve just updated the filter used by TiddlyTools History toolbar button so it now handles double-quotes in tiddler titles.

https://tiddlytools.com/InsideTW/#TiddlyTools%2FHistory

The new history filter is:

[{$:/HistoryList}splitregexp[\n]trim[]trim[,]removeprefix["title": "]removesuffix["]!prefix[Draft]search-replace:g[\"],["]unique[]reverse[]]
[enlist{$:/StoryList!!list}]
[subfilter{$:/DefaultTiddlers}]

Notes:

  • {$:/HistoryList}splitregexp[\n]
    gets the contents of $:/HistoryList and breaks it into separate lines
  • trim[]trim[,]
    removes leading/trailing whitespace and trailing comma from each line
  • removeprefix["title": "]removesuffix["]
    finds only lines starting with "title": " and ending with ", and keeps everything in between.
    These are the tiddler titles
  • !prefix[Draft]
    ignores “Draft of …” titles. Note that the previous filter used is[tiddler], which ignored any shadow tiddlers (e.g., “$:/AdvancedSearch”) as well as tiddlers that were deleted during the current session.
  • search-replace:g[\"],["]
    converts JSON embedded double-quotes (\") to regular double-quotes (")
  • unique[]
    removes duplicate titles
  • reverse[]
    lists titles in “most recently opened” order
  • [enlist{$:/StoryList!!list}]
    adds any titles currently displayed in the StoryRiver
  • [subfilter{$:/DefaultTiddlers}]
    adds any titles listed in $:/DefaultTiddlers

enjoy,
-e

1 Like

Note: I wrote this before Eric’s previous post, but my Internet gave out when actually trying to post. So some of what I wrote is already outdated…

The particular problem with the History is that a double quote in the title is merely escaped to \". Hence, the WikiText parser splits the title after the backslash. One could try splitregexp with a negative lookbehind, but would then also need unescaping of the remaining quotes. I might even have tried my hand at it were it not for the mentioned history[] filter that does it all with basically one line of JavaScript plus the usual filter overhead.

I liked the simplicity so much that I chose the JavaScript module over a WikiText approach, something that I don’t often do, for the exact reasons @EricShulman has mentioned.

@TW_Tones: I removed the quotes from the title and put them into a caption instead. They would be likely to cause more trouble down the road with other custom code in my TW than it’s worth having them. The OP might think differently, though, so I merely wanted to point out an alternative.

Have a nice day
Yaisog

PS: I only use two non-core filters in my own code, history[] and one that converts dates into “human-readable” format like “last Monday” for task lists.

1 Like