Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

feat: Use MDX v2 #45

Merged
merged 15 commits into from
Aug 15, 2022
3 changes: 3 additions & 0 deletions .github/workflows/e2e-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,8 @@ jobs:
run: npx playwright install chromium --with-deps
- name: Build Gatsby Site
run: yarn build
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
FLICKR_API_KEY: ${{ secrets.FLICKR_API_KEY }}
- name: Run Playwright
run: yarn e2e:build
8 changes: 5 additions & 3 deletions packages/core/gatsby-config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { GatsbyConfig, PluginOptions } from "gatsby"
import remarkSlug from "remark-slug"
import remarkSmartyPants from "remark-smartypants"
import remarkUnwrapImages from "remark-unwrap-images"
import camelCase from "lodash.camelcase"
import { withDefaults, capitalize } from "utils"

Expand Down Expand Up @@ -35,7 +36,7 @@ const gatsbyConfig = (themeOptions: PluginOptions): GatsbyConfig => {
{
resolve: `gatsby-source-filesystem`,
options: {
name: `src/pages`,
name: `pages`,
path: `src/pages`,
},
},
Expand Down Expand Up @@ -73,7 +74,6 @@ const gatsbyConfig = (themeOptions: PluginOptions): GatsbyConfig => {
{
resolve: `gatsby-plugin-mdx`,
options: {
lessBabel: true,
extensions: [`.mdx`, `.md`],
gatsbyRemarkPlugins: [
{
Expand All @@ -85,7 +85,9 @@ const gatsbyConfig = (themeOptions: PluginOptions): GatsbyConfig => {
},
},
],
remarkPlugins: [remarkSlug, remarkSmartyPants],
mdxOptions: {
remarkPlugins: [remarkSlug, remarkSmartyPants, remarkUnwrapImages],
},
},
},
`gatsby-transformer-sharp`,
Expand Down
70 changes: 36 additions & 34 deletions packages/core/gatsby-node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CreateNodeArgs, GatsbyNode, PluginOptions } from "gatsby"
import Prando from "prando"
import get from "lodash.get"
import readingTime from "reading-time"
import { mdxResolverPassthrough, slugify, withDefaults, shuffle } from "utils"

export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"] = ({ actions }): void => {
Expand Down Expand Up @@ -47,8 +48,6 @@ export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"]
id: ID!
slug: String! @slugify(fieldName: "category")
excerpt(pruneLength: Int = 160): String!
body: String!
html: String
tableOfContents: JSON
timeToRead: Int
image: String
Expand All @@ -60,15 +59,14 @@ export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"]
subtitle: String
title: String!
type: PostTypeEnum!
contentFilePath: String!
}

type MdxPost implements Node & Post {
slug: String! @slugify(fieldName: "category")
excerpt(pruneLength: Int = 140): String! @mdxpassthrough(fieldName: "excerpt")
body: String! @mdxpassthrough(fieldName: "body")
html: String! @mdxpassthrough(fieldName: "html")
tableOfContents: JSON @mdxpassthrough(fieldName: "tableOfContents")
timeToRead: Int @mdxpassthrough(fieldName: "timeToRead")
timeToRead: Int
image: String
category: Category! @link(by: "name")
date: Date! @dateformat
Expand All @@ -78,6 +76,7 @@ export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"]
subtitle: String
title: String!
type: PostTypeEnum!
contentFilePath: String!
}

type Category implements Node {
Expand All @@ -93,27 +92,25 @@ export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"]
id: ID!
slug: String! @slugify(fallback: "garden")
excerpt(pruneLength: Int = 160): String!
body: String!
html: String
timeToRead: Int
date: Date! @dateformat
lastUpdated: Date! @dateformat
title: String!
tags: [String!]!
icon: String!
contentFilePath: String!
}

type MdxGarden implements Node & Garden {
slug: String! @slugify(fallback: "garden")
excerpt(pruneLength: Int = 140): String! @mdxpassthrough(fieldName: "excerpt")
body: String! @mdxpassthrough(fieldName: "body")
html: String! @mdxpassthrough(fieldName: "html")
timeToRead: Int @mdxpassthrough(fieldName: "timeToRead")
timeToRead: Int
date: Date! @dateformat
lastUpdated: Date! @dateformat
title: String!
tags: [String!]!
icon: String!
contentFilePath: String!
}

type CoreConfig implements Node {
Expand Down Expand Up @@ -179,29 +176,29 @@ export const sourceNodes: GatsbyNode["sourceNodes"] = ({ actions, createContentD
}

type WritingNode = {
frontmatter: {
slug?: string
image?: string
category: "Community" | "Design" | "Gatsby" | "JavaScript" | "React"
date: string
lastUpdated?: string
description: string
published: boolean
subtitle?: string
title: string
type: "prose" | "tutorial"
}
slug?: string
image?: string
category: "Community" | "Design" | "Gatsby" | "JavaScript" | "React"
date: string
lastUpdated?: string
description: string
published: boolean
subtitle?: string
title: string
type: "prose" | "tutorial"
contentFilePath: string
timeToRead: number
}

type GardenNode = {
frontmatter: {
slug?: string
date: string
lastUpdated?: string
title: string
tags: string[]
icon: string
}
slug?: string
date: string
lastUpdated?: string
title: string
tags: string[]
icon: string
contentFilePath: string
timeToRead: number
}

type MdxNode = WritingNode | GardenNode
Expand All @@ -219,10 +216,11 @@ export const onCreateNode = (

const fileNode = getNode(node.parent)
const source = fileNode.sourceInstanceName
const timeToRead = Math.round(readingTime(node.body as string).minutes)

if (source === writingSource) {
const f = node.frontmatter as WritingNode["frontmatter"]
const fieldData: WritingNode["frontmatter"] = {
const f = node.frontmatter as WritingNode
const fieldData: WritingNode = {
slug: f.slug ? f.slug : undefined,
title: f.title,
subtitle: f.subtitle ? f.subtitle : undefined,
Expand All @@ -233,6 +231,8 @@ export const onCreateNode = (
description: f.description,
published: f.published ?? true,
type: f.type,
contentFilePath: fileNode.absolutePath as string,
timeToRead,
}

const mdxPostId = createNodeId(`${node.id} >>> MdxPost`)
Expand All @@ -254,14 +254,16 @@ export const onCreateNode = (
}

if (source === gardenSource) {
const f = node.frontmatter as GardenNode["frontmatter"]
const fieldData: GardenNode["frontmatter"] = {
const f = node.frontmatter as GardenNode
const fieldData: GardenNode = {
slug: f.slug ? f.slug : undefined,
title: f.title,
date: f.date,
lastUpdated: f.lastUpdated ? f.lastUpdated : f.date,
icon: f.icon,
tags: f.tags,
contentFilePath: fileNode.absolutePath as string,
timeToRead,
}

const mdxGardenId = createNodeId(`${node.id} >>> MdxGarden`)
Expand Down
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
"gatsby": "^4.0.0"
},
"dependencies": {
"@mdx-js/mdx": "^1.6.22",
"@mdx-js/react": "^1.6.22",
"@mdx-js/react": "^2.1.2",
"gatsby-plugin-catch-links": "next",
"gatsby-plugin-mdx": "next",
"gatsby-plugin-sharp": "next",
Expand All @@ -23,6 +22,7 @@
"prando": "^6.0.1",
"remark-slug": "^6.0.0",
"remark-smartypants": "^0.0.1",
"remark-unwrap-images": "^2.0.0",
"ts-node": "^10.7.0",
"utils": "^0.0.1"
},
Expand Down
2 changes: 1 addition & 1 deletion playwright/meta.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const metaTagAssertions = [
},
{
key: `og:description`,
value: `You're a fan of Plausible Analytics and Gatsby ? Great! In this guide you'll learn how to add Plausible Analytics to your Gatsby site…`,
value: `Youre a fan of Plausible Analytics and Gatsby? Great! In this guide youll learn how to add Plausible Analytics to your Gatsby site…`,
},
{
key: `og:image`,
Expand Down
10 changes: 7 additions & 3 deletions www/gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ type CreatePagesResult = {
nodes: {
id: string
slug: string
contentFilePath: string
}[]
}
writing: {
nodes: {
id: string
slug: string
type: "prose" | "tutorial"
contentFilePath: string
}[]
}
}
Expand All @@ -42,13 +44,15 @@ export const createPages: GatsbyNode["createPages"] = async ({ graphql, actions,
nodes {
id
slug
contentFilePath
}
}
writing: allPost(filter: { published: { eq: true } }) {
nodes {
id
slug
type
contentFilePath
}
}
}
Expand All @@ -70,19 +74,19 @@ export const createPages: GatsbyNode["createPages"] = async ({ graphql, actions,
garden.nodes.forEach((post) => {
createPage({
path: post.slug,
component: gardenTemplate,
component: `${gardenTemplate}?__contentFilePath=${post.contentFilePath}`,
context: {
id: post.id,
},
})
})

writing.nodes.forEach((article) => {
const component = article.type === `tutorial` ? tutorialTemplate : proseTemplate
const writingTemplate = article.type === `tutorial` ? tutorialTemplate : proseTemplate

createPage({
path: article.slug,
component,
component: `${writingTemplate}?__contentFilePath=${article.contentFilePath}`,
context: {
id: article.id,
},
Expand Down
11 changes: 6 additions & 5 deletions www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"clean": "gatsby clean"
},
"dependencies": {
"@chakra-ui/gatsby-plugin": "^3.0.0",
"@chakra-ui/react": "^2.0.2",
"@chakra-ui/system": "^2.0.2",
"@chakra-ui/theme-tools": "^2.0.0",
"@chakra-ui/gatsby-plugin": "3.0.1",
"@chakra-ui/react": "2.2.4",
"@chakra-ui/system": "2.2.2",
"@chakra-ui/theme-tools": "2.0.5",
"@emotion/react": "^11.9.0",
"@emotion/styled": "^11.8.1",
"@lekoarts/gatsby-source-flickr": "^0.0.1",
Expand All @@ -33,6 +33,7 @@
"query-string": "^7.1.1",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-icons": "^4.3.1"
"react-icons": "^4.3.1",
"reading-time": "^1.5.0"
}
}
4 changes: 2 additions & 2 deletions www/src/components/art/flickr-3d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ export const Flickr3D = () => {
key={img.photoId}
alt={img.description}
photoId={img.photoId}
height={img.imageUrls.image.height}
width={img.imageUrls.image.width}
height={img.imageUrls?.image?.height}
width={img.imageUrls?.image?.width}
src={img.imageUrls.image.url}
title={img.title}
/>
Expand Down
4 changes: 2 additions & 2 deletions www/src/components/art/flickr-design.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ export const FlickrDesign = () => {
key={img.photoId}
alt={img.description}
photoId={img.photoId}
height={img.imageUrls.image.height}
width={img.imageUrls.image.width}
height={img.imageUrls?.image?.height}
width={img.imageUrls?.image?.width}
src={img.imageUrls.image.url}
title={img.title}
/>
Expand Down
4 changes: 2 additions & 2 deletions www/src/components/art/flickr-photography.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ export const FlickrPhotography = () => {
key={img.photoId}
alt={img.description}
photoId={img.photoId}
height={img.imageUrls.image.height}
width={img.imageUrls.image.width}
height={img.imageUrls?.image?.height}
width={img.imageUrls?.image?.width}
src={img.imageUrls.image.url}
title={img.title}
/>
Expand Down
1 change: 0 additions & 1 deletion www/src/components/mdx/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ export const components = {
Alert,
Collapsible,
Video,
wrapper: ({ children }) => children,
...headings,
}
Loading