Get a sanitized filename from a tiddler title

250 views
Skip to first unread message

Marcio Augusto

unread,
May 5, 2018, 10:55:21 AM5/5/18
to TiddlyWiki
Hi,
Anyone knows how to convert the tiddler title to a sanitized string to use as a filename, just as the "export tiddler" button does?
I spent a lot of time searching the group messages and even wading through the source code, and I am just unable to sort this out.
Any help is appreciated.
Cheers,
Marcio.

PMario

unread,
May 5, 2018, 11:27:31 AM5/5/18
to TiddlyWiki
Hi Marcio,

What do you mean with "sanitized". Can you give some examples?

-mario

Marcio Augusto

unread,
May 5, 2018, 11:38:52 AM5/5/18
to TiddlyWiki
Hi Mario,
I would like to create a string
from the tiddler title, replacing any character invalid in a filename (like <>:"\/|?*^) with an underscore, just like the tiddler export button does.
Cheers,
Marcio.

Jed Carty

unread,
May 5, 2018, 11:58:40 AM5/5/18
to TiddlyWiki
There is a function called generateTiddlerBaseFilepath that is part of the filesystem plugin. Starts on line 118 of this file:

https://github.com/Jermolene/TiddlyWiki5/blob/master/plugins/tiddlywiki/filesystem/filesystemadaptor.js

If you have the filesystem plugin active (or the multiuser plugin, it uses the same thing) than the function is accessible as $tw.syncadaptor.generateTiddlerBaseFilepath

Jed Carty

unread,
May 5, 2018, 12:04:22 PM5/5/18
to TiddlyWiki
What I said in my last post only works if you are using it on node and isn't available in the browser. But you could make a javascript macro or widget that uses the same code to have it available in the browser.

Marcio Augusto

unread,
May 5, 2018, 12:24:49 PM5/5/18
to TiddlyWiki
If it would be possible to use something from the standard TW (as I am assuming the "export tiddler" button does), I would like to avoid adding something that would duplicate a function that already exists.

Marcio Augusto

unread,
May 5, 2018, 12:34:08 PM5/5/18
to TiddlyWiki
Anyway, the code you pointed out does exactly what I need. I just could not find it in the standard TW.
Maybe the TW just passes the tiddler title "as is" to the browser, which in turn does the invalid character replacement? Although on Linux, even valid characters (like : and ?) gets replaced by "_".

Mark S.

unread,
May 5, 2018, 2:44:57 PM5/5/18
to TiddlyWiki
It has to make names that will be valid everywhere. Characters ? and : aren't valid in Windows filenames.

Marcio Augusto

unread,
May 5, 2018, 2:57:22 PM5/5/18
to TiddlyWiki
Only if portability is a requirement (which is indeed my case). However, if someone uses TW only on Linux, there would be no need to worry about invalid characters in Windows filenames.
Digressions apart, I would like to find the function that does the character conversion on the filename when the "export tiddler" is used, do you have any sugestions?

Mark S.

unread,
May 5, 2018, 8:36:45 PM5/5/18
to TiddlyWiki
It appears that the export/saving occurs in :

$:/core/modules/savers/download.js

However, it also appears that the conversion to a safe file system name is done by the browser. So it's not available unless there's an equivalent javascript file name conversion function out there somewhere.

Good luck!
-- Mark

Mat

unread,
May 6, 2018, 6:37:41 AM5/6/18
to TiddlyWiki
On Saturday, May 5, 2018 at 6:24:49 PM UTC+2, Marcio Augusto wrote:
If it would be possible to use something from the standard TW (as I am assuming the "export tiddler" button does), I would like to avoid adding something that would duplicate a function that already exists.

There are the uriencode / uridecode filters. Maybe worth a look.

<:-)

Mark S.

unread,
May 6, 2018, 11:31:44 AM5/6/18
to TiddlyWiki
I looked at them. But they don't create the same kind of file name that the browser does when saving exports. In particular, they leave in ? and :, which have to be converted to _ (or something) for saving on Windows.

-- Mark

Marcio Augusto

unread,
May 6, 2018, 6:53:41 PM5/6/18
to TiddlyWiki
It seems this module is for saving the whole wiki as HTML.
Six months ago, I tried to follow the calls in the source code from the "export tiddler" button on, but may have missed the trail at some branch. Spent some more hours on this yesterday, again no luck.
To have this sorted out for now, I shall follow your previous advice and put together a dozen lines Javascript macro, using the code in FileSystemAdaptor.prototype.generateTiddlerBaseFilepath.
Thanks for your kind help!

Mark S.

unread,
May 6, 2018, 7:01:47 PM5/6/18
to TiddlyWiki
I know it looks like it just saves the whole wiki, but if you put in some alerts you'll see that it (somehow) gets called when doing exports. It gets called in "download" mode. But the name is the same garbled tiddler name right up to the download "click" action. Which suggests that it is the browser that does the final rename. 

-- Mark

Jeremy Ruston

unread,
May 8, 2018, 1:16:50 PM5/8/18
to tiddl...@googlegroups.com
I looked at them. But they don't create the same kind of file name that the browser does when saving exports. In particular, they leave in ? and :, which have to be converted to _ (or something) for saving on Windows.

Try using the “encodeuricomponent” filter; it performs the same processing that the core uses for generating filenames.

Best wishes

Jeremy


-- Mark

On Sunday, May 6, 2018 at 3:37:41 AM UTC-7, Mat wrote:
On Saturday, May 5, 2018 at 6:24:49 PM UTC+2, Marcio Augusto wrote:
If it would be possible to use something from the standard TW (as I am assuming the "export tiddler" button does), I would like to avoid adding something that would duplicate a function that already exists.

There are the uriencode / uridecode filters. Maybe worth a look.

<:-)

-- 
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To post to this group, send email to tiddl...@googlegroups.com.
Visit this group at https://groups.google.com/group/tiddlywiki.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/0c1e6466-9e11-44ff-b045-4e1f39749797%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Marcio Augusto

unread,
May 9, 2018, 6:06:42 PM5/9/18
to TiddlyWiki
Thanks for you input.

However, this filter replaces many characters with the equivalent % encoded UTF-8, not only the characters that are invalid as filenames, which I would need replaced by "_".

Cheers,
Marcio.

TonyM

unread,
May 9, 2018, 7:55:27 PM5/9/18
to TiddlyWiki
Marcio,

But then you can save it with the UTF-8 file name such as when dragged and drooped back on tiddlywiki it will restore the original name?

If you really want to simplify the name why not change the title in TiddlyWiki, I know that is obvious but I know how we can go down paths and forget where we came from.

Best Wishes
Tony

Marcio Augusto

unread,
May 9, 2018, 11:34:03 PM5/9/18
to TiddlyWiki
Hi Tony,

I am not trying to export and import tiddlers. What I am trying to achieve is reference image files on disk, which filenames have patterns derived from the tiddler title, except for the invalid characters.

I just mentioned the export function because I noticed that the "export tiddler" button (which offers four formats to export: CSV, JSON, Static HTML and ".tid") issues a browser popup window that allows to save or open the tiddler, where the filename to be saved is the tiddler title, except for the invalid characters for filenames in Windows (< > : " \ ? *) and Linux (/) which are replaced by "_". I am now convinced that, as Mark S. already pointed out, this is probably done by the browser.

Change the tiddler title is not an option, it needs to display any Unicode character except those invalid in tiddler titles. I could include a tiddler field with the "sanitized" tiddler title using an external script (there are hundreds of tiddlers), but this would be an asynchronous process.

To stay in the TW realm, it seems my best bet is the FileSystemAdaptor that Jed Carty suggested in the beginning.

Cheers,
Márcio.

TonyM

unread,
May 10, 2018, 12:10:49 AM5/10/18
to TiddlyWiki
Marcio

I suppose why I forgot that part of your Question is "which filenames have patterns derived from the tiddler title" which suggests you are exporting the titles at least, manually if necessary. My Question I suppose is you can rule the name of the images so why do you not force them to comply?, No Need to answer unless it helps you

Yes Follow Jeds suggestion he has more knowledge on this than myself.

Regards
Tony

Marcio Augusto

unread,
May 10, 2018, 10:38:08 PM5/10/18
to TiddlyWiki
Hi Tony,

Having slept over your considerations, I concur that the image files could be renamed sequentially, for instance, and in this case a new tiddler field would have to be created to hold the image filename, thankfully with no relation to the tiddler title. The drawback is loosing the ability to search and sort the images outside of TW (e.g., in a file browser/explorer) using the filename.

Another approach would be referencing the original files in the respective application directory (folder) structure (I am using TW to create an alternative presentation of this application data and images), but in this case I would need to build another TW, which is not that worrisome, as it would be an opportunity to learn more.

Regards,
Márcio.

Marcio Augusto

unread,
Jun 19, 2018, 6:55:59 PM6/19/18
to TiddlyWiki
Hi Jed, Mark, Jeremy and Tony,

Well, I finally did it. Created a macro borrowing a couple of lines from the FileSystemAdaptor.prototype.generateTiddlerBaseFilepath you suggested.

Although it does what I needed, I realized it is not as general as one could wish, since different tiddler titles could have the same "sanitized" result, so for other uses one would need to extend it with additional code, or treat the resulting string outside of the macro.

This drawback considered, should it be of interest to someone else, where would it be better to post it?

Regards,
Marcio.
Reply all
Reply to author
Forward
0 new messages