-
Notifications
You must be signed in to change notification settings - Fork 1
/
minearm.ts
174 lines (148 loc) · 4.41 KB
/
minearm.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
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
import fs from "fs";
import path from "path";
import matter from "gray-matter";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { promisify } from "util";
import { glob } from "glob";
import { utils } from "./src/utils/utils.js";
interface Argv {
_: string[];
filename?: string;
$0: string;
}
const readFileAsync = promisify(fs.readFile);
const writeFileAsync = promisify(fs.writeFile);
const mkdirAsync = promisify(fs.mkdir);
const getDateString = () => {
const date = new Date();
const monthNames = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
const month = monthNames[date.getMonth()];
const day = date.getDate().toString().padStart(2, "0");
const year = date.getFullYear();
return `${month} ${day} ${year}`;
};
const getHexTimestamp = () => {
const timestamp = Date.now();
const hexTimestamp = timestamp.toString(36);
return hexTimestamp;
};
const createFile = async (
filePath: string,
dirPath: string,
content: string
) => {
// Get all directories from dirPath
const directories = dirPath.split(path.sep);
// Make sure all directories exist, create them if necessary
for (let i = 1; i <= directories.length; i++) {
const segment = directories.slice(0, i).join(path.sep);
if (!fs.existsSync(segment)) {
await mkdirAsync(segment);
}
}
if (fs.existsSync(filePath)) {
console.error(`File ${filePath} already exists.`);
process.exit(1);
}
await writeFileAsync(filePath, content);
console.log(`File created: ${filePath}`);
};
const createMdFile = async (filename: string) => {
const dateStr = getDateString();
const hexTimestamp = getHexTimestamp();
// split filename into path and actual filename
const filenameParts = filename.split("/");
const actualFilename = filenameParts.pop();
// create the full directory path
const dirPath = path.resolve(`./src/content/blog/`, ...filenameParts);
const filePath = path.resolve(dirPath, `${actualFilename}.md`);
const content = `---
title: ${actualFilename}
description: ''
pubDate: ${dateStr}
updatedDate: ${dateStr}
heroColor: ''
abbrlink: ${hexTimestamp}
tags:
- ''
category: ''
---`;
await createFile(filePath, dirPath, content);
};
const createMdPage = async (filename: string) => {
const dirPath = path.resolve(`./src/pages/${filename}`);
const filePath = path.resolve(dirPath, "index.md");
const content = `---
layout: "../../layouts/DefaultMdLayout.astro"
title: ${filename}
description: ""
heroColor: "#007aff"
useComments: true
useToc: true
---
## ${filename}`;
await createFile(filePath, dirPath, content);
};
const addAbbrlinkToFile = async (filepath: string) => {
try {
const content = await readFileAsync(filepath, "utf8");
const parsedMatter = matter(content);
if (!parsedMatter.data.abbrlink) {
const pubDateStr = parsedMatter.data.pubDate;
const pubDate = new Date(pubDateStr);
const abbrlink = pubDate.getTime().toString(36);
parsedMatter.data.abbrlink = abbrlink;
const fileContent = matter.stringify(content, parsedMatter.data);
await writeFileAsync(filepath, fileContent);
console.log(`Permalink added: ${filepath}`);
}
} catch (error) {
console.error(`Unable to add link: ${filepath}`);
console.error(error);
}
};
const addAbbrlinkToFiles = async () => {
try {
const files = await glob("./src/content/blog/**/*.md");
for (const file of files) {
await addAbbrlinkToFile(file);
}
} catch (error) {
console.error(error);
}
};
const argv = yargs(hideBin(process.argv))
.command("new <filename>", "Create a new post with the title <filename>")
.command("abbr", "Add a permalink to markdown files")
.command("newPage <filename>", "Create a new page with the title <filename>")
.help()
.alias("help", "h").argv as Argv;
if (argv._[0] === "new") {
if (argv.filename) {
createMdFile(argv.filename);
} else {
console.error('Filename is required for the "new" command');
}
} else if (argv._[0] === "abbr") {
addAbbrlinkToFiles();
} else if (argv._[0] === "newPage") {
if (argv.filename) {
createMdPage(argv.filename);
} else {
console.error('Filename is required for the "newPage" command');
}
}