-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgatsby-node.ts
33 lines (26 loc) · 1 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
import type { GatsbyNode } from 'gatsby'
import type { EmailTemplate } from 'src/appTypes'
import fs from 'node:fs'
import path from 'node:path'
import yaml from 'js-yaml'
const EMAIL_TEMPLATES_DIR = './content/email-templates'
const loadEmailTemplateFromYaml = (filePath: string): EmailTemplate.Base.Config => {
const contents = fs.readFileSync(filePath, 'utf-8')
return yaml.load(contents) as any
}
const emailTemplateComponent = path.resolve('./src/templates/EmailEditorPage.tsx')
const createPages: GatsbyNode['createPages'] = ({ actions }) => {
const { createPage } = actions
const filePaths = fs.readdirSync(EMAIL_TEMPLATES_DIR)
filePaths.forEach((fileName) => {
const filePath = `${EMAIL_TEMPLATES_DIR}/${fileName}`
const slug = path.parse(fileName).name
const emailTemplate = loadEmailTemplateFromYaml(filePath)
createPage({
path: `email-templates/${slug}`,
component: emailTemplateComponent,
context: { emailTemplate },
})
})
}
exports.createPages = createPages