Action

Post > Quote

Posted by thechelsuk, Last update 1 day ago

UPDATES

about 1 month ago

Remove prompt for the url and citation, this script looks for content that matches a url and an author.

Updating the share capture template makes these easier to use.

show all updates...

about 1 month ago

Remove prompt for the url and citation, this script looks for content that matches a url and an author.

Updating the share capture template makes these easier to use.

about 1 month ago

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

about 2 months ago

Updated description

About

Prompts user for title, source, author and creates a Jekyll quote post in working copy in the _posts/year folder using the date and title to create the filename. This adds the relevant front matter.

Use a template that includes the following.

Source: <url>
Author: <name)

Tip

If you find this useful and want to support me, you can tip at ko-fi.com/thechelsuk and check out Links for apps, tools, and more.

Steps

  • script

    /*
     * @title: Regular Post to Working Copy
     * @author: TheChelsUk
     * @notes: Create markdown blog post in Working Copy
     * Extracts title from first line, source: and author: from bottom of draft
     */
    
    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 content = draft.content,
                date = new Date(),
                now = new Date().toISOString().substr(0, 10),
                lines = content.split('\n'),
                title = '',
                source = '',
                author = '',
                cleanedContent = '';
    
            // Extract title from first line (remove # if present)
            if (lines.length > 0) {
                title = lines[0].replace(/^#+\s*/, '').trim();
            }
    
            if (!title) {
                alert("No title found on first line of draft");
                context.cancel();
            }
    
            // Extract source and author from bottom of draft
            var sourceMatch = content.match(/source:\s*(.+?)$/mi);
            var authorMatch = content.match(/author:\s*(.+?)$/mi);
    
            if (sourceMatch) {
                source = sourceMatch[1].trim();
            }
    
            if (authorMatch) {
                author = authorMatch[1].trim();
            }
    
            // Remove title, source, and author lines from content
            cleanedContent = content;
            
            // Remove first line (title)
            cleanedContent = cleanedContent.replace(/^.*\n/, '');
            
            // Remove source: line
            cleanedContent = cleanedContent.replace(/source:\s*.+$/mi, '');
            
            // Remove author: line
            cleanedContent = cleanedContent.replace(/author:\s*.+$/mi, '');
            
            // Clean up extra whitespace
            cleanedContent = cleanedContent.trim();
    
            // Create filename from title
            var titleArr = title.split(' '),
                fileName = now + '-';
    
            titleArr.map((t) => fileName += `${t}-`);
            fileName = fileName.replace(/-$/, '');
            fileName += '.md';
    
            // Assemble post frontmatter
            var newDraft = '';
            newDraft += '---\n';
            newDraft += '\n';
            newDraft += 'layout: post\n';
            newDraft += 'date: ' + now + '\n';
    
            if (source !== '') {
                newDraft += 'link: ' + source + '\n';
            }
    
            newDraft += 'title: ' + title + '\n';
            
            if (author !== '') {
                newDraft += 'cited: ' + author + '\n';
            }
            
            newDraft += '\n';
            newDraft += '---\n';
            newDraft += '\n';
            newDraft += cleanedContent;
    
            // 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;
            
            if (cb.open()) {
                app.displaySuccessMessage('Post created: ' + fileName);
            } else {
                app.displayErrorMessage('Failed to send to Working Copy');
                context.fail();
            }
        }
    }
    

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.