-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreatePost.ts
91 lines (82 loc) · 2.31 KB
/
createPost.ts
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
import inquirer from "inquirer";
import dayjs from "dayjs";
import Bun from "bun";
/**
* Get all tags from the content directory
*/
const tags: Array<{ label: string; value: string }> = [];
const glob = new Bun.Glob("**/*.md");
const contentPath = "./src/content/tag";
for await (const filePath of glob.scan(contentPath)) {
const fullFilePath = [contentPath, filePath].join("/");
const record = Bun.file(fullFilePath);
const contents = await record.text();
const title = (contents.match(/name: (.*)/) ?? [])[1];
tags.push({
label: String(title) ?? filePath,
value: filePath.replace(/\.md$/, "")
});
}
/**
* Prompt the user for the post details
*/
const answers = await inquirer.prompt([
{
type: "input",
name: "title",
message: "What is the title of the post?"
},
{
type: "input",
name: "date",
message: "what is the date of the post?",
default: dayjs().format("YYYY-MM-DD"),
validate: (input) => {
if (dayjs(input).isValid()) {
return true;
}
return "Please enter a valid date in the format YYYY-MM-DD";
}
},
{
type: "checkbox",
name: "tags",
message: "Select tags for the post",
choices: tags.sort((a, b) => a.label.localeCompare(b.label))
}
]);
/**
* Function to Create YAML Front Matter from Object
*/
const YAML_INDENT = 2;
function YAMLStringify(obj: Record<string, any>) {
return `---\n${Object.entries(obj)
.map(([key, value]) => {
if (Array.isArray(value)) {
return `${key}:\n${value.map((val) => `${" ".repeat(YAML_INDENT)}- ${val}`).join("\n")}`;
}
return `${key}: ${value}`;
})
.join("\n")}\n---\n`;
}
/**
* Create the new blog post
*/
const newBlogPost = YAMLStringify({
title: "Test Post",
description: "My Post Description",
date: dayjs().format("YYYY-MM-DD"),
tags: ["devops"],
image: "../../assets/default-1.png",
featured: false,
draft: false,
...answers
});
/**
* Write the new blog post to the file system
*/
const fileName = [answers.date, answers.title]
.join("-")
.replace(/[^a-z0-9]{1,}/gi, "-")
.toLowerCase();
await Bun.write(`./src/content/blog/${fileName}.md`, newBlogPost);