Hi @JanJo, looking at it in more detail, this becomes a little complicated. But, I think I got it to work:
<$wikify name="content" text={{How can I sort the annotations made to a tiddler in the order of their appearance?}} output="text">
<$list filter="[annotate-tiddler[How can I sort the annotations made to a tiddler in the order of their appearance?]has[annotate-text]]">
<$let matchAll="[\s|\S]*"
searchString={{{ [[(.*]] [<currentTiddler>get[annotate-prefix]escaperegexp[]] [[)]] [<currentTiddler>get[annotate-text]escaperegexp[]] [<currentTiddler>get[annotate-suffix]escaperegexp[]] [<matchAll>] +[join[]] }}}
preString={{{ [<content>search-replace:m:regexp<searchString>,[$1]] }}}
letterCount={{{ [<preString>length[]] }}}>
<$link /> → {{!!annotate-text}} → <<letterCount>><br/>
</$let>
</$list>
</$wikify>
For testing from within another tiddler, I hardcoded the target tiddler name (“How can I…”).
Since the annotation-prefix
, annotation-text
and annotation-suffix
fields are derived from the plain text content of the tiddler, we need $wikify to get us that plain text. Using $wikify is discouraged, as it can slow things down. If your wiki is not too big, this might work, however.
Then, a regexp searchString
is derived from the content of these fields that captures only the content before and including annotation-prefix
. The regexp result is put into preString
and its length into letterCount
. I split it up into different variables for easier debugging.
The result is
$:/Annotation 10 → annotation → 349
$:/Annotation 11 → filter run → 258
$:/Annotation 12 → sortsub[] → 223
$:/Annotation 8 → tiddler → 173
which seems plausible (I haven’t counted).
This was just to figure out a way to get the number we need. To be able to use it as a sortsub
parameter, I like to use \define
for the corresponding definitions, like so:
\define match-all() [\s|\S]*
\define letter-count() [[(.*]] [<currentTiddler>get[annotate-prefix]escaperegexp[]] [[)]] [<currentTiddler>get[annotate-text]escaperegexp[]] [<currentTiddler>get[annotate-suffix]escaperegexp[]] [<match-all>] +[join[]] :map[<content>search-replace:m:regexp<currentTiddler>,[$1]length[]]
<$wikify name="content" text={{How can I sort the annotations made to a tiddler in the order of their appearance?}} output="text">
<$list filter="[annotate-tiddler[How can I sort the annotations made to a tiddler in the order of their appearance?]has[annotate-text]sortsub:number<letter-count>]">
<$link /> → {{!!annotate-text}}<br/>
</$list>
</$wikify>
Here I have to put everything into a single definition, because the parameter of search-replace cannot be another filter, but only a variable. That’s also the reason I use the handy :map
run prefix to apply the search string we built as the search parameter.
I hope this helps you a bit?