Skip to content

Commit 87938e4

Browse files
committed
temp: programmatically create mdx pages
1 parent 59caf29 commit 87938e4

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

src/pages/docs/chat/index.mdx renamed to content/chat/index.mdx

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
title: About Chat
3+
slug: /docs/chat
34
meta_description: 'Learn more about Ably Chat and the features that enable you to quickly build functionality into new and existing applications.'
45
redirect_from: /docs/products/chat
56
---
File renamed without changes.

data/createPages/index.ts

+58
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,24 @@ interface ApiReferenceQueryResult {
7070
};
7171
}
7272

73+
interface MdxEdge {
74+
node: {
75+
id: string;
76+
frontmatter: {
77+
slug: string;
78+
};
79+
internal: {
80+
contentFilePath: string;
81+
};
82+
};
83+
}
84+
85+
interface MdxQueryResult {
86+
allMdx: {
87+
edges: MdxEdge[];
88+
};
89+
}
90+
7391
export const createPages: GatsbyNode['createPages'] = async ({ graphql, actions: { createPage, createRedirect } }) => {
7492
/**
7593
* It's not ideal to have:
@@ -104,6 +122,25 @@ export const createPages: GatsbyNode['createPages'] = async ({ graphql, actions:
104122
}
105123
}
106124
`);
125+
126+
const mdxResult = await graphql<MdxQueryResult>(`
127+
query {
128+
allMdx {
129+
edges {
130+
node {
131+
id
132+
frontmatter {
133+
slug
134+
}
135+
internal {
136+
contentFilePath
137+
}
138+
}
139+
}
140+
}
141+
}
142+
`);
143+
107144
/**
108145
* We could log here, the reason we don't right now is that the error should already have been caught and logged.
109146
* Because Gatsby spawns a bunch of async processes during the onCreateNode step, though, and errors don't prevent
@@ -207,6 +244,26 @@ export const createPages: GatsbyNode['createPages'] = async ({ graphql, actions:
207244
return slug;
208245
};
209246

247+
const mdxCreator = async (edge: MdxEdge): Promise<string> => {
248+
const slug = edge.node.frontmatter.slug;
249+
250+
if (!slug) {
251+
return slug;
252+
}
253+
254+
createPage({
255+
path: slug,
256+
component: edge.node.internal.contentFilePath,
257+
context: {
258+
slug,
259+
mdx: true,
260+
layout: { sidebar: true, searchBar: true, template: 'base' },
261+
},
262+
});
263+
264+
return slug;
265+
};
266+
210267
createRedirect({
211268
fromPath: '/',
212269
toPath: '/docs',
@@ -233,5 +290,6 @@ export const createPages: GatsbyNode['createPages'] = async ({ graphql, actions:
233290
await Promise.all([
234291
...documentResult.data.allFileHtml.edges.map(documentCreator(documentTemplate)),
235292
...apiReferenceResult.data.allFileHtml.edges.map(documentCreator(apiReferenceTemplate)),
293+
...(mdxResult.data?.allMdx.edges.map(mdxCreator) ?? []),
236294
]);
237295
};

gatsby-browser.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,14 @@ export const wrapRootElement = ({ element }) => {
7878
);
7979
};
8080

81-
export const wrapPageElement = ({ element, props }: { element: React.ReactNode }) => {
81+
export const wrapPageElement = ({ element }: { element: React.ReactNode }) => {
8282
return (
8383
<MDXProvider
8484
components={{
8585
h1: (props: any) => <h1 className="text-4xl font-bold mb-4" {...props} />,
8686
h2: (props: any) => <h2 className="text-2xl font-semibold mb-3" {...props} />,
8787
}}
8888
>
89-
<div className="max-w-md md:pr-24 md:border-r border-mid-grey">{JSON.stringify(props)}</div>
9089
{element}
9190
</MDXProvider>
9291
);

0 commit comments

Comments
 (0)