Action

Working Copy Commit (public)

Posted by rexarski, Last update about 10 hours ago

One-click solution to share a draft to iOS/iPadOS Working Copy directory with pre-defined format.
The script shares the Draft content as a Hugo blog post Markdown file with frontmatters.

Three variables need to be updated manually in the script:

  • MYWORKINGCOPYKEY which can be found in Working Copy’s settings,
  • MYREPO which is the name of the repository, and
  • MYPATH which is the full path within the repo to the target file

Steps

  • script

    // The following variables need to be modified in the script:
    // MYWORKINGCOPYKEY
    // MYREPO
    // MYPATH
    
    // Prepare metadata
    let title = draft.processTemplate("[[title]]");
    let bodyContent = draft.processTemplate("[[body]]");
    function pad(n){ return n<10 ? "0"+n : n; }
    let now = new Date();
    let yyyy = now.getFullYear(), MM = pad(now.getMonth()+1), dd = pad(now.getDate());
    let hh = pad(now.getHours()), mm = pad(now.getMinutes()), ss = pad(now.getSeconds());
    // Timezone offset like -04:00
    let tzOffset = -now.getTimezoneOffset();
    let tzSign = tzOffset >= 0 ? "+" : "-";
    let tzHours = pad(Math.floor(Math.abs(tzOffset)/60));
    let tzMins = pad(Math.abs(tzOffset)%60);
    let dateTime = `${yyyy}-${MM}-${dd}T${hh}:${mm}:${ss}${tzSign}${tzHours}:${tzMins}`;
    // Slug
    let safeTitle = draft.processTemplate("[[safe_title]]");
    let slug = safeTitle.replace(/ +/g, "-").toLowerCase();
    // If the title contains non-Latin characters, prompt the user for a slug:
    if (!/^[0-9a-z-]+$/.test(slug)) {
    	let p = Prompt.create();
    	p.title = "Enter slug for the post";
    	p.addTextField("slug", "Slug (a-z, 0-9, -)", "");
    	p.addButton("OK"); // add this line!
    	if (p.show()) { 
    		slug = p.fieldValues["slug"]; 
    	} else { 
    		context.cancel(); 
    	}
    }
    // Tags
    let tags = draft.tags;
    let tagsYaml = tags.length ? `tags: [${ tags.map(t=> `"${t}"`).join(", ") }]` : "";
    
    // Construct the .md file with front matter
    let frontMatter = 
    `---
    title: "${title}"
    date: ${dateTime}
    slug: "${slug}"
    ${ tagsYaml ? tagsYaml + "\n" : ""}---`;
    let fullContent = frontMatter + "\n\n" + bodyContent;
    
    // Save the file to a Working Copy Folder via x-callback-url
    app.setClipboard(fullContent);
    let cb = CallbackURL.create();
    // the key has to be consistent across devices
    cb.baseURL = `working-copy://x-callback-url/write?key=MYWORKINGCOPYKEY&repo=MYREPO&clipboard=yes&mode=safe&no_text=empty&path=MYPATH/${yyyy}-${MM}-${dd}-${slug}.md`
    
    // Open the callback URL
    let success = cb.open();
    if (!success) {
    	console.error(cb.status + ": " + cb.callbackResponse);
    	context.fail(); // stop if write failed
    }
    

Options

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