What is new in the Dynannotate Relaunch? How can I sort Dynannotate Annotation Tiddlers?

Thanks @Yaisog for coming back to the dynannotate-plugin - which to my mind is still underrated in TW.
What exactly do the enhancements announced in the prerelease of 5.3 bring?
(later edit: This is explained in the link above…)

A question I was struggeling with for some time (trying to use tw for a sidebar-revision-plugin):
How can I sort the annotations made to a tiddler in the order of their appearance?

I have to admit that I have not worked much with annotations, yet. I mostly use the highlighting functionality of Dynannotate.

If I were to tackle your problem, I would probably build something that does the following:

  • Build a filter to gather all annotations for the tiddler (you probably already have that)
  • Define a sortsub[] macro/procedure or :sort filter run that does the following:
    • Use search-replace::regexp[] to get the text before the annotation text (which is saved in the annotate-text field of the annotation tiddlers). The regexp for that shouldn’t be too difficult.
    • Use length[] to find the number of characters in that text

This way, the annotations are sorted by the amount of text that exists in the tiddler before they appear, which should be the order you want. I’m not sure if all that regexp searching/replacing will slow things down, though…

1 Like

Hi @Yaisog so far I did not achieve my goal… Can you tell me what is wrong?

<$vars
	lettersbefore="[splitregexp[annotate-suffix]length[]]"
>
<$list filter="[annotate-tiddler{$:/HistoryList!!current-tiddler}has[annotate-text]sortsub:number<lettersbefore>]">
{{!!title}}{{!!annotate-text}}: {{!!text}}<br>
</$list>
</$vars>

Here is my Testwiki

correction 1: Okay, it works now… (you have to click the title to put the tiddler into the focus)
correction 2: Arghhh they lost their order again… Why is that?

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?

1 Like

Thanks @Yaisog , I built a rough annotationManager for the sidebar which mach be tried and dragged from here:
http://szen.io/ToDo/#How%20can%20I%20sort%20the%20annotations%20made%20to%20a%20tiddler%20in%20the%20order%20of%20their%20appearance%3F