tiddlywikitiddlywiki5

How can I reuse code between Javascript macros and minimize work done within the macros?


I currently have two macros that are part of a (very limited-audience) plugin I'm developing, that both look basically like:

(function(){
exports.name = "name";

exports.params = [
    {name: "value"}
];

function get(tiddler) {
    // return some contents of some tiddler fields according to some rule
}

function parse(data) {
    // convert string to some kind of useful object
}

function logic(x, y) {
    // determine whether the two objects correspond in some way
};

function format(data, title) {
    // produce WikiText for a link with some additional decoration
};

exports.run = function(value) {
    value = parse(value);

    var result = [];
    this.wiki.each(function(tiddler, title) {
        var data = get(tiddler);
        if (data !== undefined && logic(value, parse(data))) {
            result.push(format(data, title));
        }
    });
    return result.join(" | ");
};
})();

So they're already fairly neatly factored when considered individually; the problem is that only the core logic is really different between the two macros. How can I share the functions get, logic and format between the macros? I tried just putting them in a separate tiddler, but that doesn't work; when the macros run, TW raises an error claiming that the functions are "not defined". Wrapping each function as its own javascript macro in a separate tiddler, e.g.

(function(){
exports.name = "get";

exports.params = [
    {name: "tiddler"}
];

exports.run = function(tiddler) {
    // return some contents of some tiddler fields according to some rule
}
})();

also didn't help.

I'd also like to set this up to be more modular/flexible, by turning the main get/parse/logic/format process into a custom filter, then letting a normal filter expression take care of the iteration and using e.g. the widget or <> macro to display the items. How exactly do I set this up? The documentation tells me

If the provided filter operators are not enough, a developer can add new filters by adding a module with the filteroperator type

but I can't find any documentation of the API for this, nor any examples.


Solution

  • How can I share the functions get, logic and format between the macros?

    You can use the Common/JS standard require('<tiddler title>') syntax to access the exports of another tiddler. The target tiddler should be set up as a JS module (ie, the type field set to application/javascript and the module-type field set to library). You can see an example here:

    https://github.com/Jermolene/TiddlyWiki5/blob/master/core/modules/widgets/count.js#L15

    I'd also like to set this up to be more modular/flexible, by turning the main get/parse/logic/format process into a custom filter, then letting a normal filter expression take care of the iteration and using e.g. the widget or <> macro to display the items. How exactly do I set this up?

    The API for writing filter operators isn't currently documented, but there are many examples to look at:

    https://github.com/Jermolene/TiddlyWiki5/tree/master/core/modules/filters