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.
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
Category
true
John Doe
The rest of my content...
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.
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.