diff options
Diffstat (limited to 'index.ts')
-rw-r--r-- | index.ts | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..eadab52 --- /dev/null +++ b/index.ts @@ -0,0 +1,45 @@ +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); + + return [ + postData.substring(4, headerEndIndex), + postData.substring(headerEndIndex + 5), + ]; + } + return ["", postData]; +} + +await fs.rm("build", { recursive: true }); +await fs.mkdir("build"); + +for (let postPath of posts) { + const totalPostPath = `posts/${postPath}`; + if (!postPath.endsWith(".md")) { + await fs.copyFile(totalPostPath, `build/${postPath}`); + continue; + } + 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 targetPath = `build/${postPath.replace(".md", ".html")}`; + fs.writeFile(targetPath, primitiveHtmlDocument, { encoding: "utf-8" }); +} |