summaryrefslogtreecommitdiff
path: root/index.ts
diff options
context:
space:
mode:
authornea <nea@nea.moe>2023-08-04 23:02:20 +0200
committernea <nea@nea.moe>2023-08-04 23:02:20 +0200
commit2563bf526aa47b2a3e2ec3b30eff9b9f808dd270 (patch)
tree4d45199fd00419dc888035114e9339bec986dadc /index.ts
parent5945696d0752cfb70ecf0c1e410547cf1c7471e3 (diff)
downloadechowebsite-2563bf526aa47b2a3e2ec3b30eff9b9f808dd270.tar.gz
echowebsite-2563bf526aa47b2a3e2ec3b30eff9b9f808dd270.tar.bz2
echowebsite-2563bf526aa47b2a3e2ec3b30eff9b9f808dd270.zip
Add papercss
Diffstat (limited to 'index.ts')
-rw-r--r--index.ts38
1 files changed, 24 insertions, 14 deletions
diff --git a/index.ts b/index.ts
index eadab52..13b6e9f 100644
--- a/index.ts
+++ b/index.ts
@@ -18,6 +18,27 @@ function splitPostHeader(postData: string): [string, string] {
await fs.rm("build", { recursive: true });
await fs.mkdir("build");
+declare interface PostMetadata {
+ name: string;
+ unlisted?: boolean;
+ tags?: Array<string>;
+ description?: string;
+}
+
+const baseTemplate = await fs.readFile("template.html", { encoding: "utf-8" });
+
+function makeHtmlForPost(postMetadata: PostMetadata, markdown: string): string {
+ const converter = new showdown.Converter();
+ const htmlPostBody = converter.makeHtml(markdown);
+ const variables = {
+ postBody: htmlPostBody,
+ title: postMetadata.name,
+ };
+ return baseTemplate.replace(
+ /%([a-zA-Z]+)%/g,
+ (_, match) => variables[match] ?? `Missing variable: ${match}`,
+ );
+}
for (let postPath of posts) {
const totalPostPath = `posts/${postPath}`;
@@ -27,19 +48,8 @@ for (let postPath of posts) {
}
const postText = await fs.readFile(totalPostPath, { encoding: "utf-8" });
const [postHeader, postBody] = splitPostHeader(postText);
- const postMetadata = yaml.parse(postHeader);
- const converter = new showdown.Converter();
- const htmlBody = converter.makeHtml(postBody);
-
- const primitiveHtmlDocument = `
-<html>
- <head>
- <title>${postMetadata.name}</title>
- </head>
- <body>
- ${htmlBody}
- </body>
-</html>`;
+ const postMetadata = yaml.parse(postHeader) as PostMetadata;
+ const postHtml = makeHtmlForPost(postMetadata, postBody);
const targetPath = `build/${postPath.replace(".md", ".html")}`;
- fs.writeFile(targetPath, primitiveHtmlDocument, { encoding: "utf-8" });
+ fs.writeFile(targetPath, postHtml, { encoding: "utf-8" });
}