Skip to content

Commit d6f99a1

Browse files
committed
refactor BlogPost component
1 parent d73a758 commit d6f99a1

File tree

2 files changed

+25
-37
lines changed

2 files changed

+25
-37
lines changed

src/components/BlogPost/index.tsx

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,44 @@
1-
import React from "react"
1+
import * as React from "react"
2+
import { graphql, Link } from "gatsby"
23
import Marked from "../Marked"
3-
import { Link } from "gatsby"
4+
5+
export const fragments = graphql`
6+
fragment BlogPost_post on BlogPost {
7+
title
8+
excerpt
9+
rawContent
10+
date
11+
authors
12+
tags
13+
}
14+
`;
415

516
interface Props {
6-
title: string
7-
date: string
8-
permalink: string
9-
byline: string
10-
guestBio: string
11-
rawMarkdownBody: string
12-
isPermalink: boolean
13-
pageContext: any
14-
excerpt?: string
15-
showExcerpt?: true
16-
tags: Array<string>
17+
post: GatsbyTypes.BlogPost_postFragment,
18+
showExcerpt: boolean,
1719
}
1820

19-
const BlogPost = ({
20-
title,
21-
date,
22-
permalink,
23-
byline,
24-
guestBio,
25-
rawMarkdownBody,
26-
isPermalink,
27-
pageContext,
28-
excerpt,
21+
const BlogPost: React.FC<Props> = ({
22+
post,
2923
showExcerpt,
30-
tags,
31-
}: Props) => (
24+
}) => (
3225
<div className="inner-content">
33-
<h1>{isPermalink ? title : <a href={permalink}>{title}</a>}</h1>
26+
<h1>{post.title}</h1>
3427
<p>
35-
{new Date(date).toLocaleDateString()} by {byline}
28+
{new Date(post.date).toLocaleDateString()} by {post.authors.join(', ')}
3629
</p>
3730
<div className="tag-wrapper">
38-
{tags.map(tag => (
39-
<span className="tag">
31+
{post.tags.map(tag => (
32+
<span key={tag} className="tag">
4033
<Link to={`/tags/${tag}`}>{tag}</Link>
4134
</span>
4235
))}
4336
</div>
4437

45-
{guestBio ? null : <hr />}
46-
{guestBio && (
47-
<p className="guestBio">{`This guest article contributed by ${byline}, ${guestBio}.`}</p>
48-
)}
49-
5038
{showExcerpt ? (
51-
<p>{excerpt}</p>
39+
<p>{post.excerpt}</p>
5240
) : (
53-
<Marked pageContext={pageContext}>{rawMarkdownBody}</Marked>
41+
<Marked>{post.rawContent}</Marked>
5442
)}
5543
</div>
5644
)

src/components/Marked/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import MiniGraphiQL from "./MiniGraphiQL"
1414
import { StarWarsSchema } from "./swapiSchema"
1515
import { UsersSchema } from './usersSchema';
1616

17-
export default function Marked(props: { children: React.ReactElement }) {
17+
export default function Marked(props: { children: string }) {
1818
return <div>{props.children && marked(props.children, props)}</div>
1919
}
2020

0 commit comments

Comments
 (0)