This repository has been archived by the owner on Jun 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
62 lines (53 loc) · 1.75 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
/* Create slug from file-path and append that slug to the 'fields' section under GraphQl so that we can reference it inside pages */
const { createFilePath } = require(`gatsby-source-filesystem`);
exports.onCreateNode = ({ node, getNode, actions }) => {
if (node.internal.type === `MarkdownRemark`) {
const { createNodeField } = actions;
let slug = createFilePath({ node, getNode, basePath: `pages`, trailingSlash:false }); /* basePath -- path inside src folder to act as base path */
createNodeField({
node,
name: `slug`,
value: slug.replace(/ +/g,"-").replace(/-+/g,"-") //slug.replace(/\/$/,"") //also remove trailing slash if any (this is not req coz while creating the slug using createFilePath(), we have already set 'trailingSlash' as false)
})
}
}
const path = require(`path`);
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
////fetch the 'demo' md files and create pages for them
const demoPagesQueryResult = await graphql(`
query {
allMarkdownRemark(
filter: {
fileAbsolutePath: {regex: "//demo/[a-zA-Z0-9- ]+/index.md$/"},
frontmatter: {title: {regex: "/[a-zA-Z0-9]+$/"}}
}
) {
edges {
node {
fields {
slug
}
}
}
}
}
`);
//create pages for each of the 'demo' pages
demoPagesQueryResult.data.allMarkdownRemark.edges.forEach(({ node }) => {
node.fields.slug!==null && node.fields.slug.trim()!=='' && createPage({
path: node.fields.slug,
component: path.resolve(`./src/templates/demo.js`),
context: {
// Data passed to context is available
// in page queries as GraphQL variables.
slug: node.fields.slug
},
})
})
//create 'demos' page
createPage({
path: `/demos`,
component: path.resolve(`./src/templates/demos.js`),
})
}