-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
115 lines (100 loc) · 2.63 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
const path = require('path');
const remark = require('remark');
const recommended = require('remark-preset-lint-recommended');
const html = require('remark-html');
const crypto = require('crypto');
exports.modifyWebpackConfig = function (config, stage) {
console.log('devtool was', config.devtool)
config._config.devtool = 'cheap-module-source-map'
return config
}
exports.onCreateNode = ({
node,
actions,
loadNodeContent,
createNodeId,
reporter,
}) => {
const { createNode } = actions;
if (
node.internal.mediaType !== `text/markdown` &&
node.internal.mediaType !== `text/x-markdown`
) {
return;
}
return new Promise(async (resolve, reject) => {
const content = await loadNodeContent(node);
const slides = content.split('---\n').map(body => body.trim());
slides.forEach((slide, index) => {
remark()
.use(recommended)
.use(html)
.process(slide, (err, file) => {
const digest = crypto
.createHash(`md5`)
.update(String(file))
.digest(`hex`);
createNode({
id: createNodeId(`${node.id}_${index + 1} >>> Slide`),
parent: node.id,
children: [],
internal: {
type: `Slide`,
contentDigest: digest,
},
html: String(file),
index: index + 1,
});
});
});
resolve();
});
};
// 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, graphql }) => {
const { createPage } = actions;
const blogPostTemplate = path.resolve(`src/templates/slide.js`);
return graphql(`
{
allSlide {
edges {
node {
html
}
}
}
}
`).then(result => {
if (result.errors) {
return Promise.reject(result.errors);
}
const slides = result.data.allSlide.edges;
slides.forEach((slide, index) => {
createPage({
path: `/${index + 1}`,
component: blogPostTemplate,
context: {
index: index + 1,
absolutePath: process.cwd() + `/src/slides#${index + 1}`,
},
});
});
});
};