-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.ts
56 lines (51 loc) · 2.16 KB
/
gatsby-node.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import express from 'express';
import { parseMDX } from '@tinacms/mdx';
import { GatsbyNode } from 'gatsby';
import path from 'path';
const queries = require('./tina/__generated__/queries');
export const createPages: GatsbyNode['createPages'] = async ({ graphql, actions }) => {
const { createPage } = actions;
const result = (await graphql(`
{
allFile(filter: { extension: { eq: "mdx" } }) {
edges {
node {
id
childMdx {
frontmatter {
slug
}
body
}
}
}
}
}
`)) as { data: { allFile: { edges: { node: { childMdx: { frontmatter: { slug: string }; body: string } } }[] } } };
queries.collections.forEach((collection: any) => {
console.log(`Collection: ${collection}`);
result.data.allFile.edges.forEach(
({ node }: { node: { childMdx: { frontmatter: { slug: string }; body: string } } }) => {
const { frontmatter, body } = node.childMdx;
createPage({
path: `${collection.collectionName}/${frontmatter.slug.toLowerCase()}`,
component: path.resolve(`./src/templates/${collection.collectionName}.js`),
context: {
parsedMdx: parseMDX(
body,
{ type: 'rich-text', name: 'markdownParser', parser: { type: 'markdown' } },
(s: string) => s
),
variables: { relativePath: frontmatter.slug + '.mdx' },
query: `${collection.queries.frag}${collection.queries.query}`,
},
defer: true,
});
}
);
});
};
//Required as per https://tina.io/docs/frameworks/gatsby/#allowing-static-adminindexhtml-file-in-dev-mode
exports.onCreateDevServer = ({ app }: { app: Express }) => {
app.use('/admin', express.static('public/admin'));
};