2018-02-10 15:56:56 +03:00
|
|
|
const path = require('path');
|
2019-07-28 21:34:41 +03:00
|
|
|
const _ = require('lodash');
|
2018-09-19 16:24:14 +03:00
|
|
|
|
|
|
|
// Remove trailing slash
|
|
|
|
exports.onCreatePage = ({ page, actions }) => {
|
|
|
|
const { createPage, deletePage } = actions;
|
2017-11-24 02:22:47 +03:00
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// Remove trailing slash
|
|
|
|
const newPage = Object.assign({}, page, {
|
2018-07-30 17:54:57 +03:00
|
|
|
path: page.path === `/` ? page.path : page.path.replace(/\/$/, ``),
|
2017-11-24 02:22:47 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (newPage.path !== page.path) {
|
|
|
|
// Remove the old page
|
|
|
|
deletePage(page);
|
|
|
|
// Add the new page
|
|
|
|
createPage(newPage);
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
};
|
2018-02-10 15:56:56 +03:00
|
|
|
|
2018-09-09 19:11:30 +03:00
|
|
|
// Create pages from markdown nodes
|
2019-07-28 21:34:41 +03:00
|
|
|
exports.createPages = ({ actions, createContentDigest, createNodeId, graphql }) => {
|
2019-02-22 00:38:03 +03:00
|
|
|
const { createPage, createNode } = actions;
|
2019-05-26 02:02:08 +03:00
|
|
|
const slideTemplate = path.resolve(`src/templates/slide.js`);
|
2018-02-10 15:56:56 +03:00
|
|
|
|
|
|
|
return graphql(`
|
|
|
|
{
|
2019-02-22 00:38:03 +03:00
|
|
|
allMarkdownRemark {
|
2018-09-09 19:11:30 +03:00
|
|
|
edges {
|
|
|
|
node {
|
2019-07-28 21:34:41 +03:00
|
|
|
fileAbsolutePath
|
2018-09-09 19:11:30 +03:00
|
|
|
html
|
|
|
|
}
|
|
|
|
}
|
2018-02-10 15:56:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`).then(result => {
|
|
|
|
if (result.errors) {
|
|
|
|
return Promise.reject(result.errors);
|
|
|
|
}
|
|
|
|
|
2019-02-22 00:38:03 +03:00
|
|
|
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
|
2019-07-28 21:34:41 +03:00
|
|
|
})));
|
2019-02-22 00:38:03 +03:00
|
|
|
|
|
|
|
nodes.forEach(({ node, html }, index) => {
|
|
|
|
createNode({
|
|
|
|
id: createNodeId(`${node.id}_${index + 1} >>> Slide`),
|
|
|
|
parent: node.id,
|
|
|
|
children: [],
|
|
|
|
internal: {
|
|
|
|
type: `Slide`,
|
2019-07-28 21:34:41 +03:00
|
|
|
contentDigest: createContentDigest(html),
|
2019-02-22 00:38:03 +03:00
|
|
|
},
|
|
|
|
html: html,
|
|
|
|
index: index + 1,
|
|
|
|
});
|
2019-07-28 21:34:41 +03:00
|
|
|
});
|
2018-07-30 17:54:57 +03:00
|
|
|
|
2019-02-22 00:38:03 +03:00
|
|
|
nodes.forEach((slide, index) => {
|
2018-09-09 19:11:30 +03:00
|
|
|
createPage({
|
|
|
|
path: `/${index + 1}`,
|
2019-05-26 02:02:08 +03:00
|
|
|
component: slideTemplate,
|
2018-09-09 19:11:30 +03:00
|
|
|
context: {
|
2018-09-19 16:24:14 +03:00
|
|
|
index: index + 1,
|
2018-09-09 19:11:30 +03:00
|
|
|
absolutePath: process.cwd() + `/src/slides#${index + 1}`,
|
|
|
|
},
|
|
|
|
});
|
2018-02-10 15:56:56 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2019-07-28 21:34:41 +03:00
|
|
|
|
|
|
|
exports.sourceNodes = ({ actions }) => {
|
|
|
|
actions.createTypes(`
|
|
|
|
type Slide implements Node {
|
|
|
|
html: String
|
|
|
|
index: Int
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
};
|