Skip to content

Commit

Permalink
changed implementation of updatedAt
Browse files Browse the repository at this point in the history
  • Loading branch information
kzhrk committed May 13, 2024
1 parent b359785 commit 0c396e9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
10 changes: 7 additions & 3 deletions server/api/posts/[...slug].ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { readFileSync, statSync } from "node:fs";
import { readFileSync } from "node:fs";
import { parse, Renderer } from "marked";
import { execSync } from "node:child_process"
import parseMD from "parse-md";

export default defineEventHandler(async (event) => {
const slug = getRouterParam(event, "slug");
const path = `./posts/${slug}.md`;
const file = readFileSync(path);
const stat = statSync(path);

const { metadata, content } = parseMD(file.toString()) as {
metadata: Metadata;
content: string;
};

const gitLog = execSync(`git log --no-merges --pretty=format:"%ad" --date=format:"%Y/%m/%d %H:%M:%S" ${path}`);
const updatedAt = gitLog.toString().split('\n')[0];

const renderer = new Renderer();
renderer.heading = (text, level) => {
const tag = `h${level}`;
Expand All @@ -30,7 +34,7 @@ export default defineEventHandler(async (event) => {
return {
...metadata,
createdAt: metadata.date,
updatedAt: stat.mtime,
updatedAt,
html,
description,
};
Expand Down
9 changes: 6 additions & 3 deletions server/api/posts/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { readFileSync, readdirSync, statSync } from "node:fs";
import { readFileSync, readdirSync } from "node:fs";
import parseMD from "parse-md";
import { execSync } from "node:child_process"

export default defineEventHandler(() => {
const posts = readdirSync("./posts").reverse();
return posts
.map((p) => {
const path = p.replace(/(\d+)-(\d+)-(\d+)-([\w|-]+)\.md/, "/$1/$2/$3/$4");
const file = readFileSync(`./posts/${p}`);
const stat = statSync(`./posts/${p}`);
const { metadata } = parseMD(file.toString()) as { metadata: Metadata };

const gitLog = execSync(`git log --no-merges --pretty=format:"%ad" --date=format:"%Y/%m/%d %H:%M:%S" posts/${p}`);
const updatedAt = gitLog.toString().split('\n')[0];

if (metadata.draft) return null;

return {
...metadata,
createdAt: metadata.date,
updatedAt: stat.mtime,
updatedAt,
path,
};
})
Expand Down

0 comments on commit 0c396e9

Please sign in to comment.