|
| 1 | +// Create a GitHub credential |
| 2 | +let githubCredential = Credential.create("GitHub", "Enter your GitHub details for posting to your repository."); |
| 3 | +githubCredential.addTextField("username", "Username"); |
| 4 | +githubCredential.addPasswordField("token", "Personal Access Token"); |
| 5 | +githubCredential.addTextField("repoName", "Repository Name"); |
| 6 | +githubCredential.addTextField("email", "Email Address"); |
| 7 | + |
| 8 | +// Prompt the user for credentials if not already saved |
| 9 | +if (!githubCredential.authorize()) { |
| 10 | + console.log("Authorization failed or was cancelled by the user."); |
| 11 | + context.fail(); |
| 12 | +} |
| 13 | + |
| 14 | +// Extract details from the credentials |
| 15 | +const username = githubCredential.getValue("username"); |
| 16 | +const token = githubCredential.getValue("token"); |
| 17 | +const repoName = githubCredential.getValue("repoName"); |
| 18 | +const email = githubCredential.getValue("email"); |
| 19 | + |
| 20 | +// Prepare your post content with front matter |
| 21 | +const title = draft.content.split("\n")[0].replace('# ', ''); // Assuming the first line of the draft is the title |
| 22 | +const date = new Date().toISOString().split('T')[0]; |
| 23 | +const slug = title.toLowerCase().replace(/\s+/g, '-').replace(/[^\w\-]+/g, '').replace(/\-\-+/g, '-').trim('-'); |
| 24 | +const fileName = `${date}-${slug}.md`; |
| 25 | + |
| 26 | +// Dynamically generate categories from Drafts tags |
| 27 | +const categories = draft.tags.join(', '); |
| 28 | + |
| 29 | +// Construct front matter |
| 30 | +const frontMatter = `--- |
| 31 | +layout: post |
| 32 | +title: "${title}" |
| 33 | +date: ${date} |
| 34 | +categories: [${categories}] // Dynamically generated categories from Drafts tags |
| 35 | +--- |
| 36 | +`; |
| 37 | + |
| 38 | +const fullContent = frontMatter + draft.content.split("\n").slice(1).join("\n"); // Append the rest of the draft content excluding the title |
| 39 | +const encodedContent = Base64.encode(fullContent); |
| 40 | + |
| 41 | +// Setup for the GitHub API request |
| 42 | +const path = `_posts/${fileName}`; |
| 43 | +const apiUrl = `https://api.github.com/repos/${username}/${repoName}/contents/${path}`; |
| 44 | + |
| 45 | +const data = { |
| 46 | + owner: `${username}`, |
| 47 | + repo: `${repoName}`, |
| 48 | + path: `${path}`, |
| 49 | + message: 'Sent from Drafts', |
| 50 | + committer: { |
| 51 | + name: `${username}`, |
| 52 | + email: `${email}` |
| 53 | + }, |
| 54 | + content: encodedContent |
| 55 | +}; |
| 56 | + |
| 57 | +// Create the HTTP request |
| 58 | +let http = HTTP.create(); |
| 59 | +let response = http.request({ |
| 60 | + "url": apiUrl, |
| 61 | + "method": "PUT", |
| 62 | + "headers": { |
| 63 | + "Authorization": `Bearer ${token}`, |
| 64 | + "User-Agent": "DraftsApp", |
| 65 | + "Content-Type": "application/json", |
| 66 | + 'X-GitHub-Api-Version': '2022-11-28' |
| 67 | + }, |
| 68 | + "data": JSON.stringify(data) |
| 69 | +}); |
| 70 | + |
| 71 | +// Process the response |
| 72 | +if (response.statusCode === 200 || response.statusCode === 201) { |
| 73 | + console.log("Successfully created/updated the file on GitHub."); |
| 74 | +} else { |
| 75 | + console.log("Failed to post to GitHub. Status code: " + response.statusCode + " Response: " + response.responseText); |
| 76 | +} |
0 commit comments