-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathgatsby-node.ts
69 lines (64 loc) · 1.75 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
63
64
65
66
67
68
69
import { GatsbyNode } from "gatsby";
import { TemplateQueryData } from "./src/types/TemplateQueryData";
export const createPages: GatsbyNode["createPages"] = async ({
actions,
graphql,
reporter,
}) => {
const { createPage, createRedirect } = actions;
/** Shared logic between creating email and letter pages */
async function createEmailOrLetterTemplatePage({
layout,
component,
}: {
layout: string;
component: string;
}) {
const templateData = await graphql(`
{
allMarkdownRemark(filter: { frontmatter: { layout: { eq: "${layout}" } } }) {
edges {
node {
frontmatter {
permalink
redirect_from
}
}
}
}
}
`);
if (templateData.errors) {
reporter.panicOnBuild("Error while querying for email data.");
return;
}
(templateData.data as TemplateQueryData).allMarkdownRemark.edges.forEach(
({ node }) => {
// Create individual pages
createPage({
path: node.frontmatter.permalink,
component: component,
context: {
permalink: node.frontmatter.permalink,
},
});
// Create redirects
node.frontmatter.redirect_from?.forEach((redirectFrom) => {
createRedirect({
fromPath: redirectFrom,
toPath: node.frontmatter.permalink,
isPermanent: true,
});
});
}
);
}
await createEmailOrLetterTemplatePage({
layout: "email",
component: require.resolve("./src/components/email/Email.tsx"),
});
await createEmailOrLetterTemplatePage({
layout: "letter",
component: require.resolve("./src/components/letter/Letter.tsx"),
});
};