aboutsummaryrefslogtreecommitdiff
path: root/.scripts/transform.js
blob: b95c8339d9a35ca7a3fad28f294b2dc6e09f8e2e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { statSync, readFileSync, writeFileSync, readdirSync, rmSync } 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()) {
        if (blogFilePath.toLowerCase().includes("readme")) {
          rmSync(blogFilePath);
        }
        transform(blogFilePath);
      }
    });
  }
});