mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-12-08 09:48:05 +00:00
Make tiddler file paths configurable (#2379)
When saving new tiddlers on node.js, allow the user to override the path of the generated .tid file. This is done by creating a tiddler $:/config/FileSystemPaths which contains one or more filter expressions, one per line. These filters are applied in turn to the tiddler to be saved, and the first output produced is taken as a logical path relative to the wiki's tiddlers directory. Any occurences of "/" in the logical path are replaced with the platform's path separator, the extension ".tid" is appended, illegal characters are replaced by "_" and the path is disambiguated (if necessary) in order to arrive at the final tiddler file path. If none of the filters matches, or the configuration tiddler does not exist, fall back to the previous file naming scheme (i.e. replacing "/" by "_"). This implies we will now, for tiddlers matching the user-specified filters, create directory trees below the tiddlers directory. In order to avoid cluttering it with empty directory trees when renaming or removing tiddlers, any directories that become empty by deleting a tiddler file are removed (recursively). Benefits of this configuration option include the ability to organize git repositories of TiddlyWikis running on node.js, ability to replace characters that cause trouble with particular operating systems or workflows (e.g. '$' on unix) and the ability to replicate tiddler "paths" in the filesystem (by including a filter like "[!has[draft.of]]") without forcing such a (potentially problematic) change on all users.
This commit is contained in:
@@ -87,12 +87,42 @@ Transliterate string from cyrillic russian to latin
|
||||
}).join("");
|
||||
};
|
||||
|
||||
/*
|
||||
Given a list of filters, apply every one in turn to source, and return the first result of the first filter with non-empty result.
|
||||
*/
|
||||
FileSystemAdaptor.prototype.findFirstFilter = function(filters,source) {
|
||||
var numFilters = filters.length;
|
||||
for(var i=0; i<numFilters; i++) {
|
||||
var result = this.wiki.filterTiddlers(filters[i],null,source);
|
||||
if(result.length > 0) {
|
||||
return result[0];
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
Given a tiddler title and an array of existing filenames, generate a new legal filename for the title, case insensitively avoiding the array of existing filenames
|
||||
*/
|
||||
FileSystemAdaptor.prototype.generateTiddlerFilename = function(title,extension,existingFilenames) {
|
||||
// First remove any of the characters that are illegal in Windows filenames
|
||||
var baseFilename = transliterate(title.replace(/<|>|\:|\"|\/|\\|\||\?|\*|\^|\s/g,"_"));
|
||||
var baseFilename;
|
||||
// Check whether the user has configured a tiddler -> pathname mapping
|
||||
var pathNameFilters = this.wiki.getTiddlerText("$:/config/FileSystemPaths");
|
||||
if(pathNameFilters) {
|
||||
var source = this.wiki.makeTiddlerIterator([title]);
|
||||
var result = this.findFirstFilter(pathNameFilters.split("\n"),source);
|
||||
if(result) {
|
||||
// interpret "/" as path separator
|
||||
baseFilename = result.replace(/\//g,path.sep);
|
||||
}
|
||||
}
|
||||
if(!baseFilename) {
|
||||
// no mapping configured, or it did not match this tiddler
|
||||
// in this case, we fall back to legacy behaviour
|
||||
baseFilename = title.replace(/\//g,"_");
|
||||
}
|
||||
// Remove any of the characters that are illegal in Windows filenames
|
||||
var baseFilename = transliterate(baseFilename.replace(/<|>|\:|\"|\\|\||\?|\*|\^|\s/g,"_"));
|
||||
// Truncate the filename if it is too long
|
||||
if(baseFilename.length > 200) {
|
||||
baseFilename = baseFilename.substr(0,200);
|
||||
@@ -121,6 +151,10 @@ FileSystemAdaptor.prototype.saveTiddler = function(tiddler,callback) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
}
|
||||
var error = $tw.utils.createDirectory(path.dirname(fileInfo.filepath));
|
||||
if(error) {
|
||||
return callback(error);
|
||||
}
|
||||
var typeInfo = $tw.config.contentTypeInfo[fileInfo.type];
|
||||
if(fileInfo.hasMetaFile || typeInfo.encoding === "base64") {
|
||||
// Save the tiddler as a separate body and meta file
|
||||
@@ -181,10 +215,10 @@ FileSystemAdaptor.prototype.deleteTiddler = function(title,callback,options) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
}
|
||||
callback(null);
|
||||
$tw.utils.deleteEmptyDirs(path.dirname(fileInfo.filepath),callback);
|
||||
});
|
||||
} else {
|
||||
callback(null);
|
||||
$tw.utils.deleteEmptyDirs(path.dirname(fileInfo.filepath),callback);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user