forked from azdanov/ea-ultimate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
132 lines (117 loc) · 3.14 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
const slugify = require(`@sindresorhus/slugify`)
const { get, uniq } = require(`lodash`)
const { fmImagesToRelative } = require(`gatsby-remark-relative-images`)
const { createFilePath } = require(`gatsby-source-filesystem`)
const path = require(`path`)
const createPaginatedPages = require(`gatsby-paginate`)
const { createPath } = require(`./src/utils`)
/** @typedef {{data: {github: {repository: {object: {entries: [{name: string}]}}}}}} Data */
module.exports.onCreateBabelConfig = ({ actions: { setBabelPlugin } }) => {
setBabelPlugin({ name: `babel-plugin-tailwind-components` })
}
module.exports.createPages = async ({ actions, graphql }) => {
/** @type Data */
const names = await graphql(`
query {
github {
repository(owner: "kalessil", name: "phpinspectionsea") {
object(expression: "master:docs/") {
... on GitHub_Tree {
entries {
name
}
}
}
}
}
}
`)
if (names.errors) {
names.errors.forEach(e => console.error(e.toString()))
return
}
names.data.github.repository.object.entries.forEach(({ name }) => {
if (!name.endsWith(`.md`)) return
actions.createPage({
path: `docs/${createPath(name)}`,
component: path.resolve(`./src/templates/docsPage.js`),
context: {
expression: `master:docs/${name}`,
},
})
})
const posts = await graphql(`
{
allMarkdownRemark(
sort: { order: DESC, fields: [frontmatter___date] }
limit: 1000
) {
edges {
node {
id
timeToRead
fields {
slug
}
frontmatter {
description
title
tags
date(formatString: "MMMM DD, YYYY")
}
}
}
}
}
`)
if (posts.errors) {
posts.errors.forEach(e => console.error(e.toString()))
return
}
createPaginatedPages({
edges: posts.data.allMarkdownRemark.edges,
createPage: actions.createPage,
pageTemplate: path.resolve(`src/templates/blogIndex.js`),
pageLength: 2,
pathPrefix: `blog`,
context: {},
})
posts.data.allMarkdownRemark.edges.forEach(edge => {
const { id } = edge.node
actions.createPage({
path: edge.node.fields.slug,
tags: edge.node.frontmatter.tagPage,
component: path.resolve(`src/templates/blogPage.js`),
context: {
id,
},
})
})
let tags = []
posts.data.allMarkdownRemark.edges.forEach(edge => {
if (get(edge, `node.frontmatter.tags`)) {
tags = tags.concat(edge.node.frontmatter.tags)
}
})
tags = uniq(tags)
tags.forEach(tag => {
actions.createPage({
path: `/tags/${slugify(tag)}/`,
component: path.resolve(`src/templates/tagPage.js`),
context: {
tag,
},
})
})
}
module.exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
fmImagesToRelative(node)
if (node.internal.type === `MarkdownRemark`) {
createNodeField({
name: `slug`,
node,
value: createFilePath({ node, getNode }),
})
}
}