diff options
Diffstat (limited to '.scripts/transform.js')
-rw-r--r-- | .scripts/transform.js | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/.scripts/transform.js b/.scripts/transform.js new file mode 100644 index 0000000..87a5aec --- /dev/null +++ b/.scripts/transform.js @@ -0,0 +1,34 @@ +import { statSync, readFileSync, writeFileSync, readdirSync } from "fs"; +import { join } from "path"; + +function transform(filePath) { + const stats = statSync(filePath); + const lastWriteDate = new Date(stats.mtime).toISOString().split("T")[0]; + console.log(`Transforming ${filePath} last modified at ${lastWriteDate}`); + + let content = readFileSync(filePath, "utf-8"); + const title = content.split("\n")[0].substring(1).trim(); + if (title === "--") return; + console.log(`Title: ${title}`); + + content = `--- +title: "${title}" +pubDate: "${lastWriteDate}" +--- +${content}`; + + writeFileSync(filePath, content); +} + +const blogsDir = "./src/content/blog"; +readdirSync(blogsDir).forEach((blog) => { + const blogPath = join(blogsDir, blog); + if (statSync(blogPath).isDirectory()) { + readdirSync(blogPath).forEach((blogFile) => { + const blogFilePath = join(blogPath, blogFile); + if (statSync(blogFilePath).isFile()) { + transform(blogFilePath); + } + }); + } +}); |