Organise Drafts with Tags and Saved Filters

Drafts is a vital app in my workflow. In former times I used it mainly for jotting down thoughts, but since a few months I got more and more into it and found it to be a great place for all kinds of text based activities.

This caused my drafts list to hold a considerable amount of files, so I welcomed Drafts introducing Saved Filters in version 4.3, which allow to add additional tabs to the list. They are defined by a filter including a text search, which works like using the search field at the top of the draft list.

Due to the search in Drafts being very basic1 it needed some thought to create unique search results. The obvious choice were tags, which are present anyway in the metadata of the supported MultiMarkdown format I'm using in Drafts. There was only a small problem to solve.

On my Mac I define tags without any delimiter or prefix, so they look like common words. Of course this format would not work in Drafts because its text search could not distinguish between a tag or normal content. After some consideration I added a hash as a prefix to each tag, similar to hashtags on social networks2.

If drafts are exported to the Mac, obviously those prefixes need to be removed. Therefore the following script is included as a step3 in the action for the export:

var content = draft.content;
var pattern = /^Tags:\s+#*\w+(,\s*#*\w+)+/mg;

content = content.replace( pattern, function replacer(match){
    return match.replace(/#/g, "");
});

draft.defineTag("content_cleantags", content);

The code looks for a line beginning with Tags: followed by a comma-delimited list of tags each prefixed with a hash, and removes them. The result is the full draft with cleaned tags stored in the draft tag [[content_cleantags]].

Existing tags in drafts can be "hashed" with the following script:

var content = draft.content;
var pattern = /^Tags:\s+#*\w+(,\s*#*\w+)+/mg;

draft.content = content.replace( pattern, function replacer(match){
    return match.replace(/(\s+(?=\w))/g, "$1#").replace(/(,(?=\w))/g, "$1 #").replace(/,#/g, ", #");
});
commit(draft);

The replacement leaves any existing hashes alone and also takes care of a well-formed list.

One more tip: If your number of tabs in the drafts lists is growing you can save some space by naming them with single symbols. Of course this also works for the action list.


  1. No operators, let alone regex. 

  2. Actually the trigger for using a hash came from 1Writer, which supports them out of the box. 

  3. It would be great to have the possibility to use an action as a step in another action. This way you could reuse common tasks like removing the prefix in different export actions.