Action
Post to Pagecord
Posted by Martin Schuhmann,
Last update
about 8 hours ago
Post to Pagecord
Post the current draft to Pagecord via the Pagecord API.
Setup
- Create an API key on Pagecord: Pagecord → Settings → API.
- Run this action in Drafts.
- When prompted, paste your Pagecord API key.
For more information, please visit:
How It Works
- You can publish posts on Pagecord or save them as drafts.
- The first line of the draft becomes the post title.
- The rest of the draft becomes the post content.
- Drafts tags are sent to Pagecord as post tags.
- The draft content is sent as Markdown.
- Pagecord requires posts to include body content. If the draft only contains a title, the action will display an error.
Steps
-
script
// Post to Pagecord v1.0 // Created by Martin Schuhmann // Post the current draft to Pagecord via the Pagecord API const credential = Credential.create("Pagecord API Key", "Enter your Pagecord API key from Pagecord → Settings → API."); credential.addPasswordField("apiKey", "API Key"); credential.authorize(); const apiKey = credential.getValue("apiKey"); if (!apiKey) { alert("The Pagecord API key is missing. Please enter your Pagecord API key."); context.fail("No Pagecord API key"); } else { const fullContent = draft.content || ""; const firstBreak = fullContent.indexOf("\n"); const hasMultipleLines = firstBreak !== -1; let title = ""; let body = ""; if (hasMultipleLines) { const firstLine = fullContent.substring(0, firstBreak).trim(); const headingMatch = firstLine.match(/^(#{1,6})\s+(.*)$/); title = headingMatch ? headingMatch[2].trim() : firstLine; const rawBody = fullContent.substring(firstBreak + 1); body = rawBody.replace(/^[\s\n\r]+/, ""); } else { body = fullContent.replace(/^[\s\n\r]+/, ""); } if (body.length === 0 && title.length === 0) { alert("This draft has no content."); context.fail("No Draft content"); } else if (title.length > 0 && body.length === 0) { alert("This draft has no content after the draft title."); context.fail("No draft content after draft title"); } else { const prompt = Prompt.create(); prompt.title = "Post to Pagecord"; prompt.message = title ? `Title: ${title}` : "No title"; prompt.addButton("Publish Post"); prompt.addButton("Save Draft"); if (!prompt.show()) { context.cancel(); } else { const status = prompt.buttonPressed === "Publish Post" ? "published" : "draft"; const tagsParam = (draft.tags && draft.tags.length > 0) ? draft.tags.join(",") : ""; const requestData = { content: body, content_format: "markdown", status: status }; if (title.length > 0) { requestData.title = title; } if (tagsParam.length > 0) { requestData.tags = tagsParam; } const http = HTTP.create(); const response = http.request({ url: "https://api.pagecord.com/posts", method: "POST", encoding: "json", data: requestData, headers: { "Authorization": "Bearer " + apiKey, "Content-Type": "application/json" } }); if (response.success) { const msg = status === "published" ? "Published on Pagecord" : "Saved as draft on Pagecord"; draft.addTag("published"); draft.update(); app.displaySuccessMessage(msg); } else { let errorMsg = "Request to Pagecord failed (Status " + response.statusCode + ")"; try { const err = JSON.parse(response.responseText); if (err.error) errorMsg += "\n\n" + err.error; else if (err.errors) errorMsg += "\n\n" + JSON.stringify(err.errors); } catch (e) { if (response.responseText) errorMsg += "\n\n" + response.responseText; } alert(errorMsg); context.fail("HTTP " + response.statusCode); } } } }
Options
-
After Success Default Notification Info Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.