Skip to content

Commit 9c409ce

Browse files
committed
feat: enable head component and native typegen
1 parent 9b71a9f commit 9c409ce

File tree

22 files changed

+2610
-1941
lines changed

22 files changed

+2610
-1941
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,4 @@ yarn-error.log
7373
*.swp
7474

7575
# Codegen stuff
76-
src/__generated__/
76+
src/gatsby-types.d.ts

.prettierrc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"arrowParens": "avoid",
3-
"semi": false
4-
}
3+
"semi": false,
4+
"singleQuote": false
5+
}

gatsby-config.js

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ module.exports = {
55
"A query language for your API — GraphQL provides a complete description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.",
66
siteUrl: "http://graphql.org/",
77
},
8-
8+
graphqlTypegen: true,
99
plugins: [
10-
"gatsby-plugin-react-helmet",
11-
'gatsby-plugin-anchor-links',
10+
"gatsby-plugin-anchor-links",
1211
{
1312
resolve: "gatsby-source-filesystem",
1413
options: {
@@ -23,11 +22,11 @@ module.exports = {
2322
{
2423
resolve: "@weknow/gatsby-remark-twitter",
2524
options: {
26-
debug: true
27-
}
28-
}
29-
]
30-
}
25+
debug: true,
26+
},
27+
},
28+
],
29+
},
3130
},
3231
{
3332
resolve: `gatsby-plugin-webfonts`,
@@ -51,7 +50,6 @@ module.exports = {
5150
},
5251
},
5352
`gatsby-plugin-less`,
54-
`gatsby-plugin-react-helmet`,
5553
{
5654
resolve: `gatsby-plugin-google-analytics`,
5755
options: {
@@ -115,11 +113,5 @@ module.exports = {
115113
],
116114
},
117115
},
118-
{
119-
resolve: "gatsby-plugin-typegen",
120-
options: {
121-
outputPath: "src/__generated__/gatsby-types.d.ts",
122-
},
123-
},
124116
],
125117
}

gatsby-node.js

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ const { readFile } = require("fs-extra")
66
const { promisify } = require("util")
77

88
exports.createSchemaCustomization = ({ actions, schema }) => {
9-
const gql = String.raw;
10-
const { createTypes } = actions;
9+
const gql = String.raw
10+
const { createTypes } = actions
1111

1212
createTypes(gql`
13-
type BlogPost implements Node
14-
@childOf(types: ["MarkdownRemark"])
15-
{
13+
type BlogPost implements Node @childOf(types: ["MarkdownRemark"]) {
1614
postId: String!
1715
title: String!
1816
tags: [String!]!
@@ -21,8 +19,8 @@ exports.createSchemaCustomization = ({ actions, schema }) => {
2119
guestBio: String
2220
remark: MarkdownRemark! @link # backlink to the parent
2321
}
24-
`);
25-
};
22+
`)
23+
}
2624

2725
// Transform nodes, each of logic inside here can be extracted to a separated plugin later.
2826
exports.onCreateNode = async ({
@@ -32,60 +30,62 @@ exports.onCreateNode = async ({
3230
createNodeId,
3331
createContentDigest,
3432
}) => {
35-
const { createNode, createParentChildLink } = actions;
33+
const { createNode, createParentChildLink } = actions
3634

3735
// Derive content nodes from remark nodes
38-
if (node.internal.type === 'MarkdownRemark') {
39-
if (node.frontmatter.layout === 'blog') {
40-
const nodeId = createNodeId(`${node.id} >>> BlogPost`);
41-
42-
const permalink = node.frontmatter.permalink;
43-
if (!permalink?.startsWith('/blog/')) {
44-
reporter.panicOnBuild(`${permalink} is not valid permalink for blog post`);
45-
return;
36+
if (node.internal.type === "MarkdownRemark") {
37+
if (node.frontmatter.layout === "blog") {
38+
const nodeId = createNodeId(`${node.id} >>> BlogPost`)
39+
40+
const permalink = node.frontmatter.permalink
41+
if (!permalink?.startsWith("/blog/")) {
42+
reporter.panicOnBuild(
43+
`${permalink} is not valid permalink for blog post`
44+
)
45+
return
4646
}
4747

4848
// It contains a kind of transform logic. However, those logics can be extracted to resolvers into ahead of sourcing (createTypes)
4949
const blogPostContent = {
5050
id: nodeId,
51-
postId: permalink.replace('/blog/', '').replace(/\/$/, ''),
51+
postId: permalink.replace("/blog/", "").replace(/\/$/, ""),
5252
title: node.frontmatter.title,
5353
tags: node.frontmatter.tags ?? [],
5454
date: node.frontmatter.date,
55-
authors: (node.frontmatter.byline ?? '')
56-
.split(',')
55+
authors: (node.frontmatter.byline ?? "")
56+
.split(",")
5757
.map(name => name.trim())
5858
.filter(Boolean),
5959
guestBio: node.frontmatter.guestBio ?? null,
60-
};
60+
}
6161

6262
createNode({
6363
...blogPostContent,
6464
remark: node.id,
6565
parent: node.id,
6666
children: [],
6767
internal: {
68-
type: 'BlogPost',
68+
type: "BlogPost",
6969
contentDigest: createContentDigest(blogPostContent),
7070
},
71-
});
71+
})
7272

7373
createParentChildLink({
7474
parent: node,
7575
child: blogPostContent,
76-
});
76+
})
7777
}
7878
}
79-
};
79+
}
8080

8181
exports.onCreatePage = async ({ page, actions }) => {
8282
// trying to refactor code to be "the Gatsby way".
8383
// from the paths on ready, ignores a bunch of existing custom logic below.
84-
if (page.path.startsWith('/blog')) {
85-
return;
84+
if (page.path.startsWith("/blog")) {
85+
return
8686
}
87-
if (page.path.startsWith('/tags')) {
88-
return;
87+
if (page.path.startsWith("/tags")) {
88+
return
8989
}
9090

9191
const { createPage, deletePage } = actions
@@ -400,10 +400,8 @@ exports.createPages = async ({ graphql, actions }) => {
400400
let i = 0
401401
while (page && i++ < 1000) {
402402
const { frontmatter } = page
403-
const {
404-
category: definedCategory,
405-
next: definedNextPageUrl,
406-
} = frontmatter
403+
const { category: definedCategory, next: definedNextPageUrl } =
404+
frontmatter
407405
let category = definedCategory || folder
408406
if (!currentCategory || category !== currentCategory.name) {
409407
if (currentCategory) {
@@ -444,27 +442,27 @@ exports.createPages = async ({ graphql, actions }) => {
444442
// Use all the set up data to now tell Gatsby to create pages
445443
// on the site
446444
allPages
447-
.filter(page => !page.permalink.startsWith('/blog'))
445+
.filter(page => !page.permalink.startsWith("/blog"))
448446
.forEach(page => {
449-
createPage({
450-
path: `${page.permalink}`,
451-
component: docTemplate,
452-
context: {
453-
permalink: page.permalink,
454-
nextPermalink: page.nextPermalink,
455-
sideBarData: sideBardata[page.relativeDirectory],
456-
sourcePath: page.sourcePath,
457-
},
447+
createPage({
448+
path: `${page.permalink}`,
449+
component: docTemplate,
450+
context: {
451+
permalink: page.permalink,
452+
nextPermalink: page.nextPermalink,
453+
sideBarData: sideBardata[page.relativeDirectory],
454+
sourcePath: page.sourcePath,
455+
},
456+
})
458457
})
459-
})
460458
}
461459

462460
exports.onCreateWebpackConfig = ({ actions }) => {
463461
actions.setWebpackConfig({
464462
resolve: {
465463
fallback: {
466-
"assert": require.resolve("assert/"),
467-
}
468-
}
464+
assert: require.resolve("assert/"),
465+
},
466+
},
469467
})
470468
}

package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@
2121
"assert": "2.0.0",
2222
"codemirror": "5.65.1",
2323
"codemirror-graphql": "1.2.11",
24-
"gatsby": "4.7.2",
24+
"gatsby": "4.24.8",
2525
"gatsby-plugin-anchor-links": "1.2.1",
2626
"gatsby-plugin-feed": "4.7.0",
2727
"gatsby-plugin-google-analytics": "4.7.0",
2828
"gatsby-plugin-less": "6.7.0",
29-
"gatsby-plugin-react-helmet": "5.7.0",
3029
"gatsby-plugin-typegen": "2.2.4",
3130
"gatsby-plugin-webfonts": "2.2.1",
3231
"gatsby-source-filesystem": "4.7.0",
@@ -40,13 +39,11 @@
4039
"prismjs": "1.25.0",
4140
"react": "17.0.2",
4241
"react-dom": "17.0.2",
43-
"react-helmet": "6.1.0",
4442
"timeago.js": "4.0.2"
4543
},
4644
"devDependencies": {
4745
"@types/codemirror": "5.60.5",
4846
"@types/prismjs": "1.16.6",
49-
"@types/react-helmet": "6.1.4",
5047
"prettier": "2.4.1"
5148
}
52-
}
49+
}

src/components/BlogLayout/index.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@ export const fragments = graphql`
77
fragment BlogLayout_post on BlogPost {
88
...BlogPost_post
99
}
10-
`;
10+
`
1111

1212
interface Props {
13-
post: GatsbyTypes.BlogLayout_postFragment,
13+
post: Queries.BlogLayout_postFragment
1414
}
1515

16-
const BlogLayout: React.FC<Props> = ({
17-
post,
18-
}) => {
16+
const BlogLayout: React.FC<Props> = ({ post }) => {
1917
return (
2018
<section>
2119
<div className="documentationContent">

src/components/BlogPost/index.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,14 @@ export const fragments = graphql`
1313
rawMarkdownBody
1414
}
1515
}
16-
`;
16+
`
1717

1818
interface Props {
19-
post: GatsbyTypes.BlogPost_postFragment,
19+
post: Queries.BlogPost_postFragment
2020
}
2121

22-
const BlogPost: React.FC<Props> = ({
23-
post,
24-
}) => {
25-
const byline = post.authors.join(', ')
22+
const BlogPost: React.FC<Props> = ({ post }) => {
23+
const byline = post.authors.join(", ")
2624
return (
2725
<div className="inner-content">
2826
<h1>{post.title}</h1>

src/components/BlogPostPreview/index.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,20 @@ export const fragments = graphql`
1212
excerpt
1313
}
1414
}
15-
`;
15+
`
1616

1717
interface Props {
18-
post: GatsbyTypes.BlogPostPreview_postFragment,
18+
post: Queries.BlogPostPreview_postFragment
1919
}
2020

21-
const BlogPostPreview: React.FC<Props> = ({
22-
post,
23-
}) => (
21+
const BlogPostPreview: React.FC<Props> = ({ post }) => (
2422
<div className="inner-content">
2523
<h1>
2624
<Link to={post.postPath!}>{post.title}</Link>
2725
</h1>
2826

2927
<p>
30-
{new Date(post.date).toLocaleDateString()} by {post.authors.join(', ')}
28+
{new Date(post.date).toLocaleDateString()} by {post.authors.join(", ")}
3129
</p>
3230

3331
<div className="tag-wrapper">

src/components/BlogSidebar/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import { Link, useStaticQuery, graphql } from "gatsby"
33
import { useLocation } from "@reach/router"
44

55
const BlogSidebar: React.FC = () => {
6-
const data = useStaticQuery<GatsbyTypes.AllTagsStaticQuery>(graphql`
6+
const data = useStaticQuery<Queries.AllTagsStaticQuery>(graphql`
77
query AllTagsStatic {
88
allBlogPost {
99
group(field: tags) {
1010
fieldValue
1111
}
1212
}
1313
allRecentBlogPost: allBlogPost(
14-
limit: 30,
14+
limit: 30
1515
sort: { fields: [date], order: DESC }
1616
) {
1717
nodes {
@@ -26,7 +26,7 @@ const BlogSidebar: React.FC = () => {
2626
const tags = data.allBlogPost.group.map(group => group.fieldValue!)
2727
const recentPosts = data.allRecentBlogPost.nodes
2828

29-
const { pathname: currentPath } = useLocation();
29+
const { pathname: currentPath } = useLocation()
3030

3131
return (
3232
<div className="nav-docs blog-sidebar">
@@ -41,7 +41,7 @@ const BlogSidebar: React.FC = () => {
4141
<ul>
4242
{tags.map(tag => {
4343
const formattedTag = tag[0].toUpperCase() + tag.substring(1)
44-
const pathname = `/tags/${tag}/`;
44+
const pathname = `/tags/${tag}/`
4545
return (
4646
<li key={tag}>
4747
{pathname === currentPath ? (

src/components/Layout/index.tsx

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,20 @@
11
import React from "react"
22
import Footer from "../Footer"
33
import Header from "../Header"
4-
import Seo from "../Seo"
54

65
import "../../assets/css/style.less"
76

8-
interface PageContext {
9-
sourcePath?: string
10-
}
11-
127
interface Props {
138
children: React.ReactNode
14-
title?: string
15-
description?: string
169
className?: string
17-
pageContext: PageContext
10+
pageContext?: { sourcePath?: string }
1811
}
1912
const IndexLayout = ({
20-
title,
21-
description,
2213
children,
2314
className,
24-
pageContext: { sourcePath },
15+
pageContext: { sourcePath } = {},
2516
}: Props): JSX.Element => (
2617
<>
27-
<Seo title={title} description={description} />
2818
<div className={className}>
2919
<Header />
3020
{children}

0 commit comments

Comments
 (0)