This repository has been archived by the owner on Jun 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
/
gatsby-node.js
86 lines (76 loc) · 2.04 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
const path = require('path');
const _ = require('lodash');
// Remove trailing slash
exports.onCreatePage = ({ page, actions }) => {
const { createPage, deletePage } = actions;
return new Promise((resolve, reject) => {
// Remove trailing slash
const newPage = Object.assign({}, page, {
path: page.path === `/` ? page.path : page.path.replace(/\/$/, ``),
});
if (newPage.path !== page.path) {
// Remove the old page
deletePage(page);
// Add the new page
createPage(newPage);
}
resolve();
});
};
// Create pages from markdown nodes
exports.createPages = ({ actions, createContentDigest, createNodeId, graphql }) => {
const { createPage, createNode } = actions;
const slideTemplate = path.resolve(`src/templates/slide.js`);
return graphql(`
{
allMarkdownRemark {
edges {
node {
fileAbsolutePath
html
}
}
}
}
`).then(result => {
if (result.errors) {
return Promise.reject(result.errors);
}
const slides = result.data.allMarkdownRemark.edges;
slides.sort((a, b) => a.node.fileAbsolutePath > b.node.fileAbsolutePath ? 1 : -1)
const nodes = slides.flatMap((s) => s.node.html.split('<hr>').map((html) => ({
node: s.node, html
})));
nodes.forEach(({ node, html }, index) => {
createNode({
id: createNodeId(`${node.id}_${index + 1} >>> Slide`),
parent: node.id,
children: [],
internal: {
type: `Slide`,
contentDigest: createContentDigest(html),
},
html: html,
index: index + 1,
});
});
nodes.forEach((slide, index) => {
createPage({
path: `/${index + 1}`,
component: slideTemplate,
context: {
index: index + 1,
absolutePath: process.cwd() + `/src/slides#${index + 1}`,
},
});
});
});
};
exports.sourceNodes = ({ actions }) => {
actions.createTypes(`
type Slide implements Node {
html: String
index: Int
}
`);
};