Action

Post > Regular

Posted by TheChels.uk, Last update about 19 hours ago

UPDATES

about 19 hours ago

Fixed handling of url encoding to stop drafts being truncated by #

show all updates...

about 19 hours ago

Fixed handling of url encoding to stop drafts being truncated by #

6 days ago

Updated description

6 days ago

Typo

6 days ago

Typo

About

Takes a draft with Jekyll style front matter and creates a file in working copy in the _posts/year/ folder using the date and title to create the filename (yyyy-mm-dd-title.md)

Tip

If you find this useful and want to support me, you can tip at ko-fi.com/thechelsuk

Steps

  • script

    * @title: Jekyll formatting -> Working Copy
     * @author: TheChelsuk
     * @notes: creates jekyll md file
     */
    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 = new Date().toISOString().substr(0, 10);
    
            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('date', 'Date', now);
           
            prompt.addButton('Ok');
            prompt.show();
    
            if (prompt.buttonPressed == "Ok") {
                    titleArr = prompt.fieldValues['title'].split(' '),
                    fileName = now + '-';
    
                // 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 += '\n';
                newDraft += 'layout: post\n'
                newDraft += 'date: ' + prompt.fieldValues['date'] + '\n';
    
                newDraft += 'title: ' + prompt.fieldValues['title'] + '\n';
                newDraft += '\n';
                newDraft += '---\n';
                newDraft += '\n';
                newDraft += content;
    
                // set draft content
                editor.setText(newDraft);
    
                // send to working copy
                var year = new Date().getFullYear().toString(),
                    baseURL = 'working-copy://x-callback-url/write/?key=' +
                              credential.getValue("working-copy-key") +
                              '&repo=' + encodeURIComponent(credential.getValue("jekyll-repo")) +
                              '&path=' + encodeURIComponent(credential.getValue("jekyll-path")) + '/' +  year + '/' +
                              encodeURIComponent(fileName.toLowerCase()) +
                              '&text=' +
                              encodeURIComponent(newDraft),
                    cb = CallbackURL.create();
                cb.baseURL = baseURL;
                cb.open();
            };
        }
    };
    

Options

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