Accessing Tiddlers from JavaScript (Crosspost from main TW group)

87 views
Skip to first unread message

Vaughn Papenhausen

unread,
May 1, 2020, 9:23:44 PM5/1/20
to TiddlyWikiDev
[Crossposted from the main TW group here: https://groups.google.com/forum/#!topic/tiddlywiki/XQ90336dG2w]

I should probably begin by saying that I am the noobiest of noobs. I know nothing about coding, just dipping my toes in for the first time. So I have a super, super basic question.

I'm trying to make a modification to the CodeMirror autocomplete plugin. In particular, instead of autocompleting any word in the current tiddler, or autocompleting HTML, I want it to autocomplete tiddler titles (think Roam Research); basically I'm trying to do what SnowGoon's TW5-extendedit plugin does, except in CodeMirror. But CodeMirror is written in JavaScript, not WikiText. So my question is this: how do I load, say, the text of a tiddler, or the title of a tiddler, into a variable in JavaScript? Is there a list of methods somewhere? Reading through code from SnowGoon's plugin I came across the command "$tw.wiki.getTiddlerData()", but I don't know exactly how it works, or what other similar commands there are. I tried looking through the developer documentation for TW but couldn't find anything (I'm guessing this is because I failed to search well enough rather than because it's not there). Could anyone point me to the correct place in the documentation, or explain how to do this?

C Pa

unread,
May 10, 2020, 2:56:49 AM5/10/20
to TiddlyWikiDev
What you need to do is open a TiddlyWiki as a text file and search for this string (regex search)

exports\.[a-z]+[ ]*=

These are all the defined javascript functions you can call

For instance you'll find exports.setText = function(title,field,index,value,options) in the tiddler $:/core/modules/wiki.js

So you can set the text of a field  or an index by inserting a link like this:

<a href="javascript:$tw.wiki.setText('tiddlername','text','','The text to set');">Click me</a>

And you click it. It sets the text of tiddlername to that text.

If you set an index on a new tiddler, you also have to set the type in order to make it a JSON tiddler.

In fact most of the good functions are in that wiki.js tiddler

It is confusing that the code says its Wiki but wiki is what works.

Anyway. That's the only one I've used so far. I use it to enable clicking on SVGs.

It would be nice if someone could explain the rest of it.

Jed Carty

unread,
May 10, 2020, 8:53:00 AM5/10/20
to TiddlyWikiDev
To get a tiddler in javascript you use the function $tw.wiki.getTiddler('tiddler title'), it returns a json object with the from:

{
  fields
: {
    title
: "tiddler title",
    other_field
: "field contents"
 
},
  cache
{}
}

so to get the text of a tiddler you could use:

var text = $tw.wiki.getTiddler('tiddler title').fields.text

there have been a few efforts to make full documentation for the tiddlywiki internals, but no one has succeeded yet.

Arlen Beiler

unread,
May 10, 2020, 9:45:15 AM5/10/20
to tiddlywikidev
To get the list of tiddler titles, you should probably use a filter and get the filter from a tiddler. This is standard practice in the core so you or users can exclude or include tiddler titles in the list. 

To get the functions you can call, you probably want to use the global $tw variable. 

If you want to get the exports of an individual Javascript tiddler, you should probably load it into a proper type checker and get the types from there. 

Just my thoughts. I'm sure there's more than one way to skin the cat.

Arlen

--
You received this message because you are subscribed to the Google Groups "TiddlyWikiDev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywikide...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywikidev/c7a0d8c0-7939-4ec7-9cf0-4eccb7733a76%40googlegroups.com.

Vaughn Papenhausen

unread,
May 16, 2020, 11:55:38 PM5/16/20
to TiddlyWikiDev
Just checked back here and saw all of this -- thanks everyone! This all looks helpful. Saq helped me solve my most immediate problem by providing me with a modification of the anywords hint that he had done, but I am hoping to continue experimenting with customizing TiddlyWiki, so this will all be really helpful.

On Sunday, May 10, 2020 at 9:45:15 AM UTC-4, Arlen Beiler wrote:
To get the list of tiddler titles, you should probably use a filter and get the filter from a tiddler. This is standard practice in the core so you or users can exclude or include tiddler titles in the list. 

To get the functions you can call, you probably want to use the global $tw variable. 

If you want to get the exports of an individual Javascript tiddler, you should probably load it into a proper type checker and get the types from there. 

Just my thoughts. I'm sure there's more than one way to skin the cat.

Arlen

On Sun, May 10, 2020 at 8:53 AM Jed Carty <inmy...@gmail.com> wrote:
To get a tiddler in javascript you use the function $tw.wiki.getTiddler('tiddler title'), it returns a json object with the from:

{
  fields
: {
    title
: "tiddler title",
    other_field
: "field contents"
 
},
  cache
{}
}

so to get the text of a tiddler you could use:

var text = $tw.wiki.getTiddler('tiddler title').fields.text

there have been a few efforts to make full documentation for the tiddlywiki internals, but no one has succeeded yet.

--
You received this message because you are subscribed to the Google Groups "TiddlyWikiDev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddly...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages