If you want to use the Zettelkasten method, you probably feel the need to mark your notes with a unique, numeric identifier. Although you can create a link to an ID that is stored in a field, it is not as clear as if the ID was the name of the tiddler (for example, in the Node.js wiki, filenames contain the name of the tiddler). Use the following solution to rename TiddlyWiki tiddlers to datetime IDs (DTIDs) created from the value of the created field.
To see what problems the script has encountered, you can filter console messages from the drop-down menu at the top of the console window to show only the errors (if you're done, don't forget to reset).
var filterAlreadyRenamed = "!regexp[^\\d{14}$]";
var filterHashtagLinks = "!prefix[#]!search[#Referencia]";
var filterExpression = "[!is[system]" + filterAlreadyRenamed + filterHashtagLinks + "]";
function generateId(fields) {
return fields.created
? $tw.utils.formatDateString(fields.created,"YYYY0MM0DD0hh0mm0ss")
: undefined;
}
if (hasDuplicatedIds(filterExpression)) {
throw new Error("Duplicate IDs are found, please resolve this issue before running the script");
}
$tw.utils.each($tw.wiki.filterTiddlers(filterExpression),
function (current) {
var fields = Object.assign({}, $tw.wiki.getTiddler(current).fields);
var id = generateId(fields);
if (!id) {
console.error("Skip tiddler (cannot generate ID):", current);
return;
}
addHeadline(fields);
$tw.wiki.renameTiddler(fields.title, id);
makeBacklinksVerbose(id, fields.title);
}
);
function hasDuplicatedIds(filterExpression) {
var idCounter = {};
$tw.utils.each($tw.wiki.filterTiddlers(filterExpression),
function (current) {
var fields = $tw.wiki.getTiddler(current).fields;
var id = generateId(fields);
if (!id) {
return;
}
if (!idCounter[id]) {
idCounter[id] = [];
}
idCounter[id].push(current);
}
);
var results = false;
$tw.utils.each(idCounter,
function (current) {
if (current.length > 1) {
console.error("Duplicated ID:", current);
results = true;
}
}
);
return results;
}
function addHeadline(fields) {
if (fields.text.match(/\\define/)) {
console.error("Headline added to end of the body (contains macros):", fields.title);
fields.text = fields.text + "\n\n! " + fields.title;
} else {
fields.text = "! " + fields.title + "\n\n" + fields.text;
}
$tw.wiki.addTiddler(fields);
}
function makeBacklinksVerbose(id, verboseTitle) {
$tw.utils.each($tw.wiki.filterTiddlers("[[" + id + "]backlinks[]]"),
function (backlink) {
var backlinkFields = Object.assign({}, $tw.wiki.getTiddler(backlink).fields);
backlinkFields.text = backlinkFields.text.replace("[[" + id +"]]", verboseTitle + " ([[" + id + "]])");
$tw.wiki.addTiddler(backlinkFields);
}
);
}
To keep the headlines readable in the search results, change the $:/core/ui/ListItemTemplate tiddler: if the text contains a headline, print it and include the title, which is either the DTID or for unmodified (e.g., shadow) tiddlers, the original title.