Specify Draft Title

I’ve created an action that will export a Draft to a folder. I’d like to specify the file name based on the date and the title, which are both fairly straightforward using the [[date|format]] and [[safe_title]] tags. However, my Drafts all have a YAML header and no real title because the first line is three dashes; which are used to define the YAML data.

Is there a way to have the [[safe_title]] tag evaluate something other than the first line or some other workaround for this? The title is actually defined in the YAML header and I would prefer if it could grab that, but I’m open to any ideas.


There’s a way to define tags in a JavaScript action, so if you tell us where the title is within the YAML (an example or two would be nice), one or more of us can help you with that step, and then you can finish it with the template you want.

Sure. Here’s the YAML header:

title: Title Goes Here
category: Category
draft: true
author: John Doe

I thought there was a [lines] or [line] or similar template element.

Hi Martin. There’s a line tag, but I would still need a way to change it to a string that’s acceptable for a file name. Am I missing something?

1 Like

So you’d want (a new) [safeLine], I guess. Doesn’t sound hard to do.

There a few ways you could attack this…

My first suggestion would be to just include your file name as the first line of the draft, so [[safe_title]] will work for that…and in the action that writes the file, use [[body]] which will start on the second line, which would be your front matter. This would also give you a friendly name in the draft list.

You could take that a few steps further, and just write your draft without the YAML and let the action construct it for you. So, a draft like:

Title Goes Here
John Doe

The rest of my content...

And a template to write the file like:

title: [[title]]
category: [[line|2]]
draft: [[line|3]]
author: [[line|4]]


Thanks. Your first suggestion is such an elegant solution; and almost perfect.

I was under the impression [[safe_title]] would replace spaces. Is this possible? I need to use hyphens instead.

I use a function like this to make the filenames for my blog posts:

function cleanTitle(title) {
	var clean = title.toLowerCase();
	clean = clean.replace(/[^a-z0-9_\s]/g, '');
	clean = clean.replace(/\s+/g, '-');
	return clean;

In addition to converting spaces to hyphens, it makes the letters lowercase and gets rid of everything other than letters, numerals, and underscores. That may be more changes than you want, but it’s easy to adjust.

Exactly what you feed into it will depend on how your drafts are formatted. It sounds like you’re considering switching to something other than the YAML you’ve been using.

Thanks. Looks like I need to read up on scripting.

I’m not saying you don’t, but remind me why you need to use hyphens rather than spaces. Certainly on Mac OS file names can have spaces - though on the command line you’d need to backslash escape them.

I would guess it is the format used by a publishing system. Jekyll for example uses hyphens to replace spaces.