From 2563bf526aa47b2a3e2ec3b30eff9b9f808dd270 Mon Sep 17 00:00:00 2001 From: nea Date: Fri, 4 Aug 2023 23:02:20 +0200 Subject: Add papercss --- index.ts | 38 ++++++++++++++++++++++++-------------- posts/my-first-felony.md | 5 ++++- template.html | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 15 deletions(-) create mode 100644 template.html 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; + 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 = ` - - - ${postMetadata.name} - - - ${htmlBody} - -`; + 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" }); } diff --git a/posts/my-first-felony.md b/posts/my-first-felony.md index 08a9f58..eac6623 100644 --- a/posts/my-first-felony.md +++ b/posts/my-first-felony.md @@ -1,7 +1,10 @@ --- name: "whatever" - date: today +tags: + - life-stories + - nsfw +unlisted: true --- # My first felony diff --git a/template.html b/template.html new file mode 100644 index 0000000..d8abe1e --- /dev/null +++ b/template.html @@ -0,0 +1,35 @@ + + + %title% + + + + + + + +
%postBody%
+ + -- cgit