Generate UIDs for Tiddlywiki
How do I generate a unique title for a new Tiddler?
Questions like this one pop up fairly regularly at the Tiddlywiki mailing list. I had the same problems when I just started out and so I know the struggle when one does not know how to create a Tiddler scheme that either does not need the title or does generate somewhat unique IDs automatically.
For most things I do with TiddlyWiki I use it as a CRUD platform and create forms which modify and create Tiddlers behind the scenes. For these kinds of setups I do not care about the Tiddler names at all since I never look for them directly and all data retrival happens via filter operations over their fields.
So most projects I create follow a workflow like this:
- Enter mostly uniform data/information into a form build from Widgets to create a new record of some sort (i.e. new book in my library).
- Have a standardised view on those records (every book has a title, cover, author, short summary and release year). For this I use ViewTemplates, which show record fields in whatever way I need to.
- Have a view over all records of one type similar to TiddlyWiki's own Tiddler manager where I can delete/update records.
Creating these forms in the first place needs some getting used to since they are build from WikiText and abovementioned Widgets. But so far I could achieve every layout I wanted this way and by now most projects are just a copypasta of previous ones I made.
A few years back I created a little Javascript macro that generates a random 6-place ID for new records and has been my go to whenever I had to create unique Tiddler names. Maybe this is of use to you too :)
/*\
title: $:/tjout/macro/uid.js
module-type: macro
type: application/javascript
Return new UID for tiddler title
1. Create a new Tiddler and past below content into the text field
2. Set the type of the TIddler to "application/javascript"
3. Create the field "module-type" with value "macro"
4. After reloading the Wiki type "<<uid>>" whenever you want an unique ID
\*/
(function() {
"use strict";
exports.name = "uid";
exports.params = [];
exports.run = function() {
let newID;
do {
newID = Math.random().toString(36).substr(2, 6);
} while (this.wiki.getTiddler(newID))
return newID;
};
})();