Action

Meta Data < > Draft

Posted by agiletortoise, Last update almost 3 years ago

UPDATES

almost 3 years ago

Fix problem locating original draft link in meta draft.

Example action demonstrating how one could auto-create related meta data drafts to enter notes about the current draft, and flip back and forth to that meta data draft using an action or keyboard shortcut.

The first time this action is run on a draft, it will create a new draft with the tag metadata, filed in the archive, with front matter like:

# Draft Meta Data

Title: <Draft Title>
Source: <Link to Source Draft>

You can then enter notes about the draft in this metadata note.

If this action is run again from the meta data note, it will look for and open the source draft. If you are on the source draft, and a meta data draft exists already, it will navigate to it for adding additional notes.

In use, assigning a keyboard shortcut to this action would easily allow you to toggle back and forth between the draft and it’s meta data.

The meta data note, once created, could also be opened in a separate window for editing next to the source draft on iPad or Mac.

Steps

  • script

    let metadataTemplate = `# Draft Meta Data
    
    Title: ${draft.title}
    Source: ${draft.permalink}
    
    -----
    
    `;
    let metadataTag = "metadata";
    
    // util functions
    function findMetadataDraft(d) {
    	let q = Draft.query(draft.uuid, "archive", [metadataTag]);
    	if (q.length > 0) { return q[0]; }
    	return null
    }
    
    function findSourceDraft(d) {
    	let re = /drafts:\/\/open\?uuid=([^\s]*)/;
    	let match = re.exec(d.content);
    	if (match) {
    		let uuid = match[1];
    		return Draft.find(uuid);
    	}
    	return null
    }
    
    // do the work
    let isMetadataDraft = draft.hasTag(metadataTag);
    if (isMetadataDraft) {
    	// this is a metadata draft, navigate to source draft
    	let source = findSourceDraft(draft)
    	if (source) {
    		editor.load(source);
    		editor.activate();
    	}
    	else {
    		alert("Unable to locate source draft");
    	}
    }
    else {
    	// this is a source draft, find or create metadata draft
    	let metadata = findMetadataDraft(draft);
    	if (metadata) {
    		editor.load(metadata);
    		editor.activate();
    	}
    	else { // need to create metadata draft
    		let m = Draft.create();
    		m.content = metadataTemplate;
    		m.addTag(metadataTag);
    		m.isArchived = true;
    		m.update();
    		editor.load(m);
    		editor.activate();
    	}
    }

Options

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