This repository has been archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.ts
96 lines (85 loc) · 2.08 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { GatsbyNode, Node } from 'gatsby'
import { createFilePath } from 'gatsby-source-filesystem'
import path from 'path'
interface ExtendedNode extends Node {
frontmatter: {
layout: string
}
}
export const onCreateNode: GatsbyNode['onCreateNode'] = ({ node, getNode, actions }) => {
const { createNodeField } = actions
if (node.internal.type === `MarkdownRemark`) {
const slug = createFilePath({ node, getNode, basePath: `pages` })
const { layout } = (node as ExtendedNode).frontmatter
createNodeField({
node,
name: `slug`,
value: `/${layout}${slug}`
})
createNodeField({
node,
name: `layout`,
value: layout
})
}
}
interface EdgesData {
errors?: any
data?: {
allMarkdownRemark: {
edges: {
node: {
fields: {
layout: string
slug: string
}
}
}[]
}
}
}
export const createPages: GatsbyNode['createPages'] = async ({ graphql, actions }) => {
const { createPage } = actions
const allMarkdown: EdgesData = await graphql(`
{
allMarkdownRemark(filter: { fields: { layout: { ne: "other" } } }, limit: 1000) {
edges {
node {
fields {
layout
slug
}
}
}
}
}
`)
if (allMarkdown.errors) {
console.error(allMarkdown.errors)
throw new Error(allMarkdown.errors)
}
allMarkdown.data?.allMarkdownRemark.edges.forEach(({ node }) => {
const { slug, layout } = node.fields
createPage({
path: slug,
component: path.resolve(`./src/templates/${layout}.tsx`),
context: {
slug
}
})
})
}
/// todo: check why this method broke the image processing job and made the gatsby-image plugin miss the images
/*
export const createSchemaCustomization: GatsbyNode['createSchemaCustomization'] = ({ actions }) => {
const { createTypes } = actions
const typeDefs = `
type MarkdownRemarkFrontmatter @infer {
featuredImage: File
funnyImage: File
ogImage: File
}
`
createTypes(typeDefs)
}
*/