Getting a value from one of the current tiddler's fields from within a JavaScript macro module

40 views
Skip to first unread message

Kalcifer Kandari

unread,
May 24, 2020, 7:16:13 PM5/24/20
to TiddlyWikiDev
I mean, the title explains it. Been looking through the source code trying to find the answer.

The macro I want to create is incredibly simple:
  • Get the tags of the current tiddler.
  • If one starts with 'feed--', return it.
I thought it would be as simple as something like: this.getTiddler("currentTiddler").field("tags"). Not quite so easy. I got as far as discovering this.getVariable("currentTiddler"), not sure what I can do with the just title though.

Kalcifer

Joshua Fontany

unread,
May 24, 2020, 7:51:55 PM5/24/20
to TiddlyWikiDev
If we look at the Button Widget code (among others) we can get tiddler-field data as a string thusly:

this.wiki.getTiddler(this.setTitle).getFieldString(this.setField)
If you don't know if the tiddler exits, there is a this.wikitiddlerExist() method to call, for example from the Link Widget:

this.isMissing = !this.wiki.tiddlerExists(this.to);

Best,
Joshua F

Joshua Fontany

unread,
May 24, 2020, 7:54:42 PM5/24/20
to TiddlyWikiDev
Also, once you return a tiddler, you can access all of its fields through its `fields` property, like so:

let tiddler = this.wiki.getTiddler(title);
let tiddlerText = tiddler.fields.text; //OR tiddler.fields["text"]

The commented method allows you to get odd field names or to pass a fieldname by reference/variable.

Best,
Joshua F

Kalcifer Kandari

unread,
May 24, 2020, 8:13:30 PM5/24/20
to TiddlyWikiDev
Thank you so much!

Here is the code I actually used:
tags = this.wiki.getTiddler(this.getVariable("currentTiddler")).getFieldString("tags");

See, I wasn't far off with my prediction of what the API would look like, just couldn't find it. Shame there's no reference, would have saved me a few hours.

Thanks again!

Kalcifer

Kalcifer Kandari

unread,
May 24, 2020, 9:17:32 PM5/24/20
to TiddlyWikiDev
My first JavaScript macro if anyone cares, using the code above.

It's useful when creating lists based on tags.

For example, if a list is made by filtering by several different tags, you might want to display which tag caused an item to be on the list. You would need to mark those tags somehow, the way I do it is by using a prefix feed--, as in feed--foo and feed--bar, and so on. Then when generating the list within a <$list>, you can use <<match-tag """^feed--""">>.

/*\
title: return-feed
type: application/javascript
module-type: macro

2020-05-24-18-49-31

Macro to return the matched tags of the currentTiddler given a regular expression.
\*/


(function(){

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

// The name of the macro, it doesn't have to be the same as this tiddler. This is how the macro is called.
exports
.name = "match-tag";

// Parameters given to the macro when it is called.
exports
.params = [
   
{name: "regularExpression"}
];

// Run the macro.
exports
.run = function(regularExpression) {
   
   
// Get the tag list.
   
var tags = this.wiki.getTiddler(this.getVariable("currentTiddler")).getFieldString("tags").split(" ");
   
   
// Convert regularExpression from a string to a RegExp.
    regularExpression
= new RegExp(regularExpression);
   
   
// Really this is a string, but TiddlyWiki calls it a list.
   
var returnList = "";
   
   
// For every tag, see if it matches the regularExpression, if it does, add it to the returnList.
   
var i;
   
for (i = 0; i < tags.length; i++) {
       
if (tags[i].search(regularExpression) !== -1) {
           
if (i === 0) {
                returnList
= tags[i];
           
} else {
                returnList
= returnList.concat(" ", tags[i]);
           
}
       
}
   
}
   
   
return returnList;
};

})();

Kalcifer
Reply all
Reply to author
Forward
0 new messages