34
loading...
This website collects cookies to deliver better user experience
.md
file, as the final result is HTML the compiler will process it as HTML..md
files. Basically, it converts the markdown files into React components making it eligible for importing and rendering other components.gatsby-plugin-mdx
plugin with its dependencies (@mdx-js/mdx
and @mdx-js/react
) .npm install gatsby-plugin-mdx @mdx-js/mdx @mdx-js/react
gatsby-transformer-remark
plugin.npm remove gatsby-transformer-remark
gatsby-config.js
file, Replace plugin gatsby-transformer-remark
with gatsby-plugin-mdx
.--------------------
||gatsby-config.js||
--------------------
- resolve: `gatsby-transformer-remark`
+ resolve: `gatsby-plugin-mdx`
options: {
plugins
option to gatsbyRemarkPlugins
to let MDX know that these are Remark plugins.-------------------------
||gatsby-config.js||
-------------------------
resolve: `gatsby-plugin-mdx`
options: {
- plugins: [
+ gatsbyRemarkPlugins: [
gatsby-remark-images
plugin as both a sub-plugin of gatsby-plugin-mdx
and a string entry in the plugins array.-------------------------
||gatsby-config.js||
-------------------------
module.exports = {
plugins: [
`gatsby-remark-images`,
{
resolve: `gatsby-source-filesystem`,
options: {
path: `${__dirname}/content/blog`,
name: `blog`,
},
},
{
resolve: `gatsby-source-filesystem`,
options: {
path: `${__dirname}/content/assets`,
name: `assets`,
},
},
{
resolve: `gatsby-plugin-mdx`,
options: {
extensions: [`.md`, `.mdx`],
gatsbyRemarkPlugins: [
{
resolve: `gatsby-remark-images`,
options: {
maxWidth: 590,
linkImagesToOriginal: true,
},
},
{
resolve: `gatsby-remark-copy-linked-files`,
},
{
resolve: `gatsby-remark-smartypants`,
},
{
resolve: `gatsby-plugin-feed`,
},
],
},
},
{
resolve: `gatsby-plugin-sharp`,
},
.md
to .mdx
. This will allow MDX to recognize and process them with specified configurations..md
and .mdx
files by adding the extensions
option in gatsby-plugin-mdx
like this.-------------------------
||gatsby-config.js||
-------------------------
{
resolve: `gatsby-plugin-mdx`,
options: {
extensions: [`.md`, `.mdx`],
},
},
Tip: If you use Vs-code as your code editor. Use this MDX extention for syntax highlighting and bracket matching in MDX files.
createPages
API, Replace allMarkdownRemark
with allMdx
.-----------------------
||gatsby-node.js||
-----------------------
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions;
const blogPost = path.resolve(`./src/templates/blog-post.tsx`);
const blogList = path.resolve(`./src/templates/blog-list.tsx`);
const tagTemplate = path.resolve(`./src/templates/tags.tsx`);
return graphql(
`
{
- allMarkdownRemark(
+ allMdx(
sort: { fields: [frontmatter___date], order: DESC }
limit: 1000
) {
edges {
onCreateNode
API. Where it compares the node type to create slugs, there replace MarkdownRemark
to Mdx
.-----------------------
||gatsby-node.js||
-----------------------
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions;
- if (node.internal.type === `MarkdownRemark`) {
+ if (node.internal.type === `Mdx`) {
const value = createFilePath({ node, getNode });
if (typeof node.frontmatter.slug !== 'undefined') {
createNodeField({
node,
name: 'slug',
value: node.frontmatter.slug,
});
} else {
allMarkdownRemark
change it to allMdx
and markdownRemark
to mdx
. Also in the mdx
feild in query, change html
to body
.------------------------------------
||src/templates/blog-post.tsx||
------------------------------------
export const pageQuery = graphql`
query BlogPostBySlug($slug: String!, $tag: [String!]) {
site {
siteMetadata {
siteUrl
}
}
- markdownRemark(fields: { slug: { eq: $slug } }) {
+ mdx(fields: { slug: { eq: $slug } }) {
id
excerpt(pruneLength: 160)
- html
+ body
fields {
slug
}
frontmatter {
title
date(formatString: "DD MMM, YYYY")
description
hasJs
tags
cover {
publicURL
childImageSharp {
fluid(maxWidth: 1170, quality: 100) {
...GatsbyImageSharpFluid_noBase64
}
}
}
}
}
- allMarkdownRemark(
+ allMdx(
limit: 3
sort: { fields: [frontmatter___date], order: DESC }
filter: {
frontmatter: { tags: { in: $tag } }
fields: { slug: { ne: $slug } }
}
) {
edges {
allMarkdownRemark
. I received this error from gatsby-plugin-feed
plugin to resolve this i moved this inside gatsbyRemarkPlugins
option in gatsby-plugin-mdx
from the main plugins array.MDXRenderer
component from gatsby-plugin-mdx
.-----------------------------------------------------
||src/components/post-details/post-details.tsx||
-----------------------------------------------------
import _ from 'lodash';
import { MDXRenderer } from 'gatsby-plugin-mdx';
import { Link } from 'gatsby';
dangerouslySetInnerHTML
to a MDXRenderer
component:-----------------------------------------------------
||src/components/post-details/post-details.tsx||
-----------------------------------------------------
<PostDescriptionWrapper className="post_des_wrapper">
- <PostDescription
- dangerouslySetInnerHTML={{ __html: description }}
- className="post_des"
- />
+ <PostDescription className="post_des">
+ <MDXRenderer>{description}</MDXRenderer>
+ </PostDescription>
{tags == null ? null : (
<PostTags>
{tags.map((tag, index) => (
<Link key={index} to={`/tags/${_.kebabCase(tag)}/`}>
{`#${tag}`}
</Link>
))}
</PostTags>
)}
</PostDescriptionWrapper>
class
attribute in HTML component, change it to className
. So, that it doesn't create a conflict when processed by MDX.