47
loading...
This website collects cookies to deliver better user experience
@mdx-js/mdx
and @mdx-js/react
packages as well as Gatsby's official gatsby-plugin-mdx
and gatsby-plugin-feed-mdx
.npm install --save gatsby-plugin-mdx gatsby-plugin-feed-mdx @mdx-js/mdx @mdx-js/react
gatsby-transformer-remark
plugin by replacing it with gatsby-plugin-mdx
:{
- resolve: `gatsby-transformer-remark`,
+ resolve: `gatsby-plugin-mdx`,
options: {
+ extensions: [`.mdx`, `.md`],
- plugins: [
gatsbyRemarkPlugins: [ //added
{
resolve: `gatsby-remark-images`,
options: {
maxWidth: 630,
},
},
{
resolve: `gatsby-remark-responsive-iframe`,
options: {
wrapperStyle: `margin-bottom: 1.0725rem`,
},
},
`gatsby-remark-prismjs`,
`gatsby-remark-copy-linked-files`,
`gatsby-remark-smartypants`,
],
},
},
{
resolve: `gatsby-plugin-mdx`,
options: {
extensions: [`.mdx`, `.md`],
gatsbyRemarkPlugins: [
{
resolve: `gatsby-remark-images`,
options: {
maxWidth: 630,
},
},
{
resolve: `gatsby-remark-responsive-iframe`,
options: {
wrapperStyle: `margin-bottom: 1.0725rem`,
},
},
`gatsby-remark-prismjs`,
`gatsby-remark-copy-linked-files`,
`gatsby-remark-smartypants`,
],
},
},
gatsby-plugin-feed
with gatsby-plugin-feed-mdx
.- resolve: `gatsby-plugin-feed`,
+ resolve: `gatsby-plugin-feed-mdx`,
allMarkdownRemark
with allMDX
and replace html
with body
in the GraphQL query:resolve: `gatsby-plugin-feed-mdx`,
options: {
query: `
{
site {
siteMetadata {
title
description
siteUrl
site_url: siteUrl
}
}
}
`,
feeds: [
{
- serialize: ({ query: { site, allMarkdownRemark } }) => {
+ serialize: ({ query: { site, allMdx } }) => {
- return allMarkdownRemark.nodes.map(node => {
+ return allMdx.nodes.map(node => {
return Object.assign({}, node.frontmatter, {
description: node.excerpt,
date: node.frontmatter.date,
url: site.siteMetadata.siteUrl + node.fields.slug,
guid: site.siteMetadata.siteUrl + node.fields.slug,
custom_elements: [{ "content:encoded": node.html }],
})
})
},
query: `
{
- allMarkdownRemark(
+ allMdx(
sort: { order: DESC, fields: [frontmatter___date] },
) {
nodes {
excerpt
- html
+ body
fields {
slug
}
frontmatter {
title
date
}
}
}
}
`,
output: "/rss.xml",
title: "Jane Doe RSS Feed",
},
],
},
},
gatsby-transformer-remark
and gatsby-plugin-feed
are no longer used, you can uninstall them by running the following commands in the terminal:npm uninstall --save gatsby-transformer-remark gatsby-plugin-feed
const result = await graphql(
`
{
- allMarkdownRemark(
+ allMdx(
sort: { fields: [frontmatter___date], order: ASC }
limit: 1000
) {
nodes {
id
fields {
slug
}
}
}
}
`
)
const result = await graphql(
`
{
allMdx(
sort: { fields: [frontmatter___date], order: ASC }
limit: 1000
) {
nodes {
id
fields {
slug
}
}
}
}
`
)
- const posts = result.data.allMarkdownRemark.nodes
+ const posts = result.data.allMdx.nodes
onCreateNode
export:exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
- if (node.internal.type === `MarkdownRemark`) {
+ if (node.internal.type === `Mdx`) {
const value = createFilePath({ node, getNode })
createNodeField({
name: `slug`,
node,
value,
})
}
}
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
if (node.internal.type === `Mdx`) {
const value = createFilePath({ node, getNode })
createNodeField({
name: `slug`,
node,
value,
})
}
}
allMarkdownRemark
with allMdx
in the BlogIndex
functional component.- const posts = data.allMarkdownRemark.nodes
+ const posts = data.allMdx.nodes
- allMarkdownRemark(sort: { fields: [frontmatter___date], order: DESC }) {
+ allMdx(sort: { fields: [frontmatter___date], order: DESC }) {
export const pageQuery = graphql`
query {
site {
siteMetadata {
title
}
}
allMdx(sort: { fields: [frontmatter___date], order: DESC }) {
nodes {
excerpt
fields {
slug
}
frontmatter {
date(formatString: "MMMM DD, YYYY")
title
description
}
}
}
}
`
markdownRemark
with mdx
in the BlogPostTemplate
functional component:- const post = data.markdownRemark
+ const post = data.mdx
markdownRemark
with mdx
in the GraphQL query, and use body
instead of html
.export const pageQuery = graphql`
query BlogPostBySlug(
$id: String!
$previousPostId: String
$nextPostId: String
) {
site {
siteMetadata {
title
}
}
- markdownRemark(id: { eq: $id }) {
+ mdx(id: { eq: $id }) {
id
excerpt(pruneLength: 160)
- html
+ body
frontmatter {
title
date(formatString: "MMMM DD, YYYY")
description
}
}
- previous: markdownRemark(id: { eq: $previousPostId }) {
+ previous: mdx(id: { eq: $previousPostId }) {
fields {
slug
}
frontmatter {
title
}
}
- next: markdownRemark(id: { eq: $nextPostId }) {
+ next: mdx(id: { eq: $nextPostId }) {
fields {
slug
}
frontmatter {
title
}
}
}
`
export const pageQuery = graphql`
query BlogPostBySlug(
$id: String!
$previousPostId: String
$nextPostId: String
) {
site {
siteMetadata {
title
}
}
mdx(id: { eq: $id }) {
id
excerpt(pruneLength: 160)
body
frontmatter {
title
date(formatString: "MMMM DD, YYYY")
description
}
}
previous: mdx(id: { eq: $previousPostId }) {
fields {
slug
}
frontmatter {
title
}
}
next: mdx(id: { eq: $nextPostId }) {
fields {
slug
}
frontmatter {
title
}
}
}
`
MDXRenderer
at the top of the file:import * as React from "react"
import { Link, graphql } from "gatsby"
+ import { MDXRenderer } from "gatsby-plugin-mdx"
<section/>
element with the dangerouslySetInnerHTML
attribute and replace it with the MDXRenderer
component.- <section dangerouslySetInnerHTML={{ __html: post.html }}
- itemProp="articleBody"
- />
+ <MDXRenderer>{post.body}<MDXRenderer/>
---
title: "Hello MDX!"
date: "2021-10-25"
description: "The first post using MDX!"
---
import {Button} from './button.js'
This post is written in MDX, allowing you to embed a component after block of code which explains its creation!
js
here's a button in React!
<button onClick={alert("Hello MDX!")}>test</button>
Wow! Such button!
<Button>test</Button>
gatsby develop
in your terminal and check out your new post. The <Button>
component should be rendered as an element:gatsby build
and gatsby serve
, then navigate to localhost:9000/rss.xml. The RSS plugin does not generate a file in development mode, so you need to use a production build instead to test the functionality.