🪴 Scripting Drafts

https://scripting.getdrafts.com/globals

Made a custom Action

I use a custom script to write some js to make my own slug from the body content.

Added a script step with this:

// See online documentation for examples
// https://docs.getdrafts.com/docs/actions/scripting

// https://mhagemann.medium.com/the-ultimate-way-to-slugify-a-url-string-in-javascript-b8e4a0d849e1

function slugify(string) {
  const a = 'àáâäæãåāăąçćčđďèéêëēėęěğǵḧîïíīįìıİłḿñńǹňôöòóœøōõőṕŕřßśšşșťțûüùúūǘůűųẃẍÿýžźż·/_,:;'
  const b = 'aaaaaaaaaacccddeeeeeeeegghiiiiiiiilmnnnnoooooooooprrsssssttuuuuuuuuuwxyyzzz------'
  const p = new RegExp(a.split('').join('|'), 'g')

  return string
    .toString()
    .toLowerCase()
    .replace(/\s+/g, '-') // Replace spaces with -
    .replace(p, (c) => b.charAt(a.indexOf(c))) // Replace special characters
    .replace(/&/g, '-and-') // Replace & with 'and'
    .replace(/[^\w\-]+/g, '') // Remove all non-word characters
    .replace(/\-\-+/g, '-') // Replace multiple - with single -
    .replace(/^-+/, '') // Trim - from start of text
    .replace(/-+$/, '') // Trim - from end of text
}

let titleContent = draft.content.split('\n')[3]
let titleString = titleContent.replace(/(title: )([^\0]+)/g, '$2')

draft.setTemplateTag('slug', slugify(titleString))

Then I need to add that slug as a template tag

Use drafts.setTemplateTag() to make a new scripting-drafts:
https://scripting.getdrafts.com/classes/draft#settemplatetag

I also added a custom location (to my static site for my public notes) using bookmark locations:
https://docs.getdrafts.com/docs/settings/bookmarks

Then added a File step to the action where I set the Name of the file output to scripting-drafts.md and set my location to the bookmark I chose above.

https://docs.getdrafts.com/docs/actions/steps/services#file

I also added a keyboard shortcut to kick of the action and I have the action archive the note in drafts once it is done.

I went back and added an extra script step

// check for WIP tag
if (draft.hasTag("wip")) throw new Error("Can't post things with 'wip' tags")

This will let me tag some posts and keep from accidentally archiving them

Made by Brandon . If you find this project useful you can donate.