Skip to content

Commit 9b7f9f8

Browse files
committed
feat: add json-ld scripts for SEO
1 parent 2daef09 commit 9b7f9f8

File tree

5 files changed

+76
-6
lines changed

5 files changed

+76
-6
lines changed

app/[slug]/page.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import Giscus from '@/components/Giscus';
88
import { HomeIcon } from '@/components/icons';
99
import TableOfContent from '@/components/TableOfContent';
1010
import ZoomableImage from '@/components/ZoomableImage';
11-
import { allBlogPosts, parseToc } from '@/libs/post';
11+
import { allBlogPosts, contentToDescription, parseToc } from '@/libs/post';
12+
import { getPostJsonLD } from '@/libs/seo';
1213

1314
interface PostPageProps {
1415
params: {
@@ -26,10 +27,10 @@ export async function generateMetadata({ params }: PostPageProps): Promise<Metad
2627
const post = allBlogPosts.find((post) => post.slug === `/${params.slug}`);
2728

2829
if (!post) return {};
29-
30+
const description = contentToDescription(post.body.raw);
3031
return {
3132
title: `${post.title} | yuhwan park's blog`,
32-
description: post.title,
33+
description,
3334
alternates: {
3435
canonical: `https://yuhwan-park.github.io${post.slug}`,
3536
},
@@ -45,7 +46,7 @@ export async function generateMetadata({ params }: PostPageProps): Promise<Metad
4546
authors: ['yuhwan park'],
4647
title: post.title,
4748
url: `https://yuhwan-park.github.io${post.slug}`,
48-
description: post.title,
49+
description,
4950
},
5051
};
5152
}
@@ -66,6 +67,10 @@ export default function PostPage({ params }: PostPageProps) {
6667

6768
return (
6869
<main>
70+
<script
71+
type="application/ld+json"
72+
dangerouslySetInnerHTML={{ __html: JSON.stringify(getPostJsonLD(post)) }}
73+
/>
6974
<div className="blur-layer" aria-hidden />
7075
<TableOfContent data-animate className="px-2 text-sm" toc={toc} />
7176
<div data-animate>

app/layout.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const pretendard = localFont({
1919

2020
export const metadata: Metadata = {
2121
title: `yuhwan park's blog`,
22-
description: `yuhwan park's blog`,
22+
description: `안녕하세요. UX와 DX 두 마리 토끼를 다 잡고 싶은 만 2년차 프론트엔드 개발자입니다.`,
2323
robots: {
2424
index: true,
2525
follow: true,
@@ -28,7 +28,8 @@ export const metadata: Metadata = {
2828
openGraph: {
2929
title: `yuhwan park's blog`,
3030
url: 'https://yuhwan-park.github.io/',
31-
description: `yuhwan park's blog`,
31+
siteName: `yuhwan park's blog`,
32+
description: `안녕하세요. UX와 DX 두 마리 토끼를 다 잡고 싶은 만 2년차 프론트엔드 개발자입니다.`,
3233
},
3334
other: {
3435
['google-site-verification']: '1tOVlPGznTkcAVpMjZVMHwrVgJrkxd9IPKcfLHd0LWc',

app/page.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@ import Image from 'next/image';
33
import { EmailIcon, GithubIcon } from '@/components/icons';
44
import PostList from '@/components/PostList';
55
import { reducedAllBlogPosts } from '@/libs/post';
6+
import { getHomeJsonLD } from '@/libs/seo';
67

78
export default function Home() {
89
const posts = reducedAllBlogPosts;
910
return (
1011
<main data-animate>
12+
<script
13+
type="application/ld+json"
14+
dangerouslySetInnerHTML={{ __html: JSON.stringify(getHomeJsonLD()) }}
15+
/>
1116
<div className="mb-8">
1217
<b>박유환</b>
1318
</div>

libs/post.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,17 @@ export const parseToc = (source: string) => {
5959
return acc;
6060
}, []);
6161
};
62+
63+
export const contentToDescription = (content: string) => {
64+
const parsedContent = content
65+
.replace(/!\[.*?\]\(.*?\)/g, '')
66+
.replace(/(?<=\])\((.*?)\)/g, '')
67+
.replace(/(?<!\S)((http)(s?):\/\/|www\.).+?(?=\s)/g, '')
68+
.replace(/[#*\|\[\]]|(\-{3,})|(`{3})(\S*)(?=\s)/g, '')
69+
.replace(/<[^>]*>/g, '')
70+
.replace(/\s+/g, ' ')
71+
.trim()
72+
.slice(0, 130);
73+
74+
return `${parsedContent}...`;
75+
};

libs/seo.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { contentToDescription } from './post';
2+
import { Post } from './types';
3+
4+
export const getHomeJsonLD = () => {
5+
return {
6+
'@context': 'https://schema.org',
7+
'@type': 'Blog',
8+
name: "yuhwan park's blog",
9+
url: 'https://yuhwan-park.github.io/',
10+
description: `안녕하세요. UX와 DX 두 마리 토끼를 다 잡고 싶은 만 2년차 프론트엔드 개발자입니다.`,
11+
author: {
12+
'@type': 'Person',
13+
name: 'yuhwan park',
14+
url: 'https://github.com/yuhwan-park',
15+
},
16+
datePublished: '2024-11-07',
17+
dateModified: '2024-11-07',
18+
inLanguage: 'ko',
19+
mainEntityOfPage: 'https://yuhwan-park.github.io/',
20+
copyrightYear: '2024',
21+
};
22+
};
23+
24+
export const getPostJsonLD = (post: Post) => {
25+
const description = contentToDescription(post.body.raw);
26+
return {
27+
'@context': 'https://schema.org',
28+
'@type': 'BlogPosting',
29+
headline: post.title,
30+
datePublished: post.date,
31+
dateModified: post.date,
32+
author: {
33+
'@type': 'Person',
34+
name: 'yuhwan park',
35+
url: 'https://github.com/yuhwan-park',
36+
},
37+
description,
38+
publisher: {
39+
'@type': 'Organization',
40+
name: "yuhwan park's blog",
41+
url: 'https://yuhwan-park.github.io/',
42+
},
43+
articleBody: post.body.raw,
44+
};
45+
};

0 commit comments

Comments
 (0)