Skip to content

Commit 448319b

Browse files
Merge pull request #2 from howtographql/mdx-config
Mdx config
2 parents 4061562 + b6dcf79 commit 448319b

File tree

14 files changed

+227
-48
lines changed

14 files changed

+227
-48
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"license": "MIT",
88
"private": true,
99
"scripts": {
10-
"site": "yarn workspace @howtographql/oss"
10+
"site": "yarn workspace @howtographql/oss",
11+
"theme": "yarn workspace @howtographql/gatsby-theme"
1112
},
1213
"workspaces": {
1314
"packages": [
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
require('prismjs/themes/prism-tomorrow.css');
1+
import { wrapRootElement as wrap } from "./wrap-root-element";
2+
// require("prismjs/themes/prism-tomorrow.css");
3+
4+
export const wrapRootElement = wrap;

packages/gatsby-theme/gatsby-config.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,30 @@ module.exports = (opts = {}) => {
1212
"gatsby-plugin-sharp",
1313
"gatsby-plugin-typescript",
1414
"gatsby-plugin-root-import",
15-
"gatsby-transformer-remark",
1615
"gatsby-image",
1716
"gatsby-plugin-styled-components",
1817
"gatsby-plugin-offline",
1918
{
2019
resolve: `gatsby-mdx`,
2120
options: {
22-
decks: [],
23-
defaultLayouts: {
24-
default: require.resolve("./src/components/postLayout.tsx"),
25-
...mdxLayouts
26-
},
27-
extensions: [".mdx", ".md"],
28-
gatsbyRemarkPlugins: [
29-
{
30-
resolve: "gatsby-remark-prismjs",
31-
options: {
32-
classPrefix: "language-",
33-
inlineCodeMarker: {
34-
tsx: "tsx"
35-
},
36-
aliases: {}
37-
}
38-
}
39-
]
21+
// decks: [],
22+
// defaultLayouts: {
23+
// default: require.resolve("./src/components/postLayout.tsx"),
24+
// ...mdxLayouts
25+
// },
26+
extensions: [".mdx", ".md"]
27+
// gatsbyRemarkPlugins: [
28+
// {
29+
// resolve: "gatsby-remark-prismjs",
30+
// options: {
31+
// classPrefix: "language-",
32+
// inlineCodeMarker: {
33+
// tsx: "tsx"
34+
// },
35+
// aliases: {}
36+
// }
37+
// }
38+
// ]
4039
}
4140
},
4241
{

packages/gatsby-theme/gatsby-node.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// const componentWithMDXScope = require('gatsby-mdx/component-with-mdx-scope');
2-
const { getTutorialPath } = require('./src/utils/getTutorialPath.js');
2+
const { getTutorialPath } = require("./src/utils/getTutorialPath.js");
3+
const path = require("path");
34

45
exports.createPages = async ({ graphql, actions }) => {
56
const { createPage } = actions;
7+
const postLayout = require.resolve(`./src/components/postLayout.tsx`);
68

79
const { data } = await graphql(`
810
{
@@ -23,10 +25,10 @@ exports.createPages = async ({ graphql, actions }) => {
2325
const tutorialPath = getTutorialPath(node.fileAbsolutePath);
2426
createPage({
2527
path: tutorialPath,
26-
component: node.fileAbsolutePath,
28+
component: postLayout, // node.fileAbsolutePath,
2729
context: {
28-
pagePath: node.frontmatter.path,
29-
},
30+
pagePath: node.frontmatter.path
31+
}
3032
});
3133
});
3234
};

packages/gatsby-theme/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@
4444
"gatsby-plugin-styled-components": "^3.0.7",
4545
"gatsby-plugin-typescript": "^2.0.11",
4646
"gatsby-plugin-typography": "^2.2.13",
47-
"gatsby-remark-prismjs": "^3.2.5",
4847
"gatsby-source-filesystem": "^2.0.24",
4948
"gatsby-transformer-remark": "^2.3.2",
5049
"gatsby-transformer-sharp": "^2.1.17",
50+
"mdx-utils": "^0.1.1",
51+
"prism-react-renderer": "^0.1.6",
5152
"prismjs": "^1.15.0",
5253
"prop-types": "^15.7.2",
5354
"react-helmet": "^5.2.0",
55+
"react-live": "^2.0.1",
5456
"react-typography": "^0.16.19",
5557
"styled-components": "^4.1.3",
5658
"typography": "^0.16.19"
@@ -89,4 +91,4 @@
8991
"html"
9092
]
9193
}
92-
}
94+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import React from 'react'
2+
import Highlight, { defaultProps } from 'prism-react-renderer'
3+
import DraculaTheme from 'prism-react-renderer/themes/dracula'
4+
import { LiveProvider, LiveEditor, LiveError, LivePreview } from 'react-live'
5+
import Prism from 'prismjs/components/prism-core';
6+
7+
Prism.languages.graphql = { comment: /#.*/, string: { pattern: /"(?:\\.|[^\\"\r\n])*"/, greedy: !0 }, number: /(?:\B-|\b)\d+(?:\.\d+)?(?:e[+-]?\d+)?\b/i, "boolean": /\b(?:true|false)\b/, variable: /\$[a-z_]\w*/i, directive: { pattern: /@[a-z_]\w*/i, alias: "function" }, "attr-name": /[a-z_]\w*(?=\s*(?:\([^()]*\))?:)/i, keyword: [{ pattern: /(fragment\s+(?!on)[a-z_]\w*\s+|\.{3}\s*)on\b/, lookbehind: !0 }, /\b(?:query|fragment|mutation)\b/], operator: /!|=|\.{3}/, punctuation: /[!(){}\[\]:=,]/ };
8+
9+
export const Code = (props) => {
10+
return <div style={{ position: "relative" }}>
11+
<CodeRenderer {...props} />
12+
<button
13+
style={{ position: "absolute", top: 10, right: 10 }}
14+
onClick={() => {
15+
// Copying code here!
16+
}}
17+
>
18+
Copy
19+
</button>
20+
</div>
21+
}
22+
23+
const CodeRenderer = ({ codeString, language, ...props }) => {
24+
console.log(codeString, language);
25+
if (props['react-live']) {
26+
return (
27+
<LiveProvider code={codeString} noInline={true}>
28+
<LiveEditor />
29+
<LiveError />
30+
<LivePreview />
31+
</LiveProvider>
32+
)
33+
} else {
34+
return (
35+
<Highlight {...defaultProps} theme={DraculaTheme} Prism={Prism} code={codeString} language={language}>
36+
{({ className, style, tokens, getLineProps, getTokenProps }) => (
37+
<pre className={className} style={style}>
38+
{tokens.map((line, i) => (
39+
<div {...getLineProps({ line, key: i })}>
40+
{line.map((token, key) => (
41+
<span {...getTokenProps({ token, key })} />
42+
))}
43+
</div>
44+
))}
45+
</pre>
46+
)}
47+
</Highlight>
48+
)
49+
}
50+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
declare module 'gatsby-mdx';
2+
declare module 'prismjs/components/prism-core';

packages/gatsby-theme/src/components/postLayout.tsx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { RouterProps } from '@reach/router';
21
import * as React from 'react';
2+
import { RouterProps } from '@reach/router';
33
import { PostQueryData } from '../interfaces/PostQuery.interface';
44
import Layout from './layout';
5+
import { MDXRenderer } from "gatsby-mdx";
6+
import { graphql } from "gatsby";
57

68
type PostLayoutProps = PostQueryData & RouterProps;
79

@@ -14,15 +16,30 @@ const PostLayout: React.FunctionComponent<PostLayoutProps> = ({
1416
}
1517

1618
const { title, date } = data.mdx.frontmatter;
17-
const { location, children } = props;
19+
const { location } = props;
1820

1921
return (
2022
<Layout location={location}>
2123
<h1>{title}</h1>
2224
<em>{date}</em>
23-
{children}
25+
<MDXRenderer>{data.mdx.code.body}</MDXRenderer>
2426
</Layout>
2527
);
2628
};
2729

2830
export default PostLayout;
31+
32+
export const pageQuery = graphql`
33+
query MDXQuery($pagePath: String!) {
34+
mdx(frontmatter: { path: { eq: $pagePath } }) {
35+
id
36+
code {
37+
body
38+
}
39+
frontmatter {
40+
path
41+
title
42+
}
43+
}
44+
}
45+
`;

packages/gatsby-theme/src/hooks/useAllTutorialQuery.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export const useAllTutorialQuery = () => {
1212
frontmatter {
1313
path
1414
title
15+
description
16+
}
17+
code {
18+
body
1519
}
1620
}
1721
}

packages/gatsby-theme/src/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare module 'gatsby-mdx/mdx-renderer';

packages/gatsby-theme/src/interfaces/PostQuery.interface.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ export interface PostQueryData {
44
frontmatter: {
55
path: string;
66
title: string;
7+
date: Date
78
};
9+
code: {
10+
body: any
11+
}
812
};
913
};
1014
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from "react";
2+
import { MDXProvider } from "@mdx-js/react";
3+
import { preToCodeBlock } from "mdx-utils";
4+
import { Code } from "./src/components/code";
5+
6+
// components is its own object outside of render so that the references to
7+
// components are stable
8+
const components = {
9+
pre: preProps => {
10+
const props = preToCodeBlock(preProps);
11+
// if there's a codeString and some props, we passed the test
12+
if (props) {
13+
return <Code {...props} />;
14+
} else {
15+
// it's possible to have a pre without a code in it
16+
return <pre {...preProps} />;
17+
}
18+
}
19+
};
20+
export const wrapRootElement = ({ element }) => {
21+
return <MDXProvider components={components}>{element}</MDXProvider>;
22+
};

0 commit comments

Comments
 (0)