Action

Send to Obsidian with YAML

Posted by CuriousRabbit, Last update over 3 years ago

UPDATES

over 3 years ago

Version 1.0.1

Add ability to send all drafts to a consistent location within the target vault. This effectivly creates a special “inbox” for all drafts sent to Obsidian.

Add option of creating the Obsidian file silently, i.e.,
not opening the new file or bringing it to the forground.

show all updates...

over 3 years ago

Version 1.0.1

Add ability to send all drafts to a consistent location within the target vault. This effectivly creates a special “inbox” for all drafts sent to Obsidian.

Add option of creating the Obsidian file silently, i.e.,
not opening the new file or bringing it to the forground.

over 3 years ago

Restructure things to allow for complete customization of the YAML front matter. Tags are always put in YAML form but other metadata can be defined as the user needs. By default the standard document metadata of Title, Author, Subject, Date, and Source are predefined. But, any or all of these can be changed or deleted. Or, more metadata can be added. Changed commentary in the code to explain the how-to of this. Hopefully a friendlier, more flexible tool for those not so comfortable with coding skills.

over 3 years ago

Updated 2021-08-12

  • Switch to using the draft creation date as the metadata date rather than the current calendar date.
  • Clarify and amplify the commentary in the code for customization.

Send a draft to Obsidian as a new note with YAML metadata as front matter. The tags and title metadata from the draft are carried over to the Obsidian note as front matter.

Other YAML metadata keys are optionally provided. I tried to structure things such that code neophytes can modify this to their purposes without too much trouble.

The YAML keys incorportated here are what I thought would be reasonable meta data for a document, ala PDF or Word. Your needs and mileage may vary.

Hopefully some people wil find this helpful. Email scripts@creeds.cloud with bugs or suggestions.

Steps

  • script

    /* 
     Ver 0.9.0 Send Draft.app draft to Obsidian with YAML front matter. 2021-08-08
     Ver 0.9.1 Correct a copy/paste error causing runtime exception. 2021-08-09
     Ver 0.9.2 Switch YAML date to draft creation date. Clarify comments 2021-08-12
     Ver 1.0.0 Restructure to turn all YAML, except the tags, user configurable
    			text. Makes configuration of custom YAML possible. 2021-08-18
     Ver 1.0.1 Add "inbox" folder option and createSilently option. 2021-08-21 
     
     From a direction pointed to in Ulysses sheet w/ tags action.
     Thanks to Olivier Spinnler (@OlivierPS) and others. 
    
     Metadata lines and tags are formatted as YAML front matter then appended 
     with the Draft text. The combined text is then sent as a new note to your
     Obsidian vault. The text of the Draft remains unaltered by this code.
     
     
     >>>>>>>> NOTE: Configurable items to set below <<<<<<<<
     
     MANDATORY: Modify vaultName with the name of your target vault.
     This action won't work without a correct name for an existing vault.
     
     */ 
     
    const vaultName 		= "your-vault-name-here";
    
     /***NEW*** The transfered note will be located either where Obsidian settings 
    	say new files shoud go, OR, in a "inbox" folder you configure below.
    
     Examples:
     
     	inFolder = "" will rely on the Obsidian settings for where to put the file.
     	It should be noted that Obsidian's "Same folder as current file" setting
     	will depend on the currently open Obsidian file. The "current file" context
     	exists even when Obsidian is not running. If a file was open on quit, that
     	is the current file. If no file is/was open, Obsidian will locate the new 
     	file at the top level of the vault as there is no other possible context  
     	for "current file". 
     
     	inFolder = "From-Drafts" will put the note in a "From-Drafts" folder which
    	is assumed be in the top level of the vault. If "From-Drafts" does not
     	currently exist, it will be created. 
     
    	inFolder = "gathered/Drafts" will place the note in the "Drafts" subfolder
     	of the "gathered" folder. Those folders will be created if they're not
     	already there.
     */
     
    const inFolder = "";
    
    /***NEW***  createSilently determines whether the Draft should silently create
       the new Obsidian file in the vault, or, alternatively, should the new file 
       be opened and brought to the front. In either case Obsidian will be made the
       active application.
    */
    
    const createSilently = false;
    
    /*
       Any one of the following headerItems can be changed to suit your needs.
       The label, e.g. "Author", gets combined with its value, e.g. "Mr. Magoo",
       to form a YAML metadata line, e.g., "Author: Mr. Magoo"
    
       Values do not have to be literals, they can be calculated as shown
       in the default Title or Date lines below. E.G., if you wanted to have
       the default Date item be todays date, use getISOShortDate(new Date)
       For attributes of the draft, see https://scripting.getdrafts.com/classes/draft
    
    	I chose these data as reasonable metadata for a document, ala PDF or 
    	Word. Your needs and mileage may vary. I tried to make it easy for those
    	uncomfortable with coding to change things to meet their requirements.
    
       Any or all of the frontmatter lines can be deleted if they're of no 
       value to you. Be sure to leave the lines "let headerItems = [" and "];"
    
       Conversely, you can add any new lines you need by following the pattern. 
    
    */ 
    
    let headerItems = [
    		{ "label": "Title", 	"value": (draft.displayTitle) },
    		{ "label": "Author", 	"value": "Mr. Magoo" },
    		{ "label": "Subject", "value": "" },
    		{ "label": "Date", 		"value": (getISOShortDate(draft.createdAt)) },
    		{ "label": "Source", 	"value": "Drafts" },
    ];
    // >>>>>>>>>>> End of configuration variables <<<<<<<<<<<<
    
    
    const obsidianURL = "obsidian://new";
    let cb = CallbackURL.create();
    cb.baseURL = obsidianURL;
    cb.addParameter( "vault", vaultName );
    if (createSilently) 
    {
    	cb.addParameter( "silent", createSilently );  
    }
    if (inFolder == "") {
    	cb.addParameter( "name", draft.displayTitle );
    } 
    else 
    {
    	cb.addParameter( "file", (inFolder + "/" + draft.displayTitle) )
    }
    
    // Build CSV list of Drafts tags, spaces in tags changed to '_'
    let tags = draft.tags;
    let noteTags = "";
    for (let i = 0; i < tags.length; i++) {
    	let nstag = tags[i].replaceAll( ' ', '_');
    	noteTags += " " + nstag + ",";
    }
    
    // Delete the trailing comma from the tags. YAML doesn't care but it's ugly.
    noteTags = noteTags.substring( 0, noteTags.length - 1 );
    
    // Format YAML front matter
    let frontMatter = buildYAMLBlock( headerItems, noteTags );
    
    // Add the fully composed content to the note and go.
    cb.addParameter( "content", frontMatter + draft.content );
    cb.waitForResponse = false;
    var success = cb.open();
    
    // TA DA! Our work is done here.
    
    // Utility functions:
    
    // YAML composition function. 
    function buildYAMLBlock( metadataItems, metadataTags ) {
    	
    	let yaml = "---\n";
    	for (let i = 0; i < metadataItems.length; i++) {
    		yaml += metadataItems[i].label + ": " + metadataItems[i].value + '\n'
    	}
    	// Tags always go out last.
    	yaml += "tags: [" + metadataTags + " ]\n";
    	yaml += "---\n";
    	return yaml;
    }
    
    // Format a date string in yyyy-mm-dd format
    function getISOShortDate(date) {
    	const month = `${date.getMonth() + 1}`.padStart(2, "0")
    	const day = `${date.getDate()}`.padStart(2, "0");
    	return date.getFullYear() + "-" + month + "-" + day;
    }

Options

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