-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
655 lines (576 loc) · 17.5 KB
/
index.js
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
const fs = require("fs-extra");
const path = require("path");
const fetch = require("node-fetch");
const url = require("url");
const { cyan, green, yellow } = require("chalk");
const ghostContentAPI = require("@tryghost/content-api");
const log = ({ color, label, value = false }) => {
// log({
// color = chalk color
// label = string, text label
// value = var, value being read out
// });
console.log(`${color(label)}${value ? color(`: ${color.bold(value)}`) : ""}`);
};
const getContent = async ({ contentType, failPlugin }) => {
// getContent({
// contentType = api.posts || api.pages
// failPlugin = failPlugin
// });
try {
// Retrieve the content using the set API endpoint
const content = await contentType.browse({
include: "tags,authors",
limit: "all"
});
// Return content
return content;
} catch (error) {
failPlugin("Ghost API content error", { error });
}
};
const downloadImage = async ({ imagePath, outputPath, failPlugin }) => {
// downloadImage({
// imagePath = string, image path
// outputPath = string, desired relative image path
// failPlugin = failPlugin
// });
try {
// Grab file data from remote path
const response = await fetch(imagePath);
const fileData = await response.buffer();
// Write the file and cache it
await fs.outputFile(outputPath, fileData);
} catch (error) {
failPlugin("Image download error", { error });
}
};
const getRelativeImagePath = ({ imagePath, contentPath }) => {
// getRelativeImagePath({
// imagePath = string, the path of the image
// contentPath = string, the path of the post or page
// });
// Split both paths into arrays, at their directory points
const explodedImagePath = imagePath.split("/");
const explodedContentPath = contentPath.split("/");
// Find the point at which the image path diverges from the content path
const difference = explodedImagePath.findIndex((slice, index) => {
return explodedContentPath[index] != slice;
});
// Reconstruct the image path from the point it diverges to it's end
const relativePath = `/${explodedImagePath.slice(difference).join("/")}`;
// Return the new image path
return relativePath;
};
const dedent = ({ string }) => {
// dedent({
// string = string, the template content
// });
// Take any string and remove indentation
string = string.replace(/^\n/, "");
const match = string.match(/^\s+/);
const dedentedString = match
? string.replace(new RegExp("^" + match[0], "gm"), "")
: string;
return dedentedString;
};
const formatImagePaths = ({ string, imagesPath, assetsDir }) => {
// formatImagePaths({
// string = string, the template content
// imagesPath = string, original Ghost image path
// assetsDir = string, the new path for the image
// });
const imagePathRegex = new RegExp(imagesPath, "g");
if (string && string.match(imagePathRegex)) {
// Take a string and replace the Ghost image path with the new images path
return string.replace(imagePathRegex, assetsDir);
}
return string;
};
const createMarkdownContent = ({ content, imagesPath, assetsDir, layout }) => {
// createMarkdownContent({
// content = object, the content item
// imagesPath = string, the base path for Ghost images
// assetsPath = string, the new path for images
// layout = string, the layout name
// });
// Format tags into a comma separated string
const formatTags = (tags) => {
if (tags) {
return `[${tags.map((tag) => tag.slug).join(", ")}]`;
}
return "";
};
// Create the markdown template
const template = `
---
date: ${content.published_at.slice(0, 10)}
title: "${content.title}"
layout: ${layout}
excerpt: "${content.custom_excerpt ? content.custom_excerpt : ""}"
image: "${
content.feature_image
? formatImagePaths({
string: content.feature_image,
imagesPath,
assetsDir
})
: ""
}"
tags: ${formatTags(content.tags)}
---
${
content.html
? formatImagePaths({
string: content.html,
imagesPath,
assetsDir
})
: ""
}
`;
// Return the template without the indentation
return dedent({ string: template });
};
const createTagMarkdown = ({ content, imagesPath, assetsDir, layout }) => {
// createTagMarkdown({
// content = onject, the full content of the item
// imagesPath = string, the base path for Ghost images
// assetsPath = string, the new path for images
// layout = string, the layout name
//});
// Create the frontmatter template
const template = `
---
title: "${content.name ? content.name : content.slug}"
layout: ${layout}
excerpt: "${content.description ? content.description : ""}"
image: "${
content.feature_image
? formatImagePaths({
string: content.feature_image,
imagesPath,
assetsDir
})
: ""
}"
---
${content.html}
`;
// Return the template without the indentation
return dedent({ string: template });
};
const createAuthorMarkdown = ({ content, imagesPath, assetsDir, layout }) => {
// createAuthorMarkdown({
// content = onject, the full content of the item
// imagesPath = string, the base path for Ghost images
// assetsPath = string, the new path for images
// layout = string, the layout name
//});
// Create the frontmatter template
const template = `
---
title: "${content.name ? content.name : content.slug}"
layout: ${layout}
excerpt: "${content.bio ? content.bio : ""}"
image: "${
content.cover_image
? formatImagePaths({
string: content.cover_image,
imagesPath,
assetsDir
})
: ""
}"
---
${content.html}
`;
// Return the template without the indentation
return dedent({ string: template });
};
const createTaxonomyContent = ({ taxonomyItem, items, postDatePrefix }) => {
const descriptionLine = taxonomyItem.description
? `<p>${taxonomyItem.description}</p>`
: "";
const itemsList = items.length
? `
<ol>
${items
.map((item) => {
// Format post links to match date prefixing, if set
const link =
postDatePrefix && !item.page
? `${item.published_at.slice(0, 10).replace(/-/g, "/")}/${
item.slug
}`
: item.slug;
``;
return `
<li>
<a href="/${link}/">${item.title}</a>
${item.excerpt ? `<p>${item.excerpt}</p>` : ""}
</li>
`;
})
.join("")}
</ol>
`
: "";
return dedent({ string: descriptionLine + itemsList });
};
const writeFile = async ({ fullFilePath, content, failPlugin }) => {
// writeFile({
// fullFilePath = string, the full file path and name with extension
// content = contents of the file
// failPlugin = failPlugin
//});
try {
// Output file using path and name with it's content within
await fs.outputFile(fullFilePath, content);
} catch (error) {
failPlugin(`Error writing ${fullFilePath}`, { error });
}
};
const getCacheTimestamp = async ({ cache, fullFilePath, failPlugin }) => {
// getCacheTimestamp({
// cache = cache
// fullFilePath = string, the local file path and name
// failPlugin: failPlugin
// });
if (await cache.has(fullFilePath)) {
await cache.restore(fullFilePath);
const cacheDate = await readFile({
file: fullFilePath,
failPlugin: failPlugin
});
// Log cache timestamp in console
log({
color: yellow,
label: "Restoring markdown cache from",
value: cacheDate
});
return new Date(cacheDate);
} else {
// Log no cache file found
log({
color: yellow,
label: "No cache file found"
});
return 0;
}
};
const writeCacheTimestamp = async ({ cache, fullFilePath, failPlugin }) => {
// writeCacheTimestamp({
// cache = cache
// fullFilePath = string, the local file path and name
// failPlugin = failPlugin
// });
// Get the timestamp of right now
const now = new Date();
const nowISO = now.toISOString();
// Write the time into a cache file
await writeFile({
fullFilePath: fullFilePath,
content: `"${nowISO}"`,
failPlugin: failPlugin
});
await cache.save(fullFilePath);
// Log cache timestamp creation time
log({
color: yellow,
label: "Caching markdown at",
value: nowISO
});
};
const readFile = async ({ file, failPlugin }) => {
// readFile({
// file = string, the local file path and name
// failPlugin = failPlugin
// });
// Replace root path syntax with environment
const fullFilePath = file.replace("./", `${process.cwd()}/`);
const fileContent = require(fullFilePath);
// Return file content
return fileContent;
};
const getAllImages = ({ contentItems, imagesPath }) => {
// getAllImages({
// contentItems = array, post, page, tag, author objects
// imagesPath = string, the base path for Ghost images
// });
const htmlWithImages = contentItems
.filter((item) => {
return item.html && item.html.includes(imagesPath);
})
.map((filteredItem) => filteredItem.html);
const htmlImages = htmlWithImages
.map((html) => {
return html.split(/[\ "]/).filter((slice) => slice.includes(imagesPath));
})
.flat();
const featureImages = contentItems
.filter((item) => {
return item.feature_image && item.feature_image.includes(imagesPath);
})
.map((item) => item.feature_image);
const coverImages = contentItems
.filter((item) => {
return item.cover_image && item.cover_image.includes(imagesPath);
})
.map((item) => item.cover_image);
const allImages = [
...new Set([...htmlImages, ...featureImages, ...coverImages])
];
return allImages;
};
// Begin plugin export
module.exports = {
onPreBuild: async ({
inputs: {
ghostURL,
ghostKey,
assetsDir = "./assets/images/",
pagesDir = "./",
postsDir = "./_posts/",
tagPages = false,
authorPages = false,
tagsDir = "./tag/",
authorsDir = "./author/",
pagesLayout = "page",
postsLayout = "post",
tagsLayout = "tag",
authorsLayout = "author",
postDatePrefix = true,
cacheFile = "./_data/ghostMarkdownCache.json"
},
utils: {
build: { failPlugin },
cache
}
}) => {
// Ghost images path
const ghostImagePath = ghostURL + "/content/images/";
// Initialise Ghost Content API
const api = new ghostContentAPI({
url: ghostURL,
key: ghostKey,
version: "v2"
});
const [posts, pages, cacheDate, tags, authors] = await Promise.all([
getContent({
contentType: api.posts,
failPlugin: failPlugin
}),
getContent({
contentType: api.pages,
failPlugin: failPlugin
}),
getCacheTimestamp({
cache: cache,
fullFilePath: cacheFile,
failPlugin: failPlugin
}),
tagPages
? getContent({
contentType: api.tags,
failPlugin: failPlugin
})
: [],
authorPages
? getContent({
contentType: api.authors,
failPlugin: failPlugin
})
: []
]);
await Promise.all([
// Get all images from out of posts and pages
...getAllImages({
contentItems: [
...posts,
...pages,
...(tagPages ? tags : []),
...(authorPages ? authors : [])
],
imagesPath: ghostImagePath
}).map(async (image) => {
// Create destination for each image
const dest = image.replace(ghostImagePath, assetsDir);
// If the image isn't in cache download it
if (!(await cache.has(dest))) {
await downloadImage({
imagePath: image,
outputPath: dest,
failPlugin: failPlugin
});
// Cache the image
await cache.save(dest);
log({
color: green,
label: "Downloaded and cached",
value: dest
});
} else {
// Restore the image if it's already in the cache
await cache.restore(dest);
log({
color: cyan,
label: "Restored from cache",
value: dest
});
}
}),
...posts.map(async (post) => {
// Set the file name using the post slug
let fileName = `${post.slug}.md`;
// If postDatePrefix is true prefix file with post date
if (postDatePrefix) {
fileName = `${post.published_at.slice(0, 10)}-${post.slug}.md`;
}
// The full file path and name
const fullFilePath = postsDir + fileName;
// Get the post updated date and last cached date
const postUpdatedAt = new Date(post.updated_at);
if ((await cache.has(fullFilePath)) && cacheDate > postUpdatedAt) {
// Restore markdown from cache
await cache.restore(fullFilePath);
log({
color: cyan,
label: "Restored from cache",
value: fullFilePath
});
} else {
// Generate markdown file
await writeFile({
fullFilePath: fullFilePath,
content: createMarkdownContent({
content: post,
imagesPath: ghostImagePath,
assetsDir: getRelativeImagePath({
imagePath: assetsDir,
contentPath: postsDir
}),
layout: postsLayout
})
});
// Cache the markdown file
await cache.save(fullFilePath);
log({
color: green,
label: "Generated and cached",
value: fullFilePath
});
}
}),
...pages.map(async (page) => {
// Set the file name using the page slug
let fileName = `${page.slug}.md`;
// The full file path and name
const fullFilePath = pagesDir + fileName;
// Get the page updated date and last cached date
const pageUpdatedAt = new Date(page.updated_at);
if ((await cache.has(fullFilePath)) && cacheDate > pageUpdatedAt) {
// Restore markdown from cache
await cache.restore(fullFilePath);
log({
color: cyan,
label: "Restored from cache",
value: fullFilePath
});
} else {
// Generate markdown file
await writeFile({
fullFilePath: fullFilePath,
content: createMarkdownContent({
content: page,
imagesPath: ghostImagePath,
assetsDir: getRelativeImagePath({
imagePath: assetsDir,
contentPath: pagesDir
}),
layout: pagesLayout
})
});
// Cache the markdown file
await cache.save(fullFilePath);
log({
color: green,
label: "Generated and cached",
value: fullFilePath
});
}
}),
...(tagPages
? tags.map(async (tag) => {
// Filter posts and pages to only tagged items
const taggedItems = [...pages, ...posts].filter((items) => {
return items.tags.some((postTag) => postTag.slug === tag.slug);
});
// Add content to the author page
tag.html = createTaxonomyContent({
taxonomyItem: tag,
items: taggedItems,
postDatePrefix
});
// Set the file name using the page slug
let fileName = `${tag.slug}.md`;
// The full file path and name
const fullFilePath = tagsDir + fileName;
// Generate markdown file
await writeFile({
fullFilePath: fullFilePath,
content: createTagMarkdown({
content: tag,
imagesPath: ghostImagePath,
assetsDir: getRelativeImagePath({
imagePath: assetsDir,
contentPath: tagsDir
}),
layout: tagsLayout
})
});
})
: []),
...(authorPages
? authors.map(async (author) => {
// Filter posts and pages to only tagged items
const authoredItems = [...pages, ...posts].filter((items) => {
return items.authors.some(
(postAuthor) => postAuthor.slug === author.slug
);
});
// Add content to the author page
author.html = createTaxonomyContent({
taxonomyItem: author,
items: authoredItems,
postDatePrefix
});
// Set the file name using the page slug
let fileName = `${author.slug}.md`;
// The full file path and name
const fullFilePath = authorsDir + fileName;
// Generate markdown file
await writeFile({
fullFilePath: fullFilePath,
content: createAuthorMarkdown({
content: author,
imagesPath: ghostImagePath,
assetsDir: getRelativeImagePath({
imagePath: assetsDir,
contentPath: authorsDir
}),
layout: authorsLayout
})
});
})
: [])
]).then(async (response) => {
// Write a new cache file
await writeCacheTimestamp({
cache: cache,
fullFilePath: cacheFile,
failPlugin: failPlugin
});
});
}
};