forked from violentmonkey/violentmonkey.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.js
More file actions
122 lines (117 loc) · 3.01 KB
/
gatsby-node.js
File metadata and controls
122 lines (117 loc) · 3.01 KB
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
116
117
118
119
120
121
122
const _ = require('lodash');
const path = require('path');
const slash = require('slash');
const { createFilePath } = require('gatsby-source-filesystem');
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions;
if (node.internal.type === 'MarkdownRemark') {
let slug = node.frontmatter.path;
const parentNode = getNode(node.parent);
const type = parentNode.sourceInstanceName;
if (!slug) {
if (type === 'pages') {
slug = createFilePath({ node, getNode, basePath: 'content/pages' });
} else {
slug = createFilePath({ node, getNode, basePath: 'content' });
}
}
createNodeField({
node,
name: 'draft',
value: !!node.frontmatter.draft,
});
createNodeField({
node,
name: 'slug',
value: slug,
});
createNodeField({
node,
name: 'type',
value: type,
});
if (node.frontmatter.tags) {
const tagSlugs = node.frontmatter.tags.map(tag => `/tags/${_.kebabCase(tag)}/`);
createNodeField({ node, name: 'tagSlugs', value: tagSlugs });
}
}
};
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const postsTemplate = path.resolve('./src/templates/index-template.js');
const postTemplate = path.resolve('./src/templates/post-template.js');
const tagTemplate = path.resolve('./src/templates/tag-template.js');
const result = await graphql(`
{
allMarkdownRemark(
limit: 1000
filter: {
fields: { draft: { ne: true } }
}
sort: { fields: [frontmatter___date], order: DESC },
) {
edges {
node {
fields {
slug
type
}
frontmatter {
title
date
tags
type
sidebar {
match
order
}
}
}
}
}
}
`);
if (result.errors) throw result.errors;
createPage({
path: '/posts/',
component: slash(postsTemplate),
context: {
type: 'posts',
},
});
result.data.allMarkdownRemark.edges.forEach(edge => {
const { slug } = edge.node.fields;
const type = edge.node.frontmatter.type || edge.node.fields.type;
if (type === 'pages') {
createPage({
path: slug,
component: slash(postTemplate),
context: {
slug,
type,
},
});
} else if (type === 'posts') {
createPage({
path: slug,
component: slash(postTemplate),
context: {
slug,
type,
},
});
let tags = new Set();
if (_.get(edge, 'node.frontmatter.tags')) {
tags = new Set([...tags, ...edge.node.frontmatter.tags]);
}
tags.forEach(tag => {
const tagPath = `/tags/${_.kebabCase(tag)}/`;
createPage({
path: tagPath,
component: tagTemplate,
context: { tag },
});
});
}
});
};