Refactoring prettyLink for better extendability

3 views
Skip to first unread message

Udo Borkowski

unread,
May 10, 2006, 2:26:02 AM5/10/06
to Tiddly...@googlegroups.com
I got a little refactoring request:

in the "prettyLink" formatter when the target specified in the bracketted link is not an existing tiddler it will automatically generate an external link using a call to createExternalLink.

Can we factor out this call into a separate function (e.g. "handleMissingPrettyLink") to make it easier to modify this behaviour (e.g. to create tiddlyLinks instead etc.). Currently a plugin needs to overwrite the complete formatter to accomplish this.

I.e:

function handleMissingPrettyLink(place, label, link) {
    return createExternalLink(place, link);
}

...
config.formatters = [
...
{
    name: "prettyLink",
    match: "\\[\\[",
    lookahead: "\\[\\[([^\\|\\]]*?)(?:(\\]\\])|(\\|(.*?)\\]\\]))",
    terminator: "\\|",
    handler: function(w)
    {
        var lookaheadRegExp = new RegExp(this.lookahead,"mg");
        lookaheadRegExp.lastIndex = w.matchStart;
        var lookaheadMatch = lookaheadRegExp.exec(w.source)
        if(lookaheadMatch && lookaheadMatch.index == w.matchStart && lookaheadMatch[2]) // Simple bracketted link
            {
            var link = createTiddlyLink(w.output,lookaheadMatch[1],false);
            w.outputText(link,w.nextMatch,w.nextMatch + lookaheadMatch[1].length);
            w.nextMatch += lookaheadMatch[1].length + 2;
            }
        else if(lookaheadMatch && lookaheadMatch.index == w.matchStart && lookaheadMatch[3]) // Pretty bracketted link
            {
            var e;
            if(store.tiddlerExists(lookaheadMatch[4]) || store.isShadowTiddler(lookaheadMatch[4]))
                e = createTiddlyLink(w.output,lookaheadMatch[4],false);
            else
                e = handleMissingPrettyLink(w.output,lookaheadMatch[3],lookaheadMatch[4]);
               
            w.outputText(e,w.nextMatch,w.nextMatch + lookaheadMatch[1].length);
            w.nextMatch = lookaheadMatch.index + lookaheadMatch[0].length;
            }
    }
},
...



A possible adaption may look like this:

function handleMissingPrettyLink(place, label, link) {
    var s = link.substr(0,6);
    var isExternal = s == "file:/" || s == "http:/" || s == "https:";
    return isExternal ? createExternalLink(place, link) : createTiddlyLink(place, link, false);
};

Other implementations may be more sophisticated.


Udo

Jeremy Ruston

unread,
May 10, 2006, 5:16:23 AM5/10/06
to Tiddly...@googlegroups.com
Excellent idea, thanks.

Cheers

Jeremy


--
Jeremy Ruston
mailto:jer...@osmosoft.com
http://www.tiddlywiki.com

Reply all
Reply to author
Forward
0 new messages