Action

Jekyll formatting

Posted by Cory Dransfeldt, Last update over 5 years ago

This is perhaps a proof of concept, more than anything and contains fields specific to my personal site, but it should work as a functional guide for generating a workflow that works for yours. The intent is for it to simplify writing to a Jekyll blog from mobile using Drafts by handling post front-matter formatting and file name creation behind the scenes.

Steps

  • script

    /*
     * @title: Jekyll formatting -> Working Copy
     * @author: Cory Dransfeldt
     * @email: cory@coryd.me
     * @notes: This is perhaps a proof of concept, more than anything and contains fields specific to my personal site,
     * but it should work as a functional guide for generating a workflow that works for yours. The intent is for it to
     * simplify writing to a Jekyll blog from mobile using Drafts by handling post front-matter formatting and file name
     * creation behind the scenes.
     */
    var credential = Credential.create("Jekyll path", "Jekyll path");
    credential.addTextField("jekyll-repo", "Jekyll repo name");
    credential.addTextField("jekyll-path", "Path to your jekyll posts directory");
    credential.addTextField("working-copy-key", "Working Copy x-url-callback key");
    
    var result = credential.authorize();
    
    if (!result) {
    	alert("Failed to obtain required Jekyll data. Please check it and try again.");
    	context.cancel("Failed to obtain required Jekyll data. Please check it and try again.");
    } else {
        if ((typeof(credential.getValue("jekyll-repo")) === 'undefined' || String(credential.getValue("jekyll-repo")).length === 0) ||
            (typeof(credential.getValue("jekyll-path")) === 'undefined' || String(credential.getValue("jekyll-path")).length === 0) ||
            (typeof(credential.getValue("working-copy-key")) === 'undefined' || String(credential.getValue("working-copy-key")).length === 0)) {
    		alert("Repo values are invalid. Please rerun action and enter token again");
    		credential.forget();
    		context.cancel("Repo values were invalid.");
    	} else {
            var newDraft = '',
            content = draft.content,
            prompt = Prompt.create(),
            date = new Date(),
            now = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes());
    
            prompt.title = 'Jekyll post data';
            prompt.message = 'Enter Jekyll post data';
            prompt.isCancellable = true;
    
            if (draft.title.length === 0 || typeof(draft.title.length) === 'undefined') {
                prompt.addTextField('title', 'Title', '');
            } else {
                prompt.addTextField('title', 'Title', draft.title);
            };
    
            prompt.addTextField('link', 'Link', '');
            prompt.addTextField('date', 'Date', now);
            prompt.addTextField('categories', 'Categories', '');
            prompt.addTextField('tags', 'Tags', '');
    
            prompt.addButton('Ok');
            prompt.show();
    
            if (prompt.buttonPressed == "Ok") {
                var tags = prompt.fieldValues['tags'].split(' ');
                    categories = prompt.fieldValues['categories'].split(' '),
                    titleArr = prompt.fieldValues['title'].split(' '),
                    fileName = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + '-';
    
                // modify the filename to be yyyy-mm-dd-title.md
                titleArr.map((t) => fileName += `${t}-`);
                fileName = fileName.replace(/-$/, '');
                fileName += '.md';
    
                // remove the file name from the draft
                content = content.replace(prompt.fieldValues['title'], '').trim();
                
                // assemble post frontmatter
                newDraft += '---\n';
                newDraft += 'layout: post\n'
                newDraft += 'date: ' + prompt.fieldValues['date'] + '\n';
    
                if (prompt.fieldValues['link'] !== '')
                    newDraft += 'link: ' + prompt.fieldValues['link'] + '\n';
    
                newDraft += 'title: ' + "'" + prompt.fieldValues['title'] + "'" + '\n';
                newDraft += 'category:\n';
                categories.map((cat) => newDraft += `- ${cat}\n`);
                newDraft += 'tags:\n'
                tags.map((tag) => newDraft += `- ${tag}\n`);
                newDraft += '---\n';
                newDraft += content;
    
                // set draft content
                editor.setText(newDraft);
    
                // send to working copy
                var baseURL = 'working-copy://x-callback-url/write/?key=' + 
                              credential.getValue("working-copy-key") + 
                              '&repo=' + encodeURI(credential.getValue("jekyll-repo")) + 
                              '&path=' + encodeURI(credential.getValue("jekyll-path")) + 
                              '/' + 
                              encodeURI(fileName.toLowerCase()) + 
                              '&text=' + 
                              encodeURI(newDraft),
                    cb = CallbackURL.create();
    
                cb.baseURL = baseURL;
                cb.open();
            };
        }
    };

Options

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