Skip to content

Commit c7c65c9

Browse files
author
wutali
committed
Refactor content
1 parent 35e5c45 commit c7c65c9

File tree

7 files changed

+26
-12
lines changed

7 files changed

+26
-12
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
"dependencies": {
1313
"@mapbox/rehype-prism": "^0.5.0",
1414
"@svgr/webpack": "^5.4.0",
15+
"@types/js-yaml": "^3.12.5",
1516
"date-fns": "^2.11.1",
1617
"gray-matter": "^4.0.2",
18+
"js-yaml": "^3.14.0",
1719
"next": "9.5.1",
1820
"next-mdx-enhanced": "^3.1.0",
1921
"normalize.css": "^8.0.1",

src/components/Date.tsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { parseISO, formatISO, format } from "date-fns";
1+
import { format, formatISO } from "date-fns";
22

3-
export default function Date({ dateString }: { dateString: string }) {
4-
const date = parseISO(dateString);
3+
type Props = {
4+
date: Date;
5+
};
6+
export default function Date({ date }: Props) {
57
return (
68
<time dateTime={formatISO(date)}>
79
<span>{format(date, "LLLL d, yyyy")}</span>

src/components/PostItem.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { PostContent } from "../lib/posts";
22
import Date from "./Date";
33
import Link from "next/link";
4+
import { parseISO } from "date-fns";
45

56
type Props = {
67
post: PostContent;
@@ -9,7 +10,7 @@ export default function PostItem({ post }: Props) {
910
return (
1011
<Link href={"/posts/" + post.slug}>
1112
<a>
12-
<Date dateString={post.date} />
13+
<Date date={parseISO(post.date)} />
1314
<h2>{post.title}</h2>
1415
<style jsx>
1516
{`

src/components/meta/JsonLdMeta.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ type Props = {
77
url: string;
88
title: string;
99
keywords?: string[];
10-
dateString: string;
10+
date: Date;
1111
author?: string;
1212
image?: string;
1313
description?: string;
@@ -16,12 +16,11 @@ export default function JsonLdMeta({
1616
url,
1717
title,
1818
keywords,
19-
dateString,
19+
date,
2020
author,
2121
image,
2222
description,
2323
}: Props) {
24-
const date = parseISO(dateString);
2524
return (
2625
<script
2726
{...jsonLdScriptProps<BlogPosting>({

src/layouts/index.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { getTag } from "../lib/tags";
1616

1717
type Props = {
1818
title: string;
19-
date: string;
19+
date: Date;
2020
slug: string;
2121
description: string;
2222
tags: string[];
@@ -56,7 +56,7 @@ export default function Index({
5656
url={`/posts/${slug}`}
5757
title={title}
5858
keywords={keywords}
59-
dateString={date}
59+
date={date}
6060
author={authorName}
6161
description={description}
6262
/>
@@ -67,7 +67,7 @@ export default function Index({
6767
<h1>{title}</h1>
6868
<div className={"metadata"}>
6969
<div>
70-
<Date dateString={date} />
70+
<Date date={date} />
7171
</div>
7272
<div>
7373
<Author author={getAuthor(author)} />

src/lib/posts.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from "fs";
22
import matter from "gray-matter";
33
import path from "path";
4+
import yaml from "js-yaml";
45

56
const postsDirectory = path.join(process.cwd(), "src/pages/posts");
67

@@ -27,7 +28,11 @@ function fetchPostContent(): PostContent[] {
2728
const fileContents = fs.readFileSync(fullPath, "utf8");
2829

2930
// Use gray-matter to parse the post metadata section
30-
const matterResult = matter(fileContents);
31+
const matterResult = matter(fileContents, {
32+
engines: {
33+
yaml: (s) => yaml.safeLoad(s, { schema: yaml.JSON_SCHEMA }),
34+
},
35+
});
3136
const matterData = matterResult.data as {
3237
date: string;
3338
title: string;

yarn.lock

+6-1
Original file line numberDiff line numberDiff line change
@@ -1727,6 +1727,11 @@
17271727
jest-diff "^25.2.1"
17281728
pretty-format "^25.2.1"
17291729

1730+
"@types/js-yaml@^3.12.5":
1731+
version "3.12.5"
1732+
resolved "https://registry.yarnpkg.com/@types/js-yaml/-/js-yaml-3.12.5.tgz#136d5e6a57a931e1cce6f9d8126aa98a9c92a6bb"
1733+
integrity sha512-JCcp6J0GV66Y4ZMDAQCXot4xprYB+Zfd3meK9+INSJeVZwJmHAW30BBEEkPzXswMXuiyReUGOP3GxrADc9wPww==
1734+
17301735
"@types/json-schema@^7.0.4":
17311736
version "7.0.5"
17321737
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.5.tgz#dcce4430e64b443ba8945f0290fb564ad5bac6dd"
@@ -5399,7 +5404,7 @@ jest@^26.3.0:
53995404
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
54005405
integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==
54015406

5402-
js-yaml@^3.11.0, js-yaml@^3.13.1:
5407+
js-yaml@^3.11.0, js-yaml@^3.13.1, js-yaml@^3.14.0:
54035408
version "3.14.0"
54045409
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.14.0.tgz#a7a34170f26a21bb162424d8adacb4113a69e482"
54055410
integrity sha512-/4IbIeHcD9VMHFqDR/gQ7EdZdLimOvW2DdcxFjdyyZ9NsbS+ccrXqVWDtab/lRl5AlUqmpBx8EhPaWR+OtY17A==

0 commit comments

Comments
 (0)