Action

Tags in contents ➡ metadata tags, with cleanup

Posted by al3x, Last update 4 months ago

Finds tags in draft contents, adds those tags to draft metadata, then removes the tags from draft contents.

Perfect for processing documents imported from Bear or other apps that commingle tags with text.

Steps

  • script

    /*
    Finds tags in draft contents, adds those tags to draft
    metadata, then removes them from draft contents.
    
    Based on https://actions.getdrafts.com/a/1Q7
    */
    
    // Get all the tagged drafts in the inbox.
    const taggedDrafts = Draft.query("", "inbox");
    
    // Find tags in draft content.
    const tagRegex = /#[\w\d]+/g;
    
    for (var i=0; i < taggedDrafts.length; i++) {
    	var thisDraft = taggedDrafts[i];
    
    	// Add found tags to draft metadata.
    	var tags = thisDraft.content.match(tagRegex);
    	for (tag in tags) {
    		thisDraft.addTag(tags[tag].substring(1));
    	}
    
    	// Remove found tags from draft content.
    	thisDraft.content = thisDraft.content.replace(tagRegex,"");
    	thisDraft.content = thisDraft.content.trim();
    	
    	// Save changes to drafts database.
    	thisDraft.update();
    }

Options

  • After Success Nothing
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.