Action

New Linked Draft

Posted by agiletortoise, Last update 5 months ago

Create new draft linked to the current draft.

Action prompts for the title for the new draft, and tags to assign (defaulting to tags of current draft). It will create that new draft based on the template defined in the script (which can contain template tags) - and insert a [[Title of Draft]] style link in the current draft at the cursor location.

The new draft template can contain a back link to the current draft.

Steps

  • script

    // alter this template variable for the format of the new draft
    // use [[backlink]] where you want the back link to the current draft inserted
    // [[date]] and other tags can be used and will be evaluated as well
    let template = `# [[title]]
    
    < [[backlink]]`;
    
    template = template.replace("[[backlink]]", `[[${draft.displayTitle}]]`);
    
    let f = () => {
    	// prompt for title and tags
    	let p = Prompt.create();
    	p.title = "Create Linked Draft"
    	p.message = "Create a new draft with the title provide and a back link to the current draft, then insert a link to the new draft at the current cursor position."
    	
    	p.addTextField("title", "Title", "");
    	p.addTextField("tags", "Tags", draft.tags.join(", "));
    	p.addButton("Create");
    	
    	if (!p.show()) { return false; }
    	
    	// create the new linked draft
    	let d = Draft.create();
    	d.content = p.fieldValues["title"];
    	d.content = d.processTemplate(template);
    	let tags = p.fieldValues["tags"].split(",").map(s => s.trim());
    	for (let tag of tags) {
    		d.addTag(tag);
    	} 
    	d.update();
    	
    	// insert link to new draft in current draft
    	let newTitle = d.displayTitle;
    	let linkText = `[[${newTitle}]]`;
    	let [st, len] = editor.getSelectedRange();
    	editor.setSelectedText(linkText);
    	editor.setSelectedRange(st+linkText.length, 0);
    	
    	return true;
    }
    
    if (!f()) {
    	context.cancel();
    }

Options

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