Action

Cross-link to existing/new draft

Posted by @jsamlarose, Last update about 4 years ago - Unlisted

Inserts a new [[“title”]] template tag at point of cursor in current draft.

Uses the app.selectDraft() UI to select a target draft for linking.

If you select a draft that already exists, a link to the current draft will be pushed to the clipboard, ready to be pasted at an appropriate point in the draft.

Cancel draft selection to create a new draft (with a link to the new draft at point of cursor in the current draft, and a link back to the current draft under the title of the new draft.

New draft is titled with a timestamp (yearmonthday-hourminuteseconds).

As far as I currently understand it, adding descriptive text to the title in the new draft doesn’t impact the functionality of the link to it from the original draft.

Steps

  • script

    let parentD = draft
    
    let f = () => {
    	
    	// Select an existing draft...
    	
    	let d = app.selectDraft();
    	
    	// ...or if you want to create a new draft, hit cancel...
    	
    	if (!d) {
    		
    		// no existing draft, so create one
    		// change the "title" variable to reflect your preferred format for a time-stamp, or whatever logic you'd prefer to use to generate titles. I'd recommend keeping a timestamp to ensure titles are unique, but this could be developed to recognise whether text has been selected in the current draft (parentD) and add that to the title (that selected text would essentially be turned into a back-link...
    		
    		let newD = Draft.create();
    		
    		title = new Date().toString("yyyyMMdd-HHmmss")
    		body = ">>> [[" + parentD.displayTitle + "]]"
    		newD.content = "# " + title + "\n".repeat(1) + body
    		
    		// insert a backlink to the new draft at cursor in current draft...
    		
    		editor.setSelectedText("[["+newD.displayTitle+"]]")
    		
    		parentD.update();
    		newD.update();
    		
    		// load the new draft
    		
    		editor.load(newD)
    		return false;
    	}
    	
    	// insert title from selected draft as link at cursor
    	
    	editor.setSelectedText("[["+d.displayTitle+"]]")
    	parentD.update()
    	app.setClipboard("[["+parentD.displayTitle+"]]")
    	// draft already exists, so backlink is copied to clipboard, allowing you to decide where to place it.
    	editor.load(d)
    	return true;	
    }
    
    if (!f()) {
    	context.cancel();
    }

Options

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