-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgatsby-node.js
103 lines (95 loc) · 3 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
const fs = require('fs');
const path = require('path');
const { createFilePath } = require('gatsby-source-filesystem');
const site = require('./site-data');
const { multiSlashRE } = require('./lib/fmt');
const { prepareSources } = require('./lib/sources');
const nav = require('./lib/navigation');
const { cache, defaultTemplate, git, sources: rawSources } = site;
const sources = prepareSources(rawSources, git);
exports.onPostBootstrap = () => {
const navigation = nav.fromSources(sources);
const json = JSON.stringify({ ...site, sources, navigation });
fs.writeFileSync(cache, json);
};
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;
// Ensures we are processing only markdown files
if (node.internal.type === 'MarkdownRemark') {
const { sourceInstanceName } = getNode(node.parent);
const source = sources.find(s => s.name === sourceInstanceName);
const { basePath, baseURI, directory, template = defaultTemplate } = source;
const relativeFilePath = createFilePath({
node,
getNode,
basePath,
}).replace(directory, '');
const slug = node.frontmatter && node.frontmatter.slug;
const pathname = `/${baseURI}/${slug || relativeFilePath}`.replace(multiSlashRE, '/');
// Add a field to each markdown node to indicate its source instance. This
// is used by the gatsby-remark-prefix-relative-links plugin.
createNodeField({
node,
name: 'sourceInstanceName',
value: sourceInstanceName,
});
// Creates new query'able field with name of 'pathname'
createNodeField({
node,
name: 'pathname',
value: pathname,
});
createNodeField({
node,
name: 'template',
value: template,
});
}
};
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const result = await graphql(`
query {
allMarkdownRemark {
edges {
node {
fields {
pathname
sourceInstanceName
template
}
}
}
}
}
`);
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
const { fields: { pathname, sourceInstanceName, template } } = node;
// Skip other /index.md pages so they don't overwrite content/index.md
if (pathname === '/' && sourceInstanceName !== 'content') return;
const component = path.resolve(template);
createPage({
path: pathname,
component,
// Context is available in page queries as GraphQL variables.
context: { pathname, sourceInstanceName, template },
});
});
};
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
const typeDefs = `
type MarkdownRemark implements Node {
frontmatter: Frontmatter
}
type Frontmatter {
author: String
canonical: String
# Date is the only required (non-nullable) field.
date: Date! @dateformat
slug: String
title: String
}
`
createTypes(typeDefs)
}