-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathplopfile.js
More file actions
863 lines (829 loc) · 19.4 KB
/
plopfile.js
File metadata and controls
863 lines (829 loc) · 19.4 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
import { readFileSync, readdirSync, renameSync, writeFileSync } from "fs";
import { execFile } from "node:child_process";
import path from "path";
import { fileURLToPath } from "url";
// Get current date information in UTC
const today = new Date();
const iso = today.toISOString().slice(0, 19);
// Import JSON data with dynamic imports
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const series = JSON.parse(
readFileSync(path.join(__dirname, "_cache/series.json"), "utf8"),
);
const tags = JSON.parse(
readFileSync(path.join(__dirname, "_cache/tags.json"), "utf8"),
);
const events = JSON.parse(
readFileSync(
path.join(__dirname, "src/_data/speaking_engagements.json"),
"utf8",
),
);
// Helper functions
const helpers = {
escapeQuotes: (str) => {
return str.replace('"', '\\"');
},
getTimestamp: () => {
return `${iso.replace("T", " ")} +00:00`;
},
getTags: () => {
return tags;
},
getSeriesName: (tag) => {
return series[tag];
},
getDate: () => {
return iso.substring(0, 10);
},
getFilename: (title, include_date = true) => {
const date = iso.substring(0, 10);
// Slugify the title
let text = title;
text = text.replace(/^\s+|\s+$/g, "");
// Make the string lowercase
text = text.toLowerCase();
// Remove accents, swap ñ for n, etc
const from =
"ÁÄÂÀÃÅČÇĆĎÉĚËÈÊẼĔȆÍÌÎÏŇÑÓÖÒÔÕØŘŔŠŤÚŮÜÙÛÝŸŽáäâàãåčçćďéěëèêẽĕȇíìîïňñóöòôõøðřŕšťúůüùûýÿžþÞĐđ߯a·/_,:;";
const to =
"AAAAAACCCDEEEEEEEEIIIINNOOOOOORRSTUUUUUYYZaaaaaacccdeeeeeeeeiiiinnooooooorrstuuuuuyyzbBDdBAa------";
for (let i = 0, l = from.length; i < l; i++) {
text = text.replace(new RegExp(from.charAt(i), "g"), to.charAt(i));
}
// Remove invalid chars
text = text
.replace(/[^a-z0-9 -]/g, "")
// Collapse whitespace and replace by -
.replace(/\s+/g, "-")
// Collapse dashes
.replace(/-+/g, "-");
return include_date ? `${date}-${text}` : text;
},
ifEquals: (value_A, value_B, options) => {
return value_A === value_B ? options.fn(this) : options.inverse(this);
},
ifNotEquals: (value_A, value_B, options) => {
return value_A !== value_B ? options.fn(this) : options.inverse(this);
},
getEvents: (subset) => {
var choices = [];
events.map((event) => {
if (
!subset ||
(subset instanceof Array && subset.indexOf(event.id) > -1)
) {
choices.push({
name: `${event.title} (${event.date})`,
value: event.id,
});
}
});
return choices;
},
getEventDate: (selected_events) => {
selected_events.sort();
var id = selected_events[0];
var event = events.find((evt) => evt.id == id);
return event.date.replace(/(\d) ([+-]\d)/, "$1$2").replace(" ", "T");
},
getDrafts: () => {
const draftsDir = path.join(__dirname, "src/_drafts");
const files = readdirSync(draftsDir).filter((f) => f.endsWith(".md"));
return files.map((file) => ({
name: file.replace(".md", ""),
value: file,
}));
},
};
// Plop configuration
export default function (plop) {
// Add helpers to plop
Object.keys(helpers).forEach((helperName) => {
plop.setHelper(helperName, helpers[helperName]);
});
// Custom actions
plop.setActionType("openFile", async function (answers, config, _plop) {
const use_date = config.date !== undefined ? config.date : true;
const filename = `./src/${config.directory}/${helpers.getFilename(
answers.title,
use_date,
)}.md`;
setTimeout(() => {
execFile(
"code",
[filename],
{ shell: true },
(error, stdout, _stderr) => {
if (error) {
throw error;
}
console.log(stdout);
},
);
}, 3000);
return;
});
// Custom action to publish a draft
plop.setActionType("publishDraft", async function (answers, _config, _plop) {
const draftPath = path.join(__dirname, "src/_drafts", answers.draft);
const timestamp = helpers.getTimestamp();
const datePrefix = helpers.getDate();
// Read the draft file
const content = readFileSync(draftPath, "utf8");
// Update the date in frontmatter
const updatedContent = content.replace(/^date:.*$/m, `date: ${timestamp}`);
// Create new filename with date prefix
const newFilename = `${datePrefix}-${answers.draft}`;
const publishPath = path.join(__dirname, "src/posts", newFilename);
// Write to posts directory
writeFileSync(publishPath, updatedContent, "utf8");
// Delete the draft
const fs = await import("fs/promises");
await fs.unlink(draftPath);
console.log(`✓ Published ${answers.draft} to posts/${newFilename}`);
// Return the path so it can be opened
return publishPath;
});
// Article generator
plop.setGenerator("article", {
description: "Add a new article",
prompts: [
{
type: "input",
name: "title",
message: "What’s the article’s title?",
validate: (value) => (!value ? "Title cannot be empty" : true),
},
{
type: "input",
name: "date",
message: "When did it publish? (YYYY-MM-DD)",
validate: (value) =>
!value.match(/\d{4}-\d{2}-\d{2}/)
? "Date must be in the format YYY-MM-DD"
: true,
default: helpers.getDate(),
},
{
type: "checkbox",
name: "tags",
message: "Tag this article (you can add new ones later)",
choices: tags.map((tag) => ({ name: tag, value: tag })),
},
{
type: "input",
name: "publisher",
message: "Where was it published?",
validate: (value) => (!value ? "Publisher cannot be empty" : true),
},
{
type: "input",
name: "url",
message: "What’s the link?",
},
{
type: "input",
name: "description",
message: "Describe or excerpt the article",
},
],
actions: [
{
type: "add",
path: "src/publications/articles/{{getFilename title false}}.md",
templateFile: "_templates/article.md.hbs",
},
{
type: "openFile",
date: false,
directory: "publications/articles",
},
],
});
// Book generator
plop.setGenerator("book", {
description: "Add a new book",
prompts: [
{
type: "input",
name: "title",
message: "What’s the book’s title?",
validate: (value) => (!value ? "Title cannot be empty" : true),
},
{
type: "input",
name: "subtitle",
message: "If it has a subtitle, enter that here",
},
{
type: "input",
name: "date",
message: "When did it publish? (YYYY-MM-DD)",
validate: (value) =>
!value.match(/\d{4}-\d{2}-\d{2}/)
? "Date must be in the format YYY-MM-DD"
: true,
default: helpers.getDate(),
},
{
type: "input",
name: "author",
message: "Who is on the book’s byline?",
validate: (value) => (!value ? "Author cannot be empty" : true),
},
{
type: "list",
name: "type",
message: "What kind of contribution did you make?",
choices: ["authored", "foreword", "contributed"],
},
{
type: "checkbox",
name: "tags",
message: "Tag this book (you can add new ones later)",
choices: tags.map((tag) => ({ name: tag, value: tag })),
},
{
type: "input",
name: "publisher",
message: "Who published it?",
validate: (value) => (!value ? "Publisher cannot be empty" : true),
},
{
type: "input",
name: "url",
message: "What’s the link?",
validate: (value) => (!value ? "URL cannot be empty" : true),
},
{
type: "input",
name: "full_text",
message:
"If the full text of the contribution is available at a URL, enter that here",
when: (answers) => answers.type !== "authored",
},
{
type: "input",
name: "description",
message: "Describe the book",
},
],
actions: [
{
type: "add",
path: "src/publications/books/{{getFilename title false}}.md",
templateFile: "_templates/book.md.hbs",
},
{
type: "openFile",
date: false,
directory: "publications/books",
},
],
});
// Citation generator
plop.setGenerator("citation", {
description: "Add a new citation",
prompts: [
{
type: "input",
name: "title",
message: "What’s the title?",
},
{
type: "input",
name: "author",
message: "Who is the author?",
},
{
type: "input",
name: "url",
message: "Where can people find it?",
},
{
type: "list",
name: "type",
message: "What category of citation is this?",
choices: ["code", "books", "articles", "talks"],
},
{
type: "input",
name: "lang",
message:
"If it’s in another language, enter the language code here (e.g., “es-MX”)",
default: "",
},
{
type: "list",
name: "dir",
message: "Text direction",
choices: [
{ name: "left-to-right (default)", value: "" },
{ name: "right-to-left", value: "rtl" },
],
default: "",
},
],
actions: [
{
type: "modify",
path: "src/_data/citations.json",
transform(original, data) {
const citationsCopy = JSON.parse(original);
const citation = {
title: data.title,
author: data.author,
url: data.url,
};
if (data.lang) {
citation.lang = data.lang;
}
if (data.dir) {
data.dir = data.dir;
}
citationsCopy[data.type].push(citation);
return JSON.stringify(citationsCopy, null, 2);
},
},
],
});
// Event generator (you can add other generators similarly)
plop.setGenerator("event", {
description: "Add a new speaking engagement",
prompts: [
{
type: "input",
name: "title",
message: "What’s the event?",
},
{
type: "input",
name: "date",
message: "What’s the date? (YYY-MM-DD)",
default: helpers.getDate(),
},
{
type: "input",
name: "location",
message: "Where is it?",
default: "Online",
},
{
type: "input",
name: "url",
message: "Is there a link to the event?",
},
],
actions: [
{
type: "modify",
path: "src/_data/speaking_engagements.json",
transform(original, data) {
let original_events = JSON.parse(original);
const new_events = [...original_events].sort((a, b) => {
return a.id > b.id ? 1 : -1;
});
new_events.reverse();
const event = {
id: new_events[0].id + 1,
title: data.title,
date: `${data.date} 00:09:00 +0000`,
location: data.location || "Online",
};
if (data.url) {
event.url = data.url;
}
new_events.unshift(event);
return JSON.stringify(new_events, null, 2);
},
},
],
});
// Link generator
plop.setGenerator("link", {
description: "Add a new link post",
prompts: [
{
type: "input",
name: "title",
message: "Title",
},
{
type: "input",
name: "source",
message: "Source",
},
{
type: "input",
name: "url",
message: "URL",
},
{
type: "input",
name: "description",
message: "Description",
},
{
type: "input",
name: "twitter_text",
message: "Text for Twitter",
},
{
type: "checkbox",
name: "tags",
message: "Choose tags (you can add new ones later)",
choices: tags.map((tag) => ({ name: tag, value: tag })),
},
{
type: "input",
name: "quote",
message: "Anything you want to quote?",
},
{
type: "input",
name: "via_name",
message: "Did someone else post this?",
},
{
type: "input",
name: "via_url",
message: "What's the URL?",
when: (answers) => answers.via_name !== "",
},
],
actions: [
{
type: "add",
path: "src/links/{{getFilename title}}.md",
templateFile: "_templates/link.md.hbs",
},
{
type: "openFile",
directory: "links",
},
],
});
// Podcast generator
plop.setGenerator("podcast", {
description: "Add a new podcast appearance",
prompts: [
{
type: "input",
name: "title",
message: "What’s the podcast episode title?",
validate: (value) => (!value ? "Title cannot be empty" : true),
},
{
type: "input",
name: "date",
message: "When did it publish? (YYYY-MM-DD)",
validate: (value) =>
!value.match(/\d{4}-\d{2}-\d{2}/)
? "Date must be in the format YYY-MM-DD"
: true,
default: helpers.getDate(),
},
{
type: "checkbox",
name: "tags",
message: "Tag this podcast (you can add new ones later)",
choices: tags.map((tag) => ({ name: tag, value: tag })),
},
{
type: "input",
name: "publisher",
message: "What podcast were you on?",
validate: (value) => (!value ? "Publisher cannot be empty" : true),
},
{
type: "input",
name: "episode",
message:
"If there is an episode number associated with it, enter that here",
},
{
type: "input",
name: "url",
message: "What’s the link?",
validate: (value) => (!value ? "URL cannot be empty" : true),
},
{
type: "input",
name: "description",
message: "Describe the episode",
},
],
actions: [
{
type: "add",
path: "src/appearances/podcasts/{{getFilename title false}}.md",
templateFile: "_templates/podcast.md.hbs",
},
{
type: "openFile",
date: false,
directory: "appearances/podcasts",
},
],
});
// Post generator
plop.setGenerator("post", {
description: "Create a new blog post",
prompts: [
{
type: "input",
name: "title",
message: "Title",
},
{
type: "input",
name: "description",
message: "Description",
},
{
type: "input",
name: "twitter_text",
message: "Text for Twitter",
},
{
type: "checkbox",
name: "tags",
message: "Choose tags (you can add new ones later)",
choices: tags.map((tag) => ({ name: tag, value: tag })),
},
{
type: "list",
name: "series",
message: "Is this part of an existing series?",
choices: Object.keys({ None: "", ...series }).map((tag) => ({
name: series[tag],
value: tag,
})),
},
{
type: "input",
name: "in_reply_to",
message: "URL of the post to reply to",
},
],
actions: [
{
type: "add",
path: "src/posts/{{getFilename title}}.md",
templateFile: "_templates/post.md.hbs",
},
{
type: "openFile",
directory: "posts",
},
],
});
// Press appearance generator
plop.setGenerator("press", {
description: "Add a new press appearance",
prompts: [
{
type: "input",
name: "title",
message: "What’s the press item’s title?",
validate: (value) => (!value ? "Title cannot be empty" : true),
},
{
type: "input",
name: "date",
message: "When did it publish? (YYYY-MM-DD)",
default: helpers.getDate(),
validate: (value) =>
!value.match(/\d{4}-\d{2}-\d{2}/)
? "Date must be in the format YYY-MM-DD"
: true,
},
{
type: "checkbox",
name: "tags",
message: "What did you talk about?",
choices: tags.map((tag) => ({ name: tag, value: tag })),
},
{
type: "input",
name: "publisher",
message: "Where was it published?",
validate: (value) => (!value ? "Publisher cannot be empty" : true),
},
{
type: "input",
name: "url",
message: "What’s the link?",
validate: (value) => (!value ? "URL cannot be empty" : true),
},
{
type: "input",
name: "description",
message: "Describe or excerpt the press item",
},
],
actions: [
{
type: "add",
path: "src/appearances/podcast/{{getFilename title false}}.md",
templateFile: "_templates/podcast.md.hbs",
},
{
type: "openFile",
date: false,
directory: "appearances/press",
},
],
});
// Talk generator
plop.setGenerator("talk", {
description: "Create a new talk post",
prompts: [
{
type: "input",
name: "title",
message: "Title",
},
{
type: "input",
name: "description",
message: "Description",
},
{
type: "confirm",
name: "has_copresenter",
message: "Did you have a co-presenter?",
},
{
type: "input",
name: "copresenter.name",
message: "What’s their name?",
when: (answers) => answers.has_copresenter,
},
{
type: "input",
name: "copresenter.url",
message: "What’s a good URL for them?",
when: (answers) => answers.has_copresenter,
},
{
type: "list",
name: "category",
message: "What kind of talk was this?",
choices: [
"talk",
"keynote",
"workshop",
"guest lecture",
"panel",
"lightning-talk",
"webinar",
],
},
{
type: "checkbox",
name: "tags",
message: "Choose tags (you can add new ones later)",
choices: tags.map((tag) => ({ name: tag, value: tag })),
},
{
type: "checkbox",
name: "events",
message: "Which event(s) did you present this at?",
choices: helpers.getEvents(),
},
{
type: "input",
name: "date",
message: "When was this first given?",
default: (answers) => helpers.getEventDate(answers.events),
},
{
type: "confirm",
name: "has_slides",
message: "Do you have slides to share?",
},
{
type: "list",
name: "slides.event",
message: "What event is this slide deck from?",
choices: (answers) => helpers.getEvents(answers.events),
when: (answers) => answers.has_slides,
},
{
type: "input",
name: "slides.link",
message: "Link to talk on Noti.st",
when: (answers) => answers.has_slides,
},
{
type: "input",
name: "slides.embed",
message: "Embed URL",
when: (answers) => answers.has_slides,
default: (answers) =>
answers.slides.link.replace(
/^(https:\/\/presentations.aaron-gustafson.com\/.+?)\/.+$/,
"$1/embed",
),
},
{
type: "input",
name: "slides.download",
message: "Download URL (start with the year folder on GitHub)",
when: (answers) => answers.has_slides,
default: `${iso.replace(/^(\d{4}).+$/, "$1")}/`,
},
{
type: "input",
name: "slides.size",
message: "Size of the download",
when: (answers) => answers.has_slides,
default: "83.5 MB",
},
{
type: "confirm",
name: "has_video",
message: "Is there a video?",
},
{
type: "list",
name: "video.event",
message: "What event is the video from?",
choices: (answers) => helpers.getEvents(answers.events),
when: (answers) => answers.has_video,
},
{
type: "input",
name: "video.link",
message: "Link URL",
when: (answers) => answers.has_video,
},
{
type: "input",
name: "video.embed",
message: "Embed URL",
when: (answers) => answers.has_video,
},
{
type: "input",
name: "video.file",
message: "Video File",
when: (answers) => answers.has_video,
},
{
type: "confirm",
name: "has_audio",
message: "Is there an audio recording?",
},
{
type: "list",
name: "audio.event",
message: "What event is the audio from?",
choices: (answers) => helpers.getEvents(answers.events),
when: (answers) => answers.has_audio,
},
{
type: "input",
name: "audio.file",
message: "Audio File",
when: (answers) => answers.has_audio,
},
{
type: "input",
name: "text_url",
message: "If this exists as a text version, what’s the URL?",
},
],
actions: [
{
type: "add",
path: "src/talks/{{getFilename title}}.md",
templateFile: "_templates/talk.md.hbs",
},
{
type: "openFile",
date: false,
directory: "talks",
},
],
});
// Publish draft generator
plop.setGenerator("publish", {
description: "Publish a draft post",
prompts: [
{
type: "list",
name: "draft",
message: "Which draft would you like to publish?",
choices: helpers.getDrafts(),
},
],
actions: [
{
type: "publishDraft",
},
],
});
}