Repository navigation
Expand file tree
/
Copy pathaction - Super Post Maker - Script.js
More file actions
238 lines (215 loc) · 8.28 KB
/
Copy pathaction - Super Post Maker - Script.js
File metadata and controls
238 lines (215 loc) · 8.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* @title: Super Post Maker
* @author: thechelsuk
* @version: 4.5.0
* @notes: Create markdown blog post in Working Copy.
* Posts always go to _posts/[year]/.
* Supports blog, quote, rss, til, ways, mixtapes, micro as post types via front matter.
* mixtapes: skips front matter assembly and sends raw draft content as-is.
* quote: requires Link and/or Cited metadata in the draft body.
* Extracts Link, Cited, and Date from draft content automatically if present.
* syndicate is an array (mastodon, bluesky, rss, indie) rendered as an
* indented YAML list in front matter, e.g.:
* syndicate:
* - mastodon
* - bluesky
*/
var credential = Credential.create("Jekyll Post Path", "Jekyll Post Path");
credential.addTextField("jekyll-repo", "Jekyll repo name");
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 (
!credential.getValue("jekyll-repo") ||
String(credential.getValue("jekyll-repo")).length === 0 ||
!credential.getValue("working-copy-key") ||
String(credential.getValue("working-copy-key")).length === 0
) {
alert(
"Repo values are invalid. Please rerun action and enter credentials again.",
);
credential.forget();
context.cancel("Repo values were invalid.");
} else {
var date = new Date(),
year = date.getFullYear().toString(),
dateForFilename = date.toISOString().slice(0, 10),
nowWithTime = date.toISOString().slice(0, 16).replace("T", " ");
var rawContent = draft.content,
linkMatch = rawContent.match(/^Link:\s*(.+)$/m),
citedMatch = rawContent.match(/^Cited:\s*(.+)$/m),
dateMatch = rawContent.match(/^Date:\s*(.+)$/m),
extractedLink = linkMatch ? linkMatch[1].trim() : "",
extractedCited = citedMatch ? citedMatch[1].trim() : "",
extractedDate = dateMatch ? dateMatch[1].trim() : "";
var content = rawContent
.replace(/^Link:.*$/m, "")
.replace(/^Cited:.*$/m, "")
.replace(/^Date:.*$/m, "")
.trim();
var frontMatterDate =
extractedDate.length > 0 ? extractedDate : nowWithTime;
var prompt = Prompt.create();
prompt.title = "Jekyll post data";
prompt.message = "Choose Post Type";
prompt.isCancellable = true;
var titleDefault = draft.title || "";
prompt.addTextField("title", "Title", titleDefault);
prompt.addSelect(
"postType",
"Post type",
[
"blog",
"quote",
"rss",
"til",
"ways",
"mixtapes",
"micro",
"gift",
"mailbox",
"film",
],
["blog"],
false,
);
prompt.addSwitch("pinned", "Pinned", false);
prompt.addSelect(
"syndicate",
"Syndicate to",
["mastodon", "bluesky", "rss", "indie", "textlog", "devnull"],
["mastodon", "bluesky"],
true,
);
prompt.addSwitch("hide", "Hide", true);
prompt.addButton("Ok");
prompt.show();
if (prompt.buttonPressed === "Ok") {
var postType = prompt.fieldValues["postType"][0],
titleVal = prompt.fieldValues["title"],
pinnedVal = prompt.fieldValues["pinned"],
syndicateArr = prompt.fieldValues["syndicate"] || [],
hideVal = prompt.fieldValues["hide"],
linkVal = extractedLink,
citedVal = extractedCited,
newDraft = draft.content;
// Helper to create a safe slug from a title
function makeSlug(str) {
return str
.replace(/\//g, "-")
.toLowerCase()
.replace(/:/g, "-")
.replace(/ - /g, "-")
.replace(/\s+/g, "-")
.replace(/[^a-z0-9\-]/g, "")
.replace(/-+/g, "-")
.replace(/^-|-$/g, "");
}
// Helper to create a safe filename for all post types
function makeDatedFilename(title, date) {
var slug = makeSlug(title);
return date + "-" + slug + ".md";
}
// For mixtapes, extract the value from the 'title:' line in the front matter if present
function extractMixtapeTitle(content) {
var lines = content.split(/\r?\n/);
var inFrontMatter = false;
for (var i = 0; i < lines.length; i++) {
var line = lines[i].trim();
if (line === "---") {
if (!inFrontMatter) {
inFrontMatter = true;
} else {
break;
}
} else if (
inFrontMatter &&
line.toLowerCase().startsWith("title:")
) {
// Extract everything after 'title:' and trim
return line.substring(6).trim();
}
}
return null;
}
if (postType === "quote" && linkVal === "" && citedVal === "") {
alert(
"Quote posts require Link and/or Cited data in the draft body. Please correct the draft or change the post type.",
);
context.cancel("Quote post missing link/cited data.");
} else {
var mixtapeTitle = null;
if (postType === "mixtapes") {
mixtapeTitle = extractMixtapeTitle(draft.content) || titleVal;
}
var fileName = makeDatedFilename(
postType === "mixtapes" ? mixtapeTitle : titleVal,
dateForFilename,
);
var postPath = "_posts/" + year + "/" + fileName.toLowerCase();
if (postType !== "mixtapes") {
content = content.replace(titleVal, "").trim();
newDraft = "---\n\n";
newDraft += "layout: post\n";
if (syndicateArr.length > 0) {
newDraft += "syndicate:\n";
for (var i = 0; i < syndicateArr.length; i++) {
newDraft += " - " + syndicateArr[i] + "\n";
}
}
newDraft += "date: " + frontMatterDate + "\n";
newDraft +=
"title: " +
(postType === "til" ? "TIL - " + titleVal : titleVal) +
"\n";
if (linkVal !== "") newDraft += "link: " + linkVal + "\n";
if (citedVal !== "") newDraft += "cited: " + citedVal + "\n";
if (pinnedVal) newDraft += "pinned: true\n";
if (hideVal) newDraft += "show: false\n";
var typeMap = {
rss: "rss",
til: "til",
ways: "ways",
micro: "micro",
blog: "blog",
quote: "linked",
gift: "gift",
mailbox: "mailbox",
film: "film",
};
if (typeMap[postType]) {
newDraft += "type: " + typeMap[postType] + "\n";
}
newDraft += "\n---\n\n";
newDraft += content;
}
var baseURL =
"working-copy://x-callback-url/write/?key=" +
credential.getValue("working-copy-key") +
"&repo=" +
encodeURIComponent(credential.getValue("jekyll-repo")) +
"&path=" +
encodeURIComponent(postPath) +
"&text=" +
encodeURIComponent(newDraft),
cb = CallbackURL.create();
cb.baseURL = baseURL;
if (cb.open()) {
if (postType !== "mixtapes") {
editor.setText(newDraft);
}
} else {
app.displayErrorMessage("Failed to send to Working Copy");
context.fail();
}
}
} else {
context.cancel("Post creation cancelled.");
}
}