Skip to content

Instantly share code, notes, and snippets.

@jasonekratz
Last active June 26, 2020 00:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonekratz/795ce9b6135be80692e1db3983d316ef to your computer and use it in GitHub Desktop.
Save jasonekratz/795ce9b6135be80692e1db3983d316ef to your computer and use it in GitHub Desktop.
Action for Drafts App, https://getdrafts.com , to update the applied tags to match a line of hashtags on the last line of the draft. Also in the Drafts Directory here https://actions.getdrafts.com/a/10l
let lastLine = draft.lines[draft.lines.length- 1];
if(lastLine.startsWith('#')) {
let re = /#([\w]+)/g;
let hashtags = [...lastLine.matchAll(re)];
let newtags = []
// because matchAll is returning both the full hashtag and the text just get the text
hashtags.forEach(hashtag => {
newtags.push(hashtag[1]);
});
// add any tags that don't yet exist
newtags.forEach(tag => {
if(!draft.hasTag(tag)) {
draft.addTag(tag);
}
});
// remove any tags that aren't in the list
draft.tags.forEach(tag => {
if(!newtags.includes(tag)) {
draft.removeTag(tag);
}
});
draft.update();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment