diff options
-rw-r--r-- | index.ts | 88 | ||||
-rw-r--r-- | posts/about.md | 12 | ||||
-rw-r--r-- | posts/my-first-felony.md | 5 | ||||
-rw-r--r-- | template.html | 6 |
4 files changed, 79 insertions, 32 deletions
@@ -2,8 +2,6 @@ import showdown from "showdown"; import yaml from "yaml"; import { promises as fs } from "fs"; -const posts = await fs.readdir("posts"); - function splitPostHeader(postData: string): [string, string] { if (postData.startsWith("---\n")) { const headerEndIndex = postData.indexOf("\n---\n", 4); @@ -16,8 +14,6 @@ function splitPostHeader(postData: string): [string, string] { return ["", postData]; } -await fs.rm("build", { recursive: true }); -await fs.mkdir("build"); declare interface PostMetadata { name: string; unlisted?: boolean; @@ -25,31 +21,69 @@ declare interface PostMetadata { description?: string; } +declare interface PostEntry { + name: string; + id: string; + description: string | null; +} + const baseTemplate = await fs.readFile("template.html", { encoding: "utf-8" }); +class PostCollector { + allPosts: PostEntry[] = []; + 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}`, + ); + } -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}`, - ); -} + async run() { + await fs.rm("build", { recursive: true }); + await fs.mkdir("build"); + await fs.mkdir("build/posts"); -for (let postPath of posts) { - const totalPostPath = `posts/${postPath}`; - if (!postPath.endsWith(".md")) { - await fs.copyFile(totalPostPath, `build/${postPath}`); - continue; + const posts = await fs.readdir("posts"); + for (let postPath of posts) { + const totalPostPath = `posts/${postPath}`; + if (!postPath.endsWith(".md")) { + await fs.copyFile(totalPostPath, `build/posts/${postPath}`); + continue; + } + const postId = postPath.replace(".md", ""); + const postText = await fs.readFile(totalPostPath, { encoding: "utf-8" }); + const [postHeader, postBody] = splitPostHeader(postText); + const postMetadata = yaml.parse(postHeader) as PostMetadata; + const postHtml = this.makeHtmlForPost(postMetadata, postBody); + const targetPath = `build/posts/${postId}.html`; + await fs.writeFile(targetPath, postHtml, { encoding: "utf-8" }); + if (!postMetadata.unlisted) + this.allPosts.push({ + name: postMetadata.name, + id: postId, + description: postMetadata.description ?? null, + }); + } + await fs.writeFile( + "build/posts/index.html", + this.makeHtmlForPost( + { name: "Post Index" }, + "# Post List\n" + + this.allPosts + .map( + (post) => + `## [${post.name}](./${post.id}.html)\n\n${post.description ?? ""}`, + ) + .join("\n"), + ), + { encoding: "utf-8" }, + ); } - const postText = await fs.readFile(totalPostPath, { encoding: "utf-8" }); - const [postHeader, postBody] = splitPostHeader(postText); - const postMetadata = yaml.parse(postHeader) as PostMetadata; - const postHtml = makeHtmlForPost(postMetadata, postBody); - const targetPath = `build/${postPath.replace(".md", ".html")}`; - fs.writeFile(targetPath, postHtml, { encoding: "utf-8" }); } + +await new PostCollector().run(); diff --git a/posts/about.md b/posts/about.md new file mode 100644 index 0000000..6293916 --- /dev/null +++ b/posts/about.md @@ -0,0 +1,12 @@ +--- +name: About Echo +description: About page +unlisted: true +--- + +I am echo, you can find me here: + + - Discord: [@exhq](https://discord.com/users/712639419785412668) + - Email: [infidelloler@gmail.com](mailto:infidelloler@gmail.com) + - IRL: 308 Negra Arroyo Lane + diff --git a/posts/my-first-felony.md b/posts/my-first-felony.md index eac6623..37b8fb0 100644 --- a/posts/my-first-felony.md +++ b/posts/my-first-felony.md @@ -1,10 +1,11 @@ --- -name: "whatever" +name: Test Post +description: This is just a test post date: today tags: - life-stories - nsfw -unlisted: true +unlisted: false --- # My first felony diff --git a/template.html b/template.html index d8abe1e..ef2d94f 100644 --- a/template.html +++ b/template.html @@ -10,7 +10,7 @@ <meta http-equiv="X-UA-Compatible" content="ie-edge" /> </head> <body> - <nav class="border fixed split-nav"> + <nav class="border split-nav"> <div class="nav-brand"> <h3><a href="#">ECHO</a></h3> </div> @@ -23,8 +23,8 @@ </label> <div class="collapsible-body"> <ul class="inline"> - <li><a href="#">About</a></li> - <li><a href="#">Blog</a></li> + <li><a href="./about.html">About</a></li> + <li><a href="./index.html">Blog</a></li> <li><a href="https://github.com/exhq">GitHub</a></li> </ul> </div> |