I realistically have a thousand tiddlers with titles like:
Of note: all dates start with the character string "201"
I am interested in renaming them with the date in the front of the title, so that I can "!sort[]" them in filters, easily giving me the most recent ones at the top of the list.
I realistically have a thousand tiddlers with titles like:
- Foo Bar meeting 2016-9-12
- Quarterly staff meeting 2017-04-22
- 2017-07-21 Department Huddle
"use strict";
// read existing tiddlers
var titles = $tw.wiki.getTiddlers();
var newTitle = function (elem) {
var xx,
match = elem.match(/(.+)(\d{4}-\d?\d-\d?\d)(.*)/);
if (match != null) {
// matched text: match[0]
// match start: match.index
// capturing group n: match[n]
// swap matches
if (match[1] && match[2]) {
xx = match[2] + " " + match[1]
}
// If there is some text after the date append it
// format: <date> <match 1>--<match 3>
// just change the "--" to your likings
if (match[3]) xx = xx + "--" + match[3];
return xx.trim(); // trim() removes trailing spaces
} else {
return elem;
}
}
// create new titles
var newTitles = titles.map(newTitle)
newTitles.map(function(elem) {console.log(elem)})
"use strict";
// read existing tiddlers
var titles = $tw.wiki.getTiddlers();
var rewriteTitles = function (elem) {
var xx,
match = elem.match(/(.+)(\d{4}-\d?\d-\d?\d)(.*)/);
if (match != null) {
// matched text: match[0]
// match start: match.index
// capturing group n: match[n]
// swap matches
if (match[1] && match[2]) {
xx = match[2] + " " + match[1]
}
// If there is some text after the date append it
// format: <date> <match 1>--<match 3>
// just change the "--" to your likings
if (match[3]) xx = xx + "--" + match[3];
var tid = $tw.wiki.getTiddler(elem)
xx.trim(); // trim() removes trailing spaces
// danger zone ...
if (tid) {
$tw.wiki.addTiddler(new $tw.Tiddler(tid, {title: xx}));
$tw.wiki.deleteTiddler(elem);
// create some feedback
console.log(elem, "-->", xx);
}
return xx;
} else {
return elem;
}
}
// Enter the Danger Zone!
// BACKUP - BACKUP - BACKUP before you execute this!
// You need to uncomment the next line to excute the function!!
// titles.map(rewriteTitles);
You seem to be a wizard. Can you confirm your approach for me so I may learn from your example?
Loading tiddlywiki in your browser it is stored in active memory. I the developer console you are running a script to search then modify the tiddlywiki in memory. Then the user will save it (from memory) to disk and WALA it is now modified?
Is this another strong point for tiddlywikis single file quine nature or are there other cases that work the same?
Thanks
Tony
You seem to be a wizard.
Can you confirm your approach for me so I may learn from your example?Loading tiddlywiki in your browser it is stored in active memory.
I the developer console you are running a script to search then modify the tiddlywiki in memory.
Then the user will save it (from memory) to disk and WALA it is now modified?
Is this another strong point for tiddlywikis single file quine nature or are there other cases that work the same?
In database theory, one of the central tenets is to use each field to do only one thing. TW already breaks this by using the Title field for a title and an ID. Now you want to also use it for a date field.
Why not extract the dates and put them in their own date field (e.g. actiondate). Then you can sort on that field. This has the additional benefit of not breaking any existing links.
I did create 2 short scripts, that you can copy paste into your developer console and execute there.
...
The following script doesn't modify anything.It just outputs a list of new tiddler names. ...