Skip to content

Commit

Permalink
Revert implementation of updatedAt
Browse files Browse the repository at this point in the history
This reverts commit

- 0c396e9
- 005d7cc
  • Loading branch information
kzhrk committed May 14, 2024
1 parent 63e3583 commit 647d812
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 41 deletions.
13 changes: 4 additions & 9 deletions pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,11 @@ useHead(() => ({
{{ post.title }}
</nuxt-link>
</h1>
<div class="mt-2">
<ul class="flex flex-col gap-y-1">
<li class="text-sm">作成日: <time :datetime="post.createdAt">{{ getFormatedDate(post.createdAt) }}</time></li>
<li class="text-sm">更新日: <time :datetime="post.updatedAt">{{ getFormatedDate(post.updatedAt) }}</time></li>
<div class="mt-2 flex items-center">
<time class="text-sm" :datetime="post.date">{{ getFormatedDate(post.date) }}</time>
<ul v-if="post.tags" v-for="(tag, i) in post.tags" :key="i" class="flex gap-4 items-center ml-4">
<li>
<ul v-if="post.tags" v-for="(tag, i) in post.tags" :key="i" class="flex gap-4 items-center">
<li>
<nuxt-link :to="`/?tags=${tag}`" class="text-xs block px-2 py-1 text-gray-700 bg-blue-100 hover:bg-blue-200">{{ tag }}</nuxt-link>
</li>
</ul>
<nuxt-link :to="`/?tags=${tag}`" class="text-xs block px-2 py-1 text-gray-700 bg-blue-100 hover:bg-blue-200">{{ tag }}</nuxt-link>
</li>
</ul>
</div>
Expand Down
27 changes: 10 additions & 17 deletions pages/posts/[...slug].vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,21 @@ const route = useRoute();
const slug = route.params.slug as string[];
const apiParam = slug.join("-");
const { data: post } = await useFetch(`/api/posts/${apiParam}`, {
const { data } = await useFetch(`/api/posts/${apiParam}`, {
onResponseError() {
return navigateTo("/404");
},
});
const title = post.value.title;
const title = data.value.title;
const metaTitle = `${title} | blog.kzhrk.com`;
const html = post.value.html;
const description = post.value.description;
const tags = post.value.tags as string[] | undefined;
const html = data.value.html;
const date = data.value.date;
const description = data.value.description;
const tags = data.value.tags as string[] | undefined;
const formatedDate = format(date, "yyyy年M月d日");
const url = `https://blog.kzhrk.com/posts/${slug.join("/")}`;
function getFormatedDate(dateString: string) {
return format(dateString, "yyyy年M月d日");
}
useHead(() => ({
title: metaTitle,
meta: [
Expand Down Expand Up @@ -62,15 +60,10 @@ onMounted(() => {
<section class="px-6 py-12 sm:p-12">
<h1 class="mb-4 text-3xl font-bold">{{ title }}</h1>
<div class="mb-10 flex items-center">
<ul class="flex flex-col gap-y-1">
<li class="text-sm">作成日: <time :datetime="post.createdAt">{{ getFormatedDate(post.createdAt) }}</time></li>
<li class="text-sm">更新日: <time :datetime="post.updatedAt">{{ getFormatedDate(post.updatedAt) }}</time></li>
<time :datetime="date" class="text-sm text-gray-600 dark:text-gray-200">{{ formatedDate }}</time>
<ul v-if="tags" v-for="(tag, i) in tags" :key="i" class="flex gap-4 items-center ml-4">
<li>
<ul v-if="post.tags" v-for="(tag, i) in post.tags" :key="i" class="flex gap-4 items-center">
<li>
<nuxt-link :to="`/?tags=${tag}`" class="text-xs block px-2 py-1 text-gray-700 bg-blue-100 hover:bg-blue-200">{{ tag }}</nuxt-link>
</li>
</ul>
<nuxt-link :to="`/?tags=${tag}`" class="text-sm block px-2 py-1 text-gray-700 bg-blue-100 hover:bg-blue-200">{{ tag }}</nuxt-link>
</li>
</ul>
</div>
Expand Down
8 changes: 0 additions & 8 deletions server/api/posts/[...slug].ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
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 { 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 @@ -33,8 +27,6 @@ export default defineEventHandler(async (event) => {
.substring(0, 100)}...`;
return {
...metadata,
createdAt: metadata.date,
updatedAt,
html,
description,
};
Expand Down
8 changes: 1 addition & 7 deletions server/api/posts/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
import { readFileSync, readdirSync } from "node:fs";
import parseMD from "parse-md";
import { execSync } from "node:child_process"

export default defineEventHandler(() => {
export default defineEventHandler(async () => {
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 { 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,
path,
};
})
Expand Down

0 comments on commit 647d812

Please sign in to comment.