[TW5] More JavaScript macro questions

111 views
Skip to first unread message

Tristan Kohl

unread,
Apr 13, 2016, 11:26:20 AM4/13/16
to TiddlyWiki
Hey guys,

i started creating my e-book management with TW a couple days ago. But there are still some things I do not get right away so I hope you can help me out with them.

First a short explanation of my "design": I have a template tiddler which is used to show the details of a book. In this template a call my custom macro to make a comma separated list of links from a field with comma separated author names. So i.e. field "authors" contains Mark Twain and J.R.R. Tolkien separated by comma. My macro produces links like [[Mark Twain]], [[J.R.R. Tolkien]] so I can find books for every author via backlinks.

My problem is that I can not figure out, how to use the value of "authors" within my JavaScript macro. At the moment I use the set widget to create a variable authors:
<$set name=authors value={{!!authors}}><$macrocall $name="linklist" field=<<authors>> /></$set>

But this looks like overkill for me and is not that readable, so I wonder if it Is possible to replace this whole setup with something like (which does not work for me):
<<linklist {{!!authors}}>>

There is another question that bothers me. As I am not a native English speaker I do not get the difference between substitution and transclusion. I figure one is before the macro call and the other one after that? But which is when and how do I use substitution at all? Unfortunately the documentation is very short and abstract about this I feel.

Thanks for your help
Tristan

Mark S.

unread,
Apr 13, 2016, 2:03:43 PM4/13/16
to TiddlyWiki
Hi Tristan,

Your English is great -- the documentation is indeed short and abstract.

I've never had much luck passing things to a javascript macro without using a <$set> variable. I'm thinking it might be possible to use another standard macro to wrap the <$set> process.

Could you provide the javascript macro and template for testing purposes?

Mark

Mark S.

unread,
Apr 13, 2016, 2:25:54 PM4/13/16
to TiddlyWiki

You might try invoking it like this:

<$macrocall $name="listlinks" text={{!!authors}}/>

Where "text" is whatever you have set up as your parameter variable name. That seemed to work on my quick test.


Mark

On Wednesday, April 13, 2016 at 8:26:20 AM UTC-7, Tristan Kohl wrote:

Tristan Kohl

unread,
Apr 14, 2016, 2:49:29 AM4/14/16
to TiddlyWiki
Hi Mark,

here is my macro code:
/*\

title: $:/plugins/mirodin/ebooks/linklist.js
type: application/javascript
module-type: macro

\*/

(function() {

"use strict";

/*
List all field values as comma separated list of clickable links.
*/


exports
.name = "linklist";

exports
.params = [];

/*
Run the macro
*/

exports
.run = function(field) {
 
if (typeof field === "undefined") {
 
return "''Please pass field.''";
 
}
 
var elements = field.split(",");
 
var result = "";
 
for (var i = 0; i < elements.length; i++) {
 result
+= "[[" + elements[i] + "]], ";
 
}
 result
= result.slice(0, -2);
 
return result;
};

})();

Thanks for your hint, that works for me too and the whole call is way more readable.

Cheers
Tristan

Mark S.

unread,
Apr 14, 2016, 12:02:44 PM4/14/16
to TiddlyWiki
Hi Tristan,

This was the export part of the code I came up with for testing:

exports.run = function(text) {
   
 
var vartext = "[["+text.split(",").join("]] [[")+"]]"   ;
return vartext ;  

};

Both our approaches probably need something in there to squeeze out spaces after commas.

Have fun!
Mark

Tristan Kohl

unread,
Apr 14, 2016, 2:52:55 PM4/14/16
to TiddlyWiki
Hi Mark,

if I do get your code right you create a "list" of author names without commas?
In my code I join them together and remove the trailing comma and whitespace pair with a slice before returning the text. So far this works for my usecase but yours is way more compact which is nice I guess :)

Thanks a lot for your help and effort I am sure I will ask for more when I dig deeper in my e-book management plugin.

Cheers
Tristan
Reply all
Reply to author
Forward
0 new messages