-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.ts
62 lines (55 loc) · 1.31 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
57
58
59
60
61
62
import type { CreatePagesArgs } from 'gatsby';
import path from 'path';
interface Node {
sourceInstanceName: string;
childMarkdownRemark: { frontmatter: { id: string } };
}
const getPath = (node: Node) =>
`${node.sourceInstanceName}/${node.childMarkdownRemark.frontmatter.id}`;
const getComponent = (node: Node) =>
path.resolve(`./src/templates/${node.sourceInstanceName}.tsx`);
type CreatePageFn = (first: {
path: string;
component: string;
context: { id: string };
}) => void;
const processNode =
(createPage: CreatePageFn) =>
({ node }) => {
if (node.childMarkdownRemark) {
createPage({
path: getPath(node),
component: getComponent(node),
context: {
id: node.childMarkdownRemark.frontmatter.id,
},
});
}
};
interface Data {
allFile: {
edges: {
node: Node;
}[];
};
}
export const createPages = async ({ graphql, actions }: CreatePagesArgs) => {
const { createPage } = actions;
const result = await graphql(`
query {
allFile {
edges {
node {
sourceInstanceName
childMarkdownRemark {
frontmatter {
id
}
}
}
}
}
}
`);
(result.data as Data).allFile.edges.forEach(processNode(createPage));
};