Skip to content

Commit

Permalink
feat(gatsby): add mdx plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
mdluo committed Feb 18, 2020
1 parent 5c157c9 commit af064aa
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 54 deletions.
79 changes: 41 additions & 38 deletions gatsby-config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
const path = require('path');
const pxtorem = require('postcss-pxtorem');

const gatsbyRemarkPlugins = [
{
resolve: 'gatsby-remark-images',
options: {
maxWidth: 960,
},
},
{
resolve: 'gatsby-remark-responsive-iframe',
options: { wrapperStyle: 'margin-bottom: 1.0725rem' },
},
{
resolve: `gatsby-remark-vscode`,
options: {
extensionDataDirectory: path.resolve('extensions'),
extensions: [
{
identifier: 'akamud.vscode-theme-onelight',
version: '2.1.0',
},
],
colorTheme: 'Atom One Light',
wrapperClassName: ({ parsedOptions }) => {
if (parsedOptions.ln === false) {
return 'no-ln';
}
},
getLineClassName: ({ codeFenceOptions }) => {
if (codeFenceOptions.ln === false) {
return 'no-ln';
}
},
},
},
'gatsby-remark-copy-linked-files',
'gatsby-remark-autolink-headers',
];

module.exports = {
siteMetadata: {
title: `mdluo's blog`,
Expand Down Expand Up @@ -89,45 +127,10 @@ module.exports = {
},
},
{
resolve: `gatsby-transformer-remark`,
resolve: `gatsby-plugin-mdx`,
options: {
plugins: [
{
resolve: 'gatsby-remark-images',
options: {
maxWidth: 960,
},
},
{
resolve: 'gatsby-remark-responsive-iframe',
options: { wrapperStyle: 'margin-bottom: 1.0725rem' },
},
{
resolve: `gatsby-remark-vscode`,
options: {
extensionDataDirectory: path.resolve('extensions'),
extensions: [
{
identifier: 'akamud.vscode-theme-onelight',
version: '2.1.0',
},
],
colorTheme: 'Atom One Light',
wrapperClassName: ({ parsedOptions }) => {
if (parsedOptions.ln === false) {
return 'no-ln';
}
},
getLineClassName: ({ codeFenceOptions }) => {
if (codeFenceOptions.ln === false) {
return 'no-ln';
}
},
},
},
'gatsby-remark-copy-linked-files',
'gatsby-remark-autolink-headers',
],
extensions: [`.md`, `.mdx`],
gatsbyRemarkPlugins,
},
},
`gatsby-transformer-sharp`,
Expand Down
7 changes: 2 additions & 5 deletions src/gatsby/createPages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import { LoadPagesQuery } from 'generated/types/gatsby';
*/
export const schema = graphql`
query loadPages($limit: Int!) {
allMarkdownRemark(
limit: $limit
filter: { frontmatter: { draft: { ne: true } } }
) {
allMdx(limit: $limit, filter: { frontmatter: { draft: { ne: true } } }) {
edges {
node {
frontmatter {
Expand Down Expand Up @@ -49,7 +46,7 @@ export const createPages: GatsbyNode['createPages'] = async ({
* Optional Chaining and Nullish Coalescing
* https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html
*/
(result?.data?.allMarkdownRemark?.edges ?? []).forEach(({ node }) => {
(result?.data?.allMdx?.edges ?? []).forEach(({ node }) => {
const { slug } = node.frontmatter;
createPage({
// Path for this page — required
Expand Down
4 changes: 2 additions & 2 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Layout from '../components/Layout';
const Index: React.FC = () => {
const data = useStaticQuery<GetPostsQuery>(graphql`
query GetPosts {
allMarkdownRemark(
allMdx(
sort: { order: DESC, fields: frontmatter___date }
filter: { frontmatter: { type: { eq: "post" } } }
) {
Expand All @@ -28,7 +28,7 @@ const Index: React.FC = () => {
`);
return (
<Layout padding={false}>
{(data?.allMarkdownRemark?.edges ?? []).map(({ node }) => (
{(data?.allMdx?.edges ?? []).map(({ node }) => (
<article className="border-bottom px-3 pt-3 pb-2">
<Link
className="text-gray-dark link-hover-gray-light"
Expand Down
18 changes: 9 additions & 9 deletions src/templates/post.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import moment from 'moment';
import { graphql } from 'gatsby';
import { MDXRenderer } from 'gatsby-plugin-mdx';
import { DiscussionEmbed } from 'disqus-react';

import Octicon, { Calendar, Tag, Link } from '@primer/octicons-react';
Expand All @@ -17,7 +18,7 @@ interface Props {

export const query = graphql`
query GetPostBySlug($slug: String!) {
markdownRemark(frontmatter: { slug: { eq: $slug } }) {
mdx(frontmatter: { slug: { eq: $slug } }) {
frontmatter {
type
slug
Expand All @@ -26,7 +27,7 @@ export const query = graphql`
link
tags
}
html
body
excerpt
}
site {
Expand All @@ -39,11 +40,11 @@ export const query = graphql`

const Post: React.FC<Props> = ({ data }) => {
const {
markdownRemark,
mdx,
site: { siteMetadata },
} = data;

const { type, title, date, slug, tags, link } = markdownRemark.frontmatter;
const { type, title, date, slug, tags, link } = mdx.frontmatter;

return (
<Layout>
Expand All @@ -67,10 +68,9 @@ const Post: React.FC<Props> = ({ data }) => {
)}
</p>
)}
<div
className="markdown-body mt-5 mb-6"
dangerouslySetInnerHTML={{ __html: markdownRemark.html }}
/>
<div className="markdown-body mt-5 mb-6">
<MDXRenderer>{mdx.body}</MDXRenderer>
</div>
<DiscussionEmbed
shortname={siteMetadata.disqus}
config={{
Expand All @@ -79,7 +79,7 @@ const Post: React.FC<Props> = ({ data }) => {
identifier: slug,
}}
/>
<SEO title={title} description={markdownRemark.excerpt} />
<SEO title={title} description={mdx.excerpt} />
</Layout>
);
};
Expand Down

0 comments on commit af064aa

Please sign in to comment.