RelatedTiddlersPlugin usage

11 views
Skip to first unread message

RA

unread,
Apr 20, 2009, 7:39:57 AM4/20/09
to TiddlyWiki
I'd like to use RelatedTiddlersPlugin in a manner similar to
SectionLinksPlugin.

A primer would be appreciated on how to achieve the following
functionality (I suspect most of it can be had using the provided
API?)


* have a list/tree only, no "show form" button and no "There are no
tiddlers related..." text
* have a parameter for tree/list instead of global option
* define a placeholder in ViewTemplate

<span class='relatedTiddlers small' style='display:none'>
<b>Related:</b>
</span>
...
<span macro='relatedTiddlers here hideform hidebutton "exclude list"
tree/list'></span>

* in relatedTiddlers macro, make target visible when relatedTiddlers
is rendered AND the list/tree is not empty.

Thanks.

-- R

Eric Shulman

unread,
Apr 20, 2009, 10:27:01 AM4/20/09
to TiddlyWiki
> I'd like to use RelatedTiddlersPlugin in a manner similar to
> SectionLinksPlugin.

You mean like the <<sectionTOC>> macro (which automatically generates
a tree of links to sections *within* the current tiddler)... but
showing a tree of TiddlyLinks to related tiddlers instead of sections?

> A primer would be appreciated on how to achieve the following
> functionality (I suspect most of it can be had using the provided
> API?)

The Usage section of the plugin documentation says:
-----------------------------
The plugin also defines two functions that can be called externally
(from other plugins or scripts) to generate and retrieve either a list
of links or a formatted "tree view":
var list=config.macros.relatedTiddlers.getList
(start,exclude,callback);
var tree=config.macros.relatedTiddlers.getTree
(start,exclude,callback);
These functions accept parameters to specify the starting tiddler, and
a list of tiddlers to exclude, as well as an optional callback
function that takes any specified tiddler as input and returns a
custom-defined array of links related to that tiddler:
var list=callback(tiddler);
Use of the callback function enables you to generate an alternative
list/tree, based on application-specific data (such tiddler references
contained in tags or custom fields), rather than using the default
"links" list.
-----------------------------

In addition, the Examples section of the plugin documentation, shows
some sample inline scripting to invoke the plugin-defined function.
With a few changes, this script will do exactly what you want:
-----------------------------
<script>
var here=story.findContainingTiddler(place);
var target=jQuery('#'+here.id+' .relatedTiddlers')[0];
var start=here.getAttribute('tiddler');
var exclude=config.options.txtRelatedTiddlersExclude.readBracketedList
();
var count=config.macros.relatedTiddlers.getList
(start,exclude).length;
if (count>1) {
var tree=config.macros.relatedTiddlers.getTree(start,exclude);
if (target) target.style.display='inline';
wikify(tree,target||place);
}
</script>
-----------------------------

This will generate the desired tree output, starting from the current
tiddler.title and then uses a jQuery() call to locate and render the
desired 'target' element (by matching the .relatedTiddlers classname)
within the currently rendered tiddler. Note: if no such target
element is found, the output is written directly into the current
'place' (i.e., the location where the script itself is being
rendered).

To invoke this script from within your ViewTemplate (so the tree is
automatically rendered for each tiddler), first put it into a separate
tiddler (e.g., [[ShowRelatedTiddlers]]), and then write the following
in the template:
-----------------------------
<span class='relatedTiddlers small' style='display:none'
macro='tiddler ShowRelatedTiddlers'>
<b>Related:</b><br>
</span>
-----------------------------
This both creates the target element *and* invokes the script that
renders the tree into it.

enjoy,
-e
Eric Shulman
TiddlyTools / ELS Design Studios

RA

unread,
Apr 20, 2009, 8:24:17 PM4/20/09
to TiddlyWiki
Works well except the "making it visible" part is not working for me.
The span remains hidden unless I remove the style from ViewTemplate.
Also, wouldn't it be more economical to check tree.length instead of
calling getList just to get the number?

Thanks Eric.

-- R

Eric Shulman

unread,
Apr 20, 2009, 9:49:12 PM4/20/09
to TiddlyWiki
> Works well except the "making it visible" part is not working for me.
> The span remains hidden unless I remove the style from ViewTemplate.

hmm... not sure about that one.

> Also, wouldn't it be more economical to check tree.length instead of
> calling getList just to get the number?

config.macros.relatedTiddlers.getList()
returns an array of related tiddler titles
config.macros.relatedTiddlers.getTree()
returns a single text string containing the entire wiki-formatted
tree of related tiddlers

Thus, to check the # of items returned, you can write either:
config.macros.relatedTiddlers.getList(...).length;
OR
config.macros.relatedTiddlers.getTree(...).split('\n').length

The first approach counts the items directly in the array... the
second approach splits the tree output into an array of separate lines
and then count the number of items that results. However, even though
its likely that the second approach would be more efficient for most
cases, I used both functions solely in order to demonstrate how they
might be applied.

-e

RA

unread,
Apr 21, 2009, 9:53:32 PM4/21/09
to TiddlyWiki
Eric,

I realised I don't need the whole "recursive" list or tree. How can I
display a list of links from the current tiddler only (internal and
external), i.e. the first level of the tree?

Thanks.

RA

unread,
Apr 22, 2009, 1:49:02 AM4/22/09
to TiddlyWiki
I think I got it:
return wikify('[['+store.getTiddler(story.findContainingTiddler
(place).getAttribute("tiddler")).getLinks().sort().join(']]\n[[')
+']]',place);

Now how would I make it take into account the
chkDisableNonExistingWikiLinks=true?
Perhaps DisableWikiLinksPlugin should remove such links as well, not
excluded links only.

-- R

Eric Shulman

unread,
Apr 22, 2009, 2:21:30 AM4/22/09
to TiddlyWiki
On Apr 21, 10:49 pm, RA <nameany...@gmail.com> wrote:
> I think I got it:
> return wikify('[['+store.getTiddler(story.findContainingTiddler
> (place).getAttribute("tiddler")).getLinks().sort().join(']]\n[[')
> +']]',place);
>
> Now how would I make it take into account the
> chkDisableNonExistingWikiLinks=true?

Try this:

<script>
var tid=story.findContainingTiddler(place).getAttribute("tiddler");
var links=store.getTiddler(tid).getLinks();
for (var i=0; i<list.length; i++) if (!store.tiddlerExists(list[i]))
list[i]='';
return wikify('[['+list.sort().join(']]\n[[')+']]',place);
</script>

-e

RA

unread,
Apr 22, 2009, 2:46:48 AM4/22/09
to TiddlyWiki
And is there a way to list external links as well?

BTW, how do you like the following, is it cheating?

StyleSheet:
.relatedTiddlers .tiddlyLinkNonExisting {display:none;}

ShowRelatedTiddlers:
<script>
var tid=story.findContainingTiddler(place).getAttribute("tiddler");
var links=store.getTiddler(tid).getLinks();
return wikify('{{relatedTiddlers{\n:[['+links.sort().join(']]\n:[[')
+']]\n}}}',place);
</script>

-- R

RA

unread,
Apr 22, 2009, 2:56:16 AM4/22/09
to TiddlyWiki
Another issue: in a tiddler called "scratchpad (1)" I get "Syntax
error, unrecognized expression: (1)" instead of the list.

-- R

RA

unread,
Apr 22, 2009, 6:33:29 AM4/22/09
to TiddlyWiki
And a core bug to boot:

var target = jQuery('#'+here.id+' .'+targetClass)[0];

doesn't find the element when tiddler name contains spaces.

See http://www.w3.org/TR/html401/types.html#type-name

-- R.

RA

unread,
Apr 22, 2009, 6:32:54 PM4/22/09
to TiddlyWiki
> Try this:
>
> <script>
> var tid=story.findContainingTiddler(place).getAttribute("tiddler");
> var links=store.getTiddler(tid).getLinks();
> for (var i=0; i<links.length; i++) if (!store.tiddlerExists(links[i]))
> links[i]='';
> return wikify('[['+links.sort().join(']]\n[[')+']]',place);
> </script>
>
> -e

Is it Ok to splice the non-existent links out of the array instead of
just making them empty? Because I want to use

if (links.length && target) target.style.display='block';

And is there a way to list external links as well?


-- R

Eric Shulman

unread,
Apr 22, 2009, 7:04:06 PM4/22/09
to TiddlyWiki
> Is it Ok to splice the non-existent links out of the array instead of
> just making them empty? Because I want to use
>
> if (links.length && target) target.style.display='block';

instead of
links.length
use
links.join('').length
to test for an empty text string

> And is there a way to list external links as well?

hmmm... not using the same technique... I'll have to think about it
and do some experiments...

-e
Reply all
Reply to author
Forward
0 new messages