-
Notifications
You must be signed in to change notification settings - Fork 31
/
gatsby-node.js
159 lines (131 loc) · 4.03 KB
/
gatsby-node.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
'use strict'
const { createFilePath } = require('gatsby-source-filesystem')
const recipes = require('@microlink/recipes')
const { kebabCase, map } = require('lodash')
const { getDomain } = require('tldts')
const { promisify } = require('util')
const path = require('path')
const exec = promisify(require('child_process').exec)
exec.stdout = (...args) => exec(...args).then(({ stdout }) => stdout.trim())
const RECIPES_BY_FEATURES_KEYS = Object.keys(
require('@microlink/recipes/by-feature')
)
const getLastModifiedDate = filepath =>
exec.stdout(`git log --max-count=1 --format="%cI" -- ${filepath}`)
const branchName = () => exec.stdout('git rev-parse --abbrev-ref HEAD')
const githubUrl = (() => {
return async filepath => {
const base = `https://github.com/microlinkhq/www/blob/${await branchName()}`
const relative = filepath.replace(process.cwd(), '')
return base + relative
}
})()
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
resolve: {
modules: [path.resolve(__dirname, 'src'), 'node_modules'],
fallback: {
path: require.resolve('path-browserify')
}
}
})
}
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
if (node.internal.type === 'MarkdownRemark') {
const slug = createFilePath({ node, getNode, basePath: 'pages' })
createNodeField({
node,
name: 'slug',
value: slug
})
}
}
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions
return Promise.all([
createMarkdownPages({ graphql, createPage }),
createRecipesPages({ createPage, recipes })
])
}
const getMqlCode = (recipe, { name }) => `const mql = require('@microlink/mql')
const ${name} = ${recipe.toString()}
const result = await ${name}('${recipe.meta.examples[0]}')
mql.render(result)`
const getFunctionCode = (
recipe,
{ name }
) => `const mql = require('@microlink/mql')
const code = ${recipe.code}
const ${name} = (url, props) =>
mql(url, { function: code.toString(), meta: false, ...props })
.then(({ data }) => data.function)
const result = await ${name}('${recipe.meta.examples[0]}')
mql.render(result)
`
const getCode = (recipe, { name }) =>
(recipe.code ? getFunctionCode : getMqlCode)(recipe, { name })
const createRecipesPages = async ({ createPage, recipes }) => {
const pages = map(recipes, async (recipe, recipeName) => {
const slug = kebabCase(recipeName)
const route = `/recipes/${slug}`
const isProvider = !RECIPES_BY_FEATURES_KEYS.includes(recipeName)
const url = isProvider && recipe.meta.examples[0]
const domain = url ? getDomain(url) : 'microlink.io'
const description = isProvider
? `Interact with ${domain}`
: recipe.meta.description
const code = getCode(recipe, { name: recipeName })
return createPage({
path: route,
component: path.resolve('./src/templates/recipe.js'),
context: {
...recipe.meta,
slug,
code,
domain,
isProvider,
url,
key: recipeName,
description
}
})
})
return Promise.all(pages)
}
const createMarkdownPages = async ({ graphql, createPage }) => {
const query = `
{
allMarkdownRemark {
edges {
node {
fileAbsolutePath
fields {
slug
}
}
}
}
}
`
const result = await graphql(query)
if (result.errors) {
console.log(result.errors)
throw result.errors
}
const pages = result.data.allMarkdownRemark.edges.map(async ({ node }) => {
const slug = node.fields.slug.replace(/\/+$/, '')
return createPage({
path: slug,
component: path.resolve('./src/templates/index.js'),
context: {
githubUrl: await githubUrl(node.fileAbsolutePath),
lastEdited: await getLastModifiedDate(node.fileAbsolutePath),
isBlogPage: node.fields.slug.startsWith('/blog/'),
isDocPage: node.fields.slug.startsWith('/docs/'),
slug: node.fields.slug
}
})
})
return Promise.all(pages)
}