This repository was archived by the owner on Mar 13, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathhsublog-cli.ts
More file actions
215 lines (184 loc) · 5.56 KB
/
Copy pathhsublog-cli.ts
File metadata and controls
215 lines (184 loc) · 5.56 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
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 { SiteConfig } from "./src/site_config.js";
import { fileURLToPath } from "url";
import { dirname } from "path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const languages: string[] = SiteConfig.i18n.languages;
const defaultLanguage: string = SiteConfig.i18n.default;
const nonDefaultLanguages: string[] = languages.filter(
(lang) => lang != defaultLanguage
);
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();
return date.toISOString();
};
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
for (const language of languages) {
const dirPath = path.resolve(
__dirname,
`./src/content/blog/${language}`,
...filenameParts
);
const filePath = path.resolve(dirPath, `${actualFilename}.md`);
const content = `---
title: ${actualFilename}
description: ''
pubDate: ${dateStr}
updatedDate: ${dateStr}
heroColor: ''
themeColor: ''
cover: ''
abbrlink: ${hexTimestamp}
tags:
- ''
category: ''
lang: '${language}'
---`;
await createFile(filePath, dirPath, content);
}
};
const createMdPage = async (filename: string) => {
for (const lang of languages) {
const dirPath = path.resolve(
__dirname,
`./src/pages/${lang === defaultLanguage ? "" : lang + "/"}${filename}`
);
const filePath = path.resolve(dirPath, "index.md");
const content = `---
layout: "@layout/DefaultMdLayout.astro"
title: ${filename}
description: ""
heroColor: "#007aff"
themeColor: ''
useComments: true
useToc: false
---
## ${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 replaceDefaultLang = async (files: string[], lang: string) => {
for (const file of files) {
const content = await readFileAsync(file, "utf8");
const regex =
/const thePageLanguage = ".*"; \/\/the language of this page/g;
const updatedContent = content.replace(
regex,
`const thePageLanguage = '${lang}'; //the language of this page`
);
await writeFileAsync(file, updatedContent, "utf8");
}
};
const updateI18n = async () => {
const excludeDirs: string[] = [];
for (const lang of nonDefaultLanguages) {
const searchPath = `./src/pages/${lang}/**/*.astro`;
excludeDirs.push(searchPath);
const files = await glob(searchPath);
replaceDefaultLang(files, lang);
}
const searchPath = "./src/pages/**/*.astro";
const files = await glob(searchPath, {
ignore: excludeDirs,
});
replaceDefaultLang(files, defaultLanguage);
};
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>")
.command("i18n", "Create basic internationalized pages")
.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');
}
} else if (argv._[0] === "i18n") {
updateI18n();
}